blob: 625944d797f6b06e42191f5ef5a5872f3fd4211c [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
John McCallf7a1a742009-11-24 19:00:30 +000079static void FilterAcceptableTemplateNames(ASTContext &C, 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();
John McCallad00b772010-06-16 08:42:20 +000085 NamedDecl *Repl = isAcceptableTemplateName(C, 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
95 // is followed by a template-argument-list, the reference refers to the
96 // class template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
98 //
99 // FIXME: Will we eventually have to do the same for alias templates?
100 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
101 if (!ClassTemplates.insert(ClassTmpl)) {
102 filter.erase();
103 continue;
104 }
John McCall8ba66912010-08-13 07:02:08 +0000105
106 // FIXME: we promote access to public here as a workaround to
107 // the fact that LookupResult doesn't let us remember that we
108 // found this template through a particular injected class name,
109 // which means we end up doing nasty things to the invariants.
110 // Pretending that access is public is *much* safer.
111 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000112 }
John McCallf7a1a742009-11-24 19:00:30 +0000113 }
114 filter.done();
115}
116
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000117TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000118 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000119 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000120 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000121 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000122 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000123 TemplateTy &TemplateResult,
124 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000125 assert(getLangOptions().CPlusPlus && "No template names in C!");
126
Douglas Gregor014e88d2009-11-03 23:16:33 +0000127 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000128 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000129
Douglas Gregor014e88d2009-11-03 23:16:33 +0000130 switch (Name.getKind()) {
131 case UnqualifiedId::IK_Identifier:
132 TName = DeclarationName(Name.Identifier);
133 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 case UnqualifiedId::IK_OperatorFunctionId:
136 TName = Context.DeclarationNames.getCXXOperatorName(
137 Name.OperatorFunctionId.Operator);
138 break;
139
Sean Hunte6252d12009-11-28 08:58:14 +0000140 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000141 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
142 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000143
Douglas Gregor014e88d2009-11-03 23:16:33 +0000144 default:
145 return TNK_Non_template;
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
John McCallb3d87482010-08-24 05:47:05 +0000148 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000150 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000151 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000152 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
153 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000154 if (R.empty()) return TNK_Non_template;
155 if (R.isAmbiguous()) {
156 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000157 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000158
159 // FIXME: we might have ambiguous templates, in which case we
160 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000161 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000162 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000163
John McCall0bd6feb2009-12-02 08:04:21 +0000164 TemplateName Template;
165 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
John McCall0bd6feb2009-12-02 08:04:21 +0000167 unsigned ResultCount = R.end() - R.begin();
168 if (ResultCount > 1) {
169 // We assume that we'll preserve the qualifier from a function
170 // template name in other ways.
171 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
172 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000173
174 // We'll do this lookup again later.
175 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000177 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
178
179 if (SS.isSet() && !SS.isInvalid()) {
180 NestedNameSpecifier *Qualifier
181 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000182 Template = Context.getQualifiedTemplateName(Qualifier,
183 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000184 } else {
185 Template = TemplateName(TD);
186 }
187
John McCallb8592062010-08-13 02:23:42 +0000188 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000189 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000190
191 // We'll do this lookup again later.
192 R.suppressDiagnostics();
193 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000194 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
195 TemplateKind = TNK_Type_template;
196 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
John McCall0bd6feb2009-12-02 08:04:21 +0000199 TemplateResult = TemplateTy::make(Template);
200 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000201}
202
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000203bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000204 SourceLocation IILoc,
205 Scope *S,
206 const CXXScopeSpec *SS,
207 TemplateTy &SuggestedTemplate,
208 TemplateNameKind &SuggestedKind) {
209 // We can't recover unless there's a dependent scope specifier preceding the
210 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000211 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000212 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
213 computeDeclContext(*SS))
214 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000215
Douglas Gregor84d0a192010-01-12 21:28:44 +0000216 // The code is missing a 'template' keyword prior to the dependent template
217 // name.
218 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
219 Diag(IILoc, diag::err_template_kw_missing)
220 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000221 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
224 SuggestedKind = TNK_Dependent_template_name;
225 return true;
226}
227
John McCallf7a1a742009-11-24 19:00:30 +0000228void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000229 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000230 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000231 bool EnteringContext,
232 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000233 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000234 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000235 DeclContext *LookupCtx = 0;
236 bool isDependent = false;
237 if (!ObjectType.isNull()) {
238 // This nested-name-specifier occurs in a member access expression, e.g.,
239 // x->B::f, and we are looking into the type of the object.
240 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
241 LookupCtx = computeDeclContext(ObjectType);
242 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000243 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000244 "Caller should have completed object type");
245 } else if (SS.isSet()) {
246 // This nested-name-specifier occurs after another nested-name-specifier,
247 // so long into the context associated with the prior nested-name-specifier.
248 LookupCtx = computeDeclContext(SS, EnteringContext);
249 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250
John McCallf7a1a742009-11-24 19:00:30 +0000251 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000252 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000253 return;
254 }
255
256 bool ObjectTypeSearchedInScope = false;
257 if (LookupCtx) {
258 // Perform "qualified" name lookup into the declaration context we
259 // computed, which is either the type of the base of a member access
260 // expression or the declaration context associated with a prior
261 // nested-name-specifier.
262 LookupQualifiedName(Found, LookupCtx);
263
264 if (!ObjectType.isNull() && Found.empty()) {
265 // C++ [basic.lookup.classref]p1:
266 // In a class member access expression (5.2.5), if the . or -> token is
267 // immediately followed by an identifier followed by a <, the
268 // identifier must be looked up to determine whether the < is the
269 // beginning of a template argument list (14.2) or a less-than operator.
270 // The identifier is first looked up in the class of the object
271 // expression. If the identifier is not found, it is then looked up in
272 // the context of the entire postfix-expression and shall name a class
273 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000274 if (S) LookupName(Found, S);
275 ObjectTypeSearchedInScope = true;
276 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000277 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000278 // We cannot look into a dependent object type or nested nme
279 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000280 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000281 return;
282 } else {
283 // Perform unqualified name lookup in the current scope.
284 LookupName(Found, S);
285 }
286
Douglas Gregor2e933882010-01-12 17:06:20 +0000287 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000288 // If we did not find any names, attempt to correct any typos.
289 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000290 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000291 false, CTC_CXXCasts)) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000292 FilterAcceptableTemplateNames(Context, Found);
John McCallad00b772010-06-16 08:42:20 +0000293 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000294 if (LookupCtx)
295 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
296 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000297 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000298 Found.getLookupName().getAsString());
299 else
300 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
301 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000302 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000303 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000304 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
305 Diag(Template->getLocation(), diag::note_previous_decl)
306 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000307 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000308 } else {
309 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000310 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000311 }
312 }
313
John McCallf7a1a742009-11-24 19:00:30 +0000314 FilterAcceptableTemplateNames(Context, Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000315 if (Found.empty()) {
316 if (isDependent)
317 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000318 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000319 }
John McCallf7a1a742009-11-24 19:00:30 +0000320
321 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
322 // C++ [basic.lookup.classref]p1:
323 // [...] If the lookup in the class of the object expression finds a
324 // template, the name is also looked up in the context of the entire
325 // postfix-expression and [...]
326 //
327 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
328 LookupOrdinaryName);
329 LookupName(FoundOuter, S);
330 FilterAcceptableTemplateNames(Context, FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000331
John McCallf7a1a742009-11-24 19:00:30 +0000332 if (FoundOuter.empty()) {
333 // - if the name is not found, the name found in the class of the
334 // object expression is used, otherwise
335 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
336 // - if the name is found in the context of the entire
337 // postfix-expression and does not name a class template, the name
338 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000339 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000340 // - if the name found is a class template, it must refer to the same
341 // entity as the one found in the class of the object expression,
342 // otherwise the program is ill-formed.
343 if (!Found.isSingleResult() ||
344 Found.getFoundDecl()->getCanonicalDecl()
345 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000346 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000347 diag::ext_nested_name_member_ref_lookup_ambiguous)
348 << Found.getLookupName()
349 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000350 Diag(Found.getRepresentativeDecl()->getLocation(),
351 diag::note_ambig_member_ref_object_type)
352 << ObjectType;
353 Diag(FoundOuter.getFoundDecl()->getLocation(),
354 diag::note_ambig_member_ref_scope);
355
356 // Recover by taking the template that we found in the object
357 // expression's type.
358 }
359 }
360 }
361}
362
John McCall2f841ba2009-12-02 03:53:29 +0000363/// ActOnDependentIdExpression - Handle a dependent id-expression that
364/// was just parsed. This is only possible with an explicit scope
365/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000366ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000367Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000369 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *TemplateArgs) {
371 NestedNameSpecifier *Qualifier
372 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
John McCallea1471e2010-05-20 01:18:31 +0000373
374 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000375
John McCall2f841ba2009-12-02 03:53:29 +0000376 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000377 isa<CXXMethodDecl>(DC) &&
378 cast<CXXMethodDecl>(DC)->isInstance()) {
379 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000380
John McCallf7a1a742009-11-24 19:00:30 +0000381 // Since the 'this' expression is synthesized, we don't need to
382 // perform the double-lookup check.
383 NamedDecl *FirstQualifierInScope = 0;
384
John McCallaa81e162009-12-01 22:10:20 +0000385 return Owned(CXXDependentScopeMemberExpr::Create(Context,
386 /*This*/ 0, ThisType,
387 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000388 /*Op*/ SourceLocation(),
389 Qualifier, SS.getRange(),
390 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000391 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000392 TemplateArgs));
393 }
394
Abramo Bagnara25777432010-08-11 22:01:17 +0000395 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000396}
397
John McCall60d7b3a2010-08-24 06:29:42 +0000398ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000399Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000400 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000401 const TemplateArgumentListInfo *TemplateArgs) {
402 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000403 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000404 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000405 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000406}
407
Douglas Gregor72c3f312008-12-05 18:15:24 +0000408/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
409/// that the template parameter 'PrevDecl' is being shadowed by a new
410/// declaration at location Loc. Returns true to indicate that this is
411/// an error, and false otherwise.
412bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000413 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000414
415 // Microsoft Visual C++ permits template parameters to be shadowed.
416 if (getLangOptions().Microsoft)
417 return false;
418
419 // C++ [temp.local]p4:
420 // A template-parameter shall not be redeclared within its
421 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000422 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000423 << cast<NamedDecl>(PrevDecl)->getDeclName();
424 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
425 return true;
426}
427
Douglas Gregor2943aed2009-03-03 04:44:36 +0000428/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000429/// the parameter D to reference the templated declaration and return a pointer
430/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000431TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
432 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
433 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000434 return Temp;
435 }
436 return 0;
437}
438
Douglas Gregorba68eca2011-01-05 17:40:24 +0000439ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
440 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000441 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000442 "Only template template arguments can be pack expansions here");
443 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
444 "Template template argument pack expansion without packs");
445 ParsedTemplateArgument Result(*this);
446 Result.EllipsisLoc = EllipsisLoc;
447 return Result;
448}
449
Douglas Gregor788cd062009-11-11 01:00:40 +0000450static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
451 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452
Douglas Gregor788cd062009-11-11 01:00:40 +0000453 switch (Arg.getKind()) {
454 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000455 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000456 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000457 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000458 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000459 return TemplateArgumentLoc(TemplateArgument(T), DI);
460 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000461
Douglas Gregor788cd062009-11-11 01:00:40 +0000462 case ParsedTemplateArgument::NonType: {
463 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
464 return TemplateArgumentLoc(TemplateArgument(E), E);
465 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000466
Douglas Gregor788cd062009-11-11 01:00:40 +0000467 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000468 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000469 TemplateArgument TArg;
470 if (Arg.getEllipsisLoc().isValid())
471 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
472 else
473 TArg = Template;
474 return TemplateArgumentLoc(TArg,
Douglas Gregor788cd062009-11-11 01:00:40 +0000475 Arg.getScopeSpec().getRange(),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000476 Arg.getLocation(),
477 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000478 }
479 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000480
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000481 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000482 return TemplateArgumentLoc();
483}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000484
Douglas Gregor788cd062009-11-11 01:00:40 +0000485/// \brief Translates template arguments as provided by the parser
486/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000487void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
488 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000489 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000490 TemplateArgs.addArgument(translateTemplateArgument(*this,
491 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000492}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000493
Douglas Gregor72c3f312008-12-05 18:15:24 +0000494/// ActOnTypeParameter - Called when a C++ template type parameter
495/// (e.g., "typename T") has been parsed. Typename specifies whether
496/// the keyword "typename" was used to declare the type parameter
497/// (otherwise, "class" was used), and KeyLoc is the location of the
498/// "class" or "typename" keyword. ParamName is the name of the
499/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000500/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000501/// If the type parameter has a default argument, it will be added
502/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000503Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
504 SourceLocation EllipsisLoc,
505 SourceLocation KeyLoc,
506 IdentifierInfo *ParamName,
507 SourceLocation ParamNameLoc,
508 unsigned Depth, unsigned Position,
509 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000510 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000511 assert(S->isTemplateParamScope() &&
512 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000513 bool Invalid = false;
514
515 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000516 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000517 LookupOrdinaryName,
518 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000519 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000520 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000521 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000522 }
523
Douglas Gregorddc29e12009-02-06 22:42:48 +0000524 SourceLocation Loc = ParamNameLoc;
525 if (!ParamName)
526 Loc = KeyLoc;
527
Douglas Gregor72c3f312008-12-05 18:15:24 +0000528 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000529 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
530 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000531 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000532 if (Invalid)
533 Param->setInvalidDecl();
534
535 if (ParamName) {
536 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000537 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000538 IdResolver.AddDecl(Param);
539 }
540
Douglas Gregor61c4d282011-01-05 15:48:55 +0000541 // C++0x [temp.param]p9:
542 // A default template-argument may be specified for any kind of
543 // template-parameter that is not a template parameter pack.
544 if (DefaultArg && Ellipsis) {
545 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
546 DefaultArg = ParsedType();
547 }
548
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000549 // Handle the default argument, if provided.
550 if (DefaultArg) {
551 TypeSourceInfo *DefaultTInfo;
552 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000553
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000554 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000555
Douglas Gregor6f526752010-12-16 08:48:57 +0000556 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000557 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000558 UPPC_DefaultArgument))
559 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000560
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000561 // Check the template argument itself.
562 if (CheckTemplateArgument(Param, DefaultTInfo)) {
563 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000564 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000565 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000567 Param->setDefaultArgument(DefaultTInfo, false);
568 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000569
John McCalld226f652010-08-21 09:40:31 +0000570 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000571}
572
Douglas Gregor2943aed2009-03-03 04:44:36 +0000573/// \brief Check that the type of a non-type template parameter is
574/// well-formed.
575///
576/// \returns the (possibly-promoted) parameter type if valid;
577/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000578QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000579Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000580 // We don't allow variably-modified types as the type of non-type template
581 // parameters.
582 if (T->isVariablyModifiedType()) {
583 Diag(Loc, diag::err_variably_modified_nontype_template_param)
584 << T;
585 return QualType();
586 }
587
Douglas Gregor2943aed2009-03-03 04:44:36 +0000588 // C++ [temp.param]p4:
589 //
590 // A non-type template-parameter shall have one of the following
591 // (optionally cv-qualified) types:
592 //
593 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000594 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000595 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000596 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000597 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000598 T->isReferenceType() ||
599 // -- pointer to member.
600 T->isMemberPointerType() ||
601 // If T is a dependent type, we can't do the check now, so we
602 // assume that it is well-formed.
603 T->isDependentType())
604 return T;
605 // C++ [temp.param]p8:
606 //
607 // A non-type template-parameter of type "array of T" or
608 // "function returning T" is adjusted to be of type "pointer to
609 // T" or "pointer to function returning T", respectively.
610 else if (T->isArrayType())
611 // FIXME: Keep the type prior to promotion?
612 return Context.getArrayDecayedType(T);
613 else if (T->isFunctionType())
614 // FIXME: Keep the type prior to promotion?
615 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000616
Douglas Gregor2943aed2009-03-03 04:44:36 +0000617 Diag(Loc, diag::err_template_nontype_parm_bad_type)
618 << T;
619
620 return QualType();
621}
622
John McCalld226f652010-08-21 09:40:31 +0000623Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
624 unsigned Depth,
625 unsigned Position,
626 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000627 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000628 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
629 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000630
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000631 assert(S->isTemplateParamScope() &&
632 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000633 bool Invalid = false;
634
635 IdentifierInfo *ParamName = D.getIdentifier();
636 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000637 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000638 LookupOrdinaryName,
639 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000640 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000641 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000642 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000643 }
644
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000645 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
646 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000647 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000648 Invalid = true;
649 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000650
Douglas Gregor10738d32010-12-23 23:51:58 +0000651 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000652 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000653 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
654 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000655 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000656 IsParameterPack, TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000657 if (Invalid)
658 Param->setInvalidDecl();
659
660 if (D.getIdentifier()) {
661 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000662 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000663 IdResolver.AddDecl(Param);
664 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000665
Douglas Gregor61c4d282011-01-05 15:48:55 +0000666 // C++0x [temp.param]p9:
667 // A default template-argument may be specified for any kind of
668 // template-parameter that is not a template parameter pack.
669 if (Default && IsParameterPack) {
670 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
671 Default = 0;
672 }
673
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000674 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000675 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000676 // Check for unexpanded parameter packs.
677 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
678 return Param;
679
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000680 TemplateArgument Converted;
681 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
682 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000683 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000684 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000685
John McCall9ae2f072010-08-23 23:25:46 +0000686 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000687 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000688
John McCalld226f652010-08-21 09:40:31 +0000689 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000690}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000691
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000692/// ActOnTemplateTemplateParameter - Called when a C++ template template
693/// parameter (e.g. T in template <template <typename> class T> class array)
694/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000695Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
696 SourceLocation TmpLoc,
697 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000698 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000699 IdentifierInfo *Name,
700 SourceLocation NameLoc,
701 unsigned Depth,
702 unsigned Position,
703 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000704 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000705 assert(S->isTemplateParamScope() &&
706 "Template template parameter not in template parameter scope!");
707
708 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000709 bool IsParameterPack = EllipsisLoc.isValid();
710 // FIXME: Pack-ness is dropped
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000711 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000712 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000713 NameLoc.isInvalid()? TmpLoc : NameLoc,
714 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000715 Name, Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000716
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000717 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000718 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000719 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000720 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000721 IdResolver.AddDecl(Param);
722 }
723
Douglas Gregor6f526752010-12-16 08:48:57 +0000724 if (Params->size() == 0) {
725 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
726 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
727 Param->setInvalidDecl();
728 }
729
Douglas Gregor61c4d282011-01-05 15:48:55 +0000730 // C++0x [temp.param]p9:
731 // A default template-argument may be specified for any kind of
732 // template-parameter that is not a template parameter pack.
733 if (IsParameterPack && !Default.isInvalid()) {
734 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
735 Default = ParsedTemplateArgument();
736 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000737
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000738 if (!Default.isInvalid()) {
739 // Check only that we have a template template argument. We don't want to
740 // try to check well-formedness now, because our template template parameter
741 // might have dependent types in its template parameters, which we wouldn't
742 // be able to match now.
743 //
744 // If none of the template template parameter's template arguments mention
745 // other template parameters, we could actually perform more checking here.
746 // However, it isn't worth doing.
747 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
748 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
749 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
750 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000751 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000752 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000753
Douglas Gregor6f526752010-12-16 08:48:57 +0000754 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000755 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000756 DefaultArg.getArgument().getAsTemplate(),
757 UPPC_DefaultArgument))
758 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000759
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000760 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000761 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000762
John McCalld226f652010-08-21 09:40:31 +0000763 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000764}
765
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000766/// ActOnTemplateParameterList - Builds a TemplateParameterList that
767/// contains the template parameters in Params/NumParams.
768Sema::TemplateParamsTy *
769Sema::ActOnTemplateParameterList(unsigned Depth,
770 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000771 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000772 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000773 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000774 SourceLocation RAngleLoc) {
775 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000776 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000777
Douglas Gregorddc29e12009-02-06 22:42:48 +0000778 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000779 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000780 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000781}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000782
John McCallb6217662010-03-15 10:12:16 +0000783static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
784 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000785 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000786}
787
John McCallf312b1e2010-08-26 23:41:50 +0000788DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000789Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000790 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000791 IdentifierInfo *Name, SourceLocation NameLoc,
792 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000793 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000794 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000795 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000796 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000797 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000798 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000799
800 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000801 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000802 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000803
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000804 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
805 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000806
807 // There is no such thing as an unnamed class template.
808 if (!Name) {
809 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000810 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000811 }
812
813 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000814 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000815 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000816 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000817 if (SS.isNotEmpty() && !SS.isInvalid()) {
818 SemanticContext = computeDeclContext(SS, true);
819 if (!SemanticContext) {
820 // FIXME: Produce a reasonable diagnostic here
821 return true;
822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
John McCall77bb1aa2010-05-01 00:40:08 +0000824 if (RequireCompleteDeclContext(SS, SemanticContext))
825 return true;
826
John McCalla24dc2e2009-11-17 02:14:36 +0000827 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000828 } else {
829 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000830 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000831 }
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Douglas Gregor57265e32010-04-12 16:00:01 +0000833 if (Previous.isAmbiguous())
834 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000835
Douglas Gregorddc29e12009-02-06 22:42:48 +0000836 NamedDecl *PrevDecl = 0;
837 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000838 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000839
Douglas Gregorddc29e12009-02-06 22:42:48 +0000840 // If there is a previous declaration with the same name, check
841 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000842 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000843 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000844
845 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000846 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000847 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000848 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000849 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
850 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000851 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000852 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
853 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
854 PrevClassTemplate
855 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
856 ->getSpecializedTemplate();
857 }
858 }
859
John McCall65c49462009-12-18 11:25:59 +0000860 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000861 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000862 // [...] When looking for a prior declaration of a class or a function
863 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000864 // function is neither a qualified name nor a template-id, scopes outside
865 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000866 if (!SS.isSet()) {
867 DeclContext *OutermostContext = CurContext;
868 while (!OutermostContext->isFileContext())
869 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000870
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000871 if (PrevDecl &&
872 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
873 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
874 SemanticContext = PrevDecl->getDeclContext();
875 } else {
876 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000877 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000878 // declaration.
879 PrevDecl = PrevClassTemplate = 0;
880 SemanticContext = OutermostContext;
881 }
John McCalle129d442009-12-17 23:21:11 +0000882 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000883
John McCalle129d442009-12-17 23:21:11 +0000884 if (CurContext->isDependentContext()) {
885 // If this is a dependent context, we don't want to link the friend
886 // class template to the template in scope, because that would perform
887 // checking of the template parameter lists that can't be performed
888 // until the outer context is instantiated.
889 PrevDecl = PrevClassTemplate = 0;
890 }
891 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
892 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000893
Douglas Gregorddc29e12009-02-06 22:42:48 +0000894 if (PrevClassTemplate) {
895 // Ensure that the template parameter lists are compatible.
896 if (!TemplateParameterListsAreEqual(TemplateParams,
897 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000898 /*Complain=*/true,
899 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000900 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000901
902 // C++ [temp.class]p4:
903 // In a redeclaration, partial specialization, explicit
904 // specialization or explicit instantiation of a class template,
905 // the class-key shall agree in kind with the original class
906 // template declaration (7.1.5.3).
907 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000908 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000909 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000910 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000911 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000912 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000913 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914 }
915
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000917 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000918 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000919 Diag(NameLoc, diag::err_redefinition) << Name;
920 Diag(Def->getLocation(), diag::note_previous_definition);
921 // FIXME: Would it make sense to try to "forget" the previous
922 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000923 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000924 }
925 }
926 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
927 // Maybe we will complain about the shadowed template parameter.
928 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
929 // Just pretend that we didn't see the previous declaration.
930 PrevDecl = 0;
931 } else if (PrevDecl) {
932 // C++ [temp]p5:
933 // A class template shall not have the same name as any other
934 // template, class, function, object, enumeration, enumerator,
935 // namespace, or type in the same scope (3.3), except as specified
936 // in (14.5.4).
937 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
938 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000939 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000940 }
941
Douglas Gregord684b002009-02-10 19:49:53 +0000942 // Check the template parameter list of this declaration, possibly
943 // merging in the template parameter list from the previous class
944 // template declaration.
945 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000946 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000947 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000948 SemanticContext->isRecord() &&
949 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000950 ? TPC_ClassTemplateMember
951 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000952 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Douglas Gregor57265e32010-04-12 16:00:01 +0000954 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000955 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000956 // template out-of-line.
957 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
958 !(TUK == TUK_Friend && CurContext->isDependentContext()))
959 Diag(NameLoc, diag::err_member_def_does_not_match)
960 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000961 }
962
Mike Stump1eb44332009-09-09 15:08:12 +0000963 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000964 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000965 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000966 PrevClassTemplate->getTemplatedDecl() : 0,
967 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000968 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000969
970 ClassTemplateDecl *NewTemplate
971 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
972 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000973 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000974 NewClass->setDescribedClassTemplate(NewTemplate);
975
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000976 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000977 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000978 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000979 assert(T->isDependentType() && "Class template type is not dependent?");
980 (void)T;
981
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000982 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000983 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000984 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000985 PrevClassTemplate->getInstantiatedFromMemberTemplate())
986 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000987
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000988 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000989 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000990 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregorddc29e12009-02-06 22:42:48 +0000992 // Set the lexical context of these templates
993 NewClass->setLexicalDeclContext(CurContext);
994 NewTemplate->setLexicalDeclContext(CurContext);
995
John McCall0f434ec2009-07-31 02:45:11 +0000996 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000997 NewClass->startDefinition();
998
999 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001000 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001001
John McCall05b23ea2009-09-14 21:59:20 +00001002 if (TUK != TUK_Friend)
1003 PushOnScopeChains(NewTemplate, S);
1004 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001005 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001006 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001007 NewClass->setAccess(PrevClassTemplate->getAccess());
1008 }
John McCall05b23ea2009-09-14 21:59:20 +00001009
Douglas Gregord85bea22009-09-26 06:47:28 +00001010 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1011 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001012
John McCall05b23ea2009-09-14 21:59:20 +00001013 // Friend templates are visible in fairly strange ways.
1014 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001015 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001016 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1017 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1018 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001020 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001021
Douglas Gregord85bea22009-09-26 06:47:28 +00001022 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1023 NewClass->getLocation(),
1024 NewTemplate,
1025 /*FIXME:*/NewClass->getLocation());
1026 Friend->setAccess(AS_public);
1027 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001028 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001029
Douglas Gregord684b002009-02-10 19:49:53 +00001030 if (Invalid) {
1031 NewTemplate->setInvalidDecl();
1032 NewClass->setInvalidDecl();
1033 }
John McCalld226f652010-08-21 09:40:31 +00001034 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001035}
1036
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001037/// \brief Diagnose the presence of a default template argument on a
1038/// template parameter, which is ill-formed in certain contexts.
1039///
1040/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001041static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001042 Sema::TemplateParamListContext TPC,
1043 SourceLocation ParamLoc,
1044 SourceRange DefArgRange) {
1045 switch (TPC) {
1046 case Sema::TPC_ClassTemplate:
1047 return false;
1048
1049 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001050 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001051 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001052 // A default template-argument shall not be specified in a
1053 // function template declaration or a function template
1054 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001055 // If a friend function template declaration specifies a default
1056 // template-argument, that declaration shall be a definition and shall be
1057 // the only declaration of the function template in the translation unit.
1058 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001059 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001060 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001061 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001062 << DefArgRange;
1063 return false;
1064
1065 case Sema::TPC_ClassTemplateMember:
1066 // C++0x [temp.param]p9:
1067 // A default template-argument shall not be specified in the
1068 // template-parameter-lists of the definition of a member of a
1069 // class template that appears outside of the member's class.
1070 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1071 << DefArgRange;
1072 return true;
1073
1074 case Sema::TPC_FriendFunctionTemplate:
1075 // C++ [temp.param]p9:
1076 // A default template-argument shall not be specified in a
1077 // friend template declaration.
1078 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1079 << DefArgRange;
1080 return true;
1081
1082 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1083 // for friend function templates if there is only a single
1084 // declaration (and it is a definition). Strange!
1085 }
1086
1087 return false;
1088}
1089
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001090/// \brief Check for unexpanded parameter packs within the template parameters
1091/// of a template template parameter, recursively.
1092bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1093 TemplateParameterList *Params = TTP->getTemplateParameters();
1094 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1095 NamedDecl *P = Params->getParam(I);
1096 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001097 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001098 NTTP->getTypeSourceInfo(),
1099 Sema::UPPC_NonTypeTemplateParameterType))
1100 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001101
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001102 continue;
1103 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001104
1105 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001106 = dyn_cast<TemplateTemplateParmDecl>(P))
1107 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1108 return true;
1109 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001110
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001111 return false;
1112}
1113
Douglas Gregord684b002009-02-10 19:49:53 +00001114/// \brief Checks the validity of a template parameter list, possibly
1115/// considering the template parameter list from a previous
1116/// declaration.
1117///
1118/// If an "old" template parameter list is provided, it must be
1119/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1120/// template parameter list.
1121///
1122/// \param NewParams Template parameter list for a new template
1123/// declaration. This template parameter list will be updated with any
1124/// default arguments that are carried through from the previous
1125/// template parameter list.
1126///
1127/// \param OldParams If provided, template parameter list from a
1128/// previous declaration of the same template. Default template
1129/// arguments will be merged from the old template parameter list to
1130/// the new template parameter list.
1131///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001132/// \param TPC Describes the context in which we are checking the given
1133/// template parameter list.
1134///
Douglas Gregord684b002009-02-10 19:49:53 +00001135/// \returns true if an error occurred, false otherwise.
1136bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001137 TemplateParameterList *OldParams,
1138 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001139 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Douglas Gregord684b002009-02-10 19:49:53 +00001141 // C++ [temp.param]p10:
1142 // The set of default template-arguments available for use with a
1143 // template declaration or definition is obtained by merging the
1144 // default arguments from the definition (if in scope) and all
1145 // declarations in scope in the same way default function
1146 // arguments are (8.3.6).
1147 bool SawDefaultArgument = false;
1148 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001149
Anders Carlsson49d25572009-06-12 23:20:15 +00001150 bool SawParameterPack = false;
1151 SourceLocation ParameterPackLoc;
1152
Mike Stump1a35fde2009-02-11 23:03:27 +00001153 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001154 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001155 if (OldParams)
1156 OldParam = OldParams->begin();
1157
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001158 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001159 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1160 NewParamEnd = NewParams->end();
1161 NewParam != NewParamEnd; ++NewParam) {
1162 // Variables used to diagnose redundant default arguments
1163 bool RedundantDefaultArg = false;
1164 SourceLocation OldDefaultLoc;
1165 SourceLocation NewDefaultLoc;
1166
1167 // Variables used to diagnose missing default arguments
1168 bool MissingDefaultArg = false;
1169
Anders Carlsson49d25572009-06-12 23:20:15 +00001170 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001171 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001172 // parameter pack, it shall be the last template parameter.
1173 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001174 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001175 diag::err_template_param_pack_must_be_last_template_parameter);
1176 Invalid = true;
1177 }
1178
Douglas Gregord684b002009-02-10 19:49:53 +00001179 if (TemplateTypeParmDecl *NewTypeParm
1180 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001181 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001182 if (NewTypeParm->hasDefaultArgument() &&
1183 DiagnoseDefaultTemplateArgument(*this, TPC,
1184 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001185 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001186 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001187 NewTypeParm->removeDefaultArgument();
1188
1189 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001190 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001191 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Anders Carlsson49d25572009-06-12 23:20:15 +00001193 if (NewTypeParm->isParameterPack()) {
1194 assert(!NewTypeParm->hasDefaultArgument() &&
1195 "Parameter packs can't have a default argument!");
1196 SawParameterPack = true;
1197 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001198 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001199 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001200 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1201 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1202 SawDefaultArgument = true;
1203 RedundantDefaultArg = true;
1204 PreviousDefaultArgLoc = NewDefaultLoc;
1205 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1206 // Merge the default argument from the old declaration to the
1207 // new declaration.
1208 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001209 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001210 true);
1211 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1212 } else if (NewTypeParm->hasDefaultArgument()) {
1213 SawDefaultArgument = true;
1214 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1215 } else if (SawDefaultArgument)
1216 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001217 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001218 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001219 // Check for unexpanded parameter packs.
1220 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001221 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001222 UPPC_NonTypeTemplateParameterType)) {
1223 Invalid = true;
1224 continue;
1225 }
1226
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001227 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001228 if (NewNonTypeParm->hasDefaultArgument() &&
1229 DiagnoseDefaultTemplateArgument(*this, TPC,
1230 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001231 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001232 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001233 }
1234
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001235 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001236 NonTypeTemplateParmDecl *OldNonTypeParm
1237 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001238 if (NewNonTypeParm->isParameterPack()) {
1239 assert(!NewNonTypeParm->hasDefaultArgument() &&
1240 "Parameter packs can't have a default argument!");
1241 SawParameterPack = true;
1242 ParameterPackLoc = NewNonTypeParm->getLocation();
1243 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001244 NewNonTypeParm->hasDefaultArgument()) {
1245 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1246 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1247 SawDefaultArgument = true;
1248 RedundantDefaultArg = true;
1249 PreviousDefaultArgLoc = NewDefaultLoc;
1250 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1251 // Merge the default argument from the old declaration to the
1252 // new declaration.
1253 SawDefaultArgument = true;
1254 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001255 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001256 // parameter.
1257 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001258 OldNonTypeParm->getDefaultArgument(),
1259 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001260 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1261 } else if (NewNonTypeParm->hasDefaultArgument()) {
1262 SawDefaultArgument = true;
1263 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1264 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001265 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001266 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001267 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001268 TemplateTemplateParmDecl *NewTemplateParm
1269 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001270
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001271 // Check for unexpanded parameter packs, recursively.
1272 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1273 Invalid = true;
1274 continue;
1275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001276
1277 if (NewTemplateParm->hasDefaultArgument() &&
1278 DiagnoseDefaultTemplateArgument(*this, TPC,
1279 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001280 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001281 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001282
1283 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001284 TemplateTemplateParmDecl *OldTemplateParm
1285 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001286 if (NewTemplateParm->isParameterPack()) {
1287 assert(!NewTemplateParm->hasDefaultArgument() &&
1288 "Parameter packs can't have a default argument!");
1289 SawParameterPack = true;
1290 ParameterPackLoc = NewTemplateParm->getLocation();
1291 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001292 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001293 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1294 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001295 SawDefaultArgument = true;
1296 RedundantDefaultArg = true;
1297 PreviousDefaultArgLoc = NewDefaultLoc;
1298 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1299 // Merge the default argument from the old declaration to the
1300 // new declaration.
1301 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001302 // FIXME: We need to create a new kind of "default argument" expression
1303 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001304 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001305 OldTemplateParm->getDefaultArgument(),
1306 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001307 PreviousDefaultArgLoc
1308 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001309 } else if (NewTemplateParm->hasDefaultArgument()) {
1310 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001311 PreviousDefaultArgLoc
1312 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001313 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001314 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001315 }
1316
1317 if (RedundantDefaultArg) {
1318 // C++ [temp.param]p12:
1319 // A template-parameter shall not be given default arguments
1320 // by two different declarations in the same scope.
1321 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1322 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1323 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001324 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001325 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001326 // If a template-parameter of a class template has a default
1327 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001328 // have a default template-argument supplied or be a template parameter
1329 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001330 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001331 diag::err_template_param_default_arg_missing);
1332 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1333 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001334 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001335 }
1336
1337 // If we have an old template parameter list that we're merging
1338 // in, move on to the next parameter.
1339 if (OldParams)
1340 ++OldParam;
1341 }
1342
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001343 // We were missing some default arguments at the end of the list, so remove
1344 // all of the default arguments.
1345 if (RemoveDefaultArguments) {
1346 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1347 NewParamEnd = NewParams->end();
1348 NewParam != NewParamEnd; ++NewParam) {
1349 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1350 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001351 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001352 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1353 NTTP->removeDefaultArgument();
1354 else
1355 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1356 }
1357 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001358
Douglas Gregord684b002009-02-10 19:49:53 +00001359 return Invalid;
1360}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001361
John McCall4e2cbb22010-10-20 05:44:58 +00001362namespace {
1363
1364/// A class which looks for a use of a certain level of template
1365/// parameter.
1366struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1367 typedef RecursiveASTVisitor<DependencyChecker> super;
1368
1369 unsigned Depth;
1370 bool Match;
1371
1372 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1373 NamedDecl *ND = Params->getParam(0);
1374 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1375 Depth = PD->getDepth();
1376 } else if (NonTypeTemplateParmDecl *PD =
1377 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1378 Depth = PD->getDepth();
1379 } else {
1380 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1381 }
1382 }
1383
1384 bool Matches(unsigned ParmDepth) {
1385 if (ParmDepth >= Depth) {
1386 Match = true;
1387 return true;
1388 }
1389 return false;
1390 }
1391
1392 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1393 return !Matches(T->getDepth());
1394 }
1395
1396 bool TraverseTemplateName(TemplateName N) {
1397 if (TemplateTemplateParmDecl *PD =
1398 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1399 if (Matches(PD->getDepth())) return false;
1400 return super::TraverseTemplateName(N);
1401 }
1402
1403 bool VisitDeclRefExpr(DeclRefExpr *E) {
1404 if (NonTypeTemplateParmDecl *PD =
1405 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1406 if (PD->getDepth() == Depth) {
1407 Match = true;
1408 return false;
1409 }
1410 }
1411 return super::VisitDeclRefExpr(E);
1412 }
1413};
1414}
1415
1416/// Determines whether a template-id depends on the given parameter
1417/// list.
1418static bool
1419DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1420 TemplateParameterList *Params) {
1421 DependencyChecker Checker(Params);
1422 Checker.TraverseType(QualType(TemplateId, 0));
1423 return Checker.Match;
1424}
1425
Mike Stump1eb44332009-09-09 15:08:12 +00001426/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001427/// specifier, returning the template parameter list that applies to the
1428/// name.
1429///
1430/// \param DeclStartLoc the start of the declaration that has a scope
1431/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001432///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001433/// \param SS the scope specifier that will be matched to the given template
1434/// parameter lists. This scope specifier precedes a qualified name that is
1435/// being declared.
1436///
1437/// \param ParamLists the template parameter lists, from the outermost to the
1438/// innermost template parameter lists.
1439///
1440/// \param NumParamLists the number of template parameter lists in ParamLists.
1441///
John McCall77e8b112010-04-13 20:37:33 +00001442/// \param IsFriend Whether to apply the slightly different rules for
1443/// matching template parameters to scope specifiers in friend
1444/// declarations.
1445///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001446/// \param IsExplicitSpecialization will be set true if the entity being
1447/// declared is an explicit specialization, false otherwise.
1448///
Mike Stump1eb44332009-09-09 15:08:12 +00001449/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001450/// name that is preceded by the scope specifier @p SS. This template
1451/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001452/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001453/// template specialization), or may be NULL (if we were's declaring isn't
1454/// itself a template).
1455TemplateParameterList *
1456Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1457 const CXXScopeSpec &SS,
1458 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001459 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001460 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001461 bool &IsExplicitSpecialization,
1462 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001463 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001464
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001465 // Find the template-ids that occur within the nested-name-specifier. These
1466 // template-ids will match up with the template parameter lists.
1467 llvm::SmallVector<const TemplateSpecializationType *, 4>
1468 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001469 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1470 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001471 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1472 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001473 const Type *T = NNS->getAsType();
1474 if (!T) break;
1475
1476 // C++0x [temp.expl.spec]p17:
1477 // A member or a member template may be nested within many
1478 // enclosing class templates. In an explicit specialization for
1479 // such a member, the member declaration shall be preceded by a
1480 // template<> for each enclosing class template that is
1481 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001482 //
1483 // Following the existing practice of GNU and EDG, we allow a typedef of a
1484 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001485 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1486 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001487
Mike Stump1eb44332009-09-09 15:08:12 +00001488 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001489 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001490 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1491 if (!Template)
1492 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenek6217b802009-07-29 21:53:49 +00001494 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001495 ClassTemplateSpecializationDecl *SpecDecl
1496 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1497 // If the nested name specifier refers to an explicit specialization,
1498 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001499 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1500 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001501 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001502 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503 }
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001505 TemplateIdsInSpecifier.push_back(SpecType);
1506 }
1507 }
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001509 // Reverse the list of template-ids in the scope specifier, so that we can
1510 // more easily match up the template-ids and the template parameter lists.
1511 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001513 SourceLocation FirstTemplateLoc = DeclStartLoc;
1514 if (NumParamLists)
1515 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001517 // Match the template-ids found in the specifier to the template parameter
1518 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001519 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001520 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001521 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1522 const TemplateSpecializationType *TemplateId
1523 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001524 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001525
1526 // In friend declarations we can have template-ids which don't
1527 // depend on the corresponding template parameter lists. But
1528 // assume that empty parameter lists are supposed to match this
1529 // template-id.
1530 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1531 if (!DependentTemplateId ||
1532 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1533 continue;
1534 }
1535
1536 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001537 // We have a template-id without a corresponding template parameter
1538 // list.
John McCall77e8b112010-04-13 20:37:33 +00001539
1540 // ...which is fine if this is a friend declaration.
1541 if (IsFriend) {
1542 IsExplicitSpecialization = true;
1543 break;
1544 }
1545
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001546 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001547 // FIXME: the location information here isn't great.
1548 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001549 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001550 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001551 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001552 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001553 } else {
1554 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1555 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001556 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001557 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001558 }
1559 return 0;
1560 }
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001562 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001563 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001564 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001565
John McCall31f17ec2010-04-27 00:57:59 +00001566 // Are there cases in (e.g.) friends where this won't match?
1567 if (const InjectedClassNameType *Injected
1568 = TemplateId->getAs<InjectedClassNameType>()) {
1569 CXXRecordDecl *Record = Injected->getDecl();
1570 if (ClassTemplatePartialSpecializationDecl *Partial =
1571 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1572 ExpectedTemplateParams = Partial->getTemplateParameters();
1573 else
1574 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1575 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001576 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001577
John McCall31f17ec2010-04-27 00:57:59 +00001578 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001579 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001580 ExpectedTemplateParams,
1581 true, TPL_TemplateMatch);
1582
John McCall4e2cbb22010-10-20 05:44:58 +00001583 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1584 TPC_ClassTemplateMember);
1585 } else if (ParamLists[ParamIdx]->size() > 0)
1586 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001587 diag::err_template_param_list_matches_nontemplate)
1588 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001589 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001590 else
1591 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001592
1593 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001594 }
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001596 // If there were at least as many template-ids as there were template
1597 // parameter lists, then there are no template parameter lists remaining for
1598 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001599 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001600 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001602 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001603 if (ParamIdx != NumParamLists - 1) {
1604 while (ParamIdx < NumParamLists - 1) {
1605 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1606 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001607 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1608 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001609 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1610 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001611
1612 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1613 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1614 diag::note_explicit_template_spec_does_not_need_header)
1615 << ExplicitSpecializationsInSpecifier.back();
1616 ExplicitSpecializationsInSpecifier.pop_back();
1617 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001618
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001619 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001620 // means that the resulting template declaration can't be instantiated
1621 // properly (we'll end up with dependent nodes when we shouldn't).
1622 if (!isExplicitSpecHeader)
1623 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001624
John McCall4e2cbb22010-10-20 05:44:58 +00001625 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001626 }
1627 }
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001629 // Return the last template parameter list, which corresponds to the
1630 // entity being declared.
1631 return ParamLists[NumParamLists - 1];
1632}
1633
Douglas Gregor7532dc62009-03-30 22:58:21 +00001634QualType Sema::CheckTemplateIdType(TemplateName Name,
1635 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00001636 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001637 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001638 if (!Template) {
1639 // The template name does not resolve to a template, so we just
1640 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001641 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001642 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001643
Douglas Gregor40808ce2009-03-09 23:48:35 +00001644 // Check that the template argument list is well-formed for this
1645 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001646 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001647 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001648 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001649 return QualType();
1650
Douglas Gregor910f8002010-11-07 23:05:16 +00001651 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001652 "Converted template argument list is too short!");
1653
1654 QualType CanonType;
1655
Douglas Gregorcaddba02009-11-12 18:38:13 +00001656 if (Name.isDependent() ||
1657 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001658 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001659 // This class template specialization is a dependent
1660 // type. Therefore, its canonical type is another class template
1661 // specialization type that contains all of the converted
1662 // arguments in canonical form. This ensures that, e.g., A<T> and
1663 // A<T, T> have identical types when A is declared as:
1664 //
1665 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001666 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001667 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001668 Converted.data(),
1669 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregor1275ae02009-07-28 23:00:59 +00001671 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001672 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001673 // In the future, we need to teach getTemplateSpecializationType to only
1674 // build the canonical type and return that to us.
1675 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001676
1677 // This might work out to be a current instantiation, in which
1678 // case the canonical type needs to be the InjectedClassNameType.
1679 //
1680 // TODO: in theory this could be a simple hashtable lookup; most
1681 // changes to CurContext don't change the set of current
1682 // instantiations.
1683 if (isa<ClassTemplateDecl>(Template)) {
1684 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1685 // If we get out to a namespace, we're done.
1686 if (Ctx->isFileContext()) break;
1687
1688 // If this isn't a record, keep looking.
1689 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1690 if (!Record) continue;
1691
1692 // Look for one of the two cases with InjectedClassNameTypes
1693 // and check whether it's the same template.
1694 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1695 !Record->getDescribedClassTemplate())
1696 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001697
John McCall31f17ec2010-04-27 00:57:59 +00001698 // Fetch the injected class name type and check whether its
1699 // injected type is equal to the type we just built.
1700 QualType ICNT = Context.getTypeDeclType(Record);
1701 QualType Injected = cast<InjectedClassNameType>(ICNT)
1702 ->getInjectedSpecializationType();
1703
1704 if (CanonType != Injected->getCanonicalTypeInternal())
1705 continue;
1706
1707 // If so, the canonical type of this TST is the injected
1708 // class name type of the record we just found.
1709 assert(ICNT.isCanonical());
1710 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001711 break;
1712 }
1713 }
Mike Stump1eb44332009-09-09 15:08:12 +00001714 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001715 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001716 // Find the class template specialization declaration that
1717 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001718 void *InsertPos = 0;
1719 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001720 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001721 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001722 if (!Decl) {
1723 // This is the first time we have referenced this class template
1724 // specialization. Create the canonical declaration and add it to
1725 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001726 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001727 ClassTemplate->getTemplatedDecl()->getTagKind(),
1728 ClassTemplate->getDeclContext(),
1729 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001730 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001731 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001732 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001733 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001734 Decl->setLexicalDeclContext(CurContext);
1735 }
1736
1737 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001738 assert(isa<RecordType>(CanonType) &&
1739 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001740 }
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Douglas Gregor40808ce2009-03-09 23:48:35 +00001742 // Build the fully-sugared type for this class template
1743 // specialization, which refers back to the class template
1744 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001745 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001746}
1747
John McCallf312b1e2010-08-26 23:41:50 +00001748TypeResult
Douglas Gregor7532dc62009-03-30 22:58:21 +00001749Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001750 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001751 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001752 SourceLocation RAngleLoc) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001753 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001754
Douglas Gregor40808ce2009-03-09 23:48:35 +00001755 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001756 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001757 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001758
John McCalld5532b62009-11-23 01:53:49 +00001759 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001760 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001761
1762 if (Result.isNull())
1763 return true;
1764
John McCalla93c9342009-12-07 02:54:59 +00001765 TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result);
John McCall833ca992009-10-29 08:12:44 +00001766 TemplateSpecializationTypeLoc TL
1767 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1768 TL.setTemplateNameLoc(TemplateLoc);
1769 TL.setLAngleLoc(LAngleLoc);
1770 TL.setRAngleLoc(RAngleLoc);
1771 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1772 TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
1773
John McCallb3d87482010-08-24 05:47:05 +00001774 return CreateParsedType(Result, DI);
John McCall6b2becf2009-09-08 17:47:29 +00001775}
John McCallf1bbbb42009-09-04 01:14:41 +00001776
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001777TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS,
1778 TypeResult TypeResult,
John McCallf312b1e2010-08-26 23:41:50 +00001779 TagUseKind TUK,
1780 TypeSpecifierType TagSpec,
1781 SourceLocation TagLoc) {
John McCall6b2becf2009-09-08 17:47:29 +00001782 if (TypeResult.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001783 return ::TypeResult();
John McCallf1bbbb42009-09-04 01:14:41 +00001784
John McCalla93c9342009-12-07 02:54:59 +00001785 TypeSourceInfo *DI;
John McCall833ca992009-10-29 08:12:44 +00001786 QualType Type = GetTypeFromParser(TypeResult.get(), &DI);
John McCallf1bbbb42009-09-04 01:14:41 +00001787
John McCall6b2becf2009-09-08 17:47:29 +00001788 // Verify the tag specifier.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001789 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001790
John McCall6b2becf2009-09-08 17:47:29 +00001791 if (const RecordType *RT = Type->getAs<RecordType>()) {
1792 RecordDecl *D = RT->getDecl();
1793
1794 IdentifierInfo *Id = D->getIdentifier();
1795 assert(Id && "templated class must have an identifier");
1796
1797 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1798 Diag(TagLoc, diag::err_use_with_wrong_tag)
John McCallc4e70192009-09-11 04:59:25 +00001799 << Type
Douglas Gregor849b2432010-03-31 17:46:05 +00001800 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001801 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001802 }
1803 }
1804
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001805 ElaboratedTypeKeyword Keyword
1806 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
1807 QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type);
John McCall6b2becf2009-09-08 17:47:29 +00001808
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001809 TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType);
1810 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc());
1811 TL.setKeywordLoc(TagLoc);
1812 TL.setQualifierRange(SS.getRange());
1813 TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc());
1814 return CreateParsedType(ElabType, ElabDI);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001815}
1816
John McCall60d7b3a2010-08-24 06:29:42 +00001817ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +00001818 LookupResult &R,
1819 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001820 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001821 // FIXME: Can we do any checking at this point? I guess we could check the
1822 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001823 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001824 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001825 // foo<int> could identify a single function unambiguously
1826 // This approach does NOT work, since f<int>(1);
1827 // gets resolved prior to resorting to overload resolution
1828 // i.e., template<class T> void f(double);
1829 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001830
1831 // These should be filtered out by our callers.
1832 assert(!R.empty() && "empty lookup results when building templateid");
1833 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1834
1835 NestedNameSpecifier *Qualifier = 0;
1836 SourceRange QualifierRange;
1837 if (SS.isSet()) {
1838 Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
1839 QualifierRange = SS.getRange();
Douglas Gregora9e29aa2009-10-22 07:19:14 +00001840 }
John McCallc373d482010-01-27 01:50:18 +00001841
1842 // We don't want lookup warnings at this point.
1843 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001844
John McCallf7a1a742009-11-24 19:00:30 +00001845 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001846 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
John McCallf7a1a742009-11-24 19:00:30 +00001847 Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +00001848 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001849 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001850 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001851
1852 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001853}
1854
John McCallf7a1a742009-11-24 19:00:30 +00001855// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001856ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001857Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001858 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001859 const TemplateArgumentListInfo &TemplateArgs) {
1860 DeclContext *DC;
1861 if (!(DC = computeDeclContext(SS, false)) ||
1862 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001863 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001864 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001866 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001867 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001868 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1869 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001870
John McCallf7a1a742009-11-24 19:00:30 +00001871 if (R.isAmbiguous())
1872 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001873
John McCallf7a1a742009-11-24 19:00:30 +00001874 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001875 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1876 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001877 return ExprError();
1878 }
1879
1880 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001881 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1882 << (NestedNameSpecifier*) SS.getScopeRep()
1883 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001884 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1885 return ExprError();
1886 }
1887
1888 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001889}
1890
Douglas Gregorc45c2322009-03-31 00:43:58 +00001891/// \brief Form a dependent template name.
1892///
1893/// This action forms a dependent template name given the template
1894/// name and its (presumably dependent) scope specifier. For
1895/// example, given "MetaFun::template apply", the scope specifier \p
1896/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1897/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001898TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001899 SourceLocation TemplateKWLoc,
1900 CXXScopeSpec &SS,
1901 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001902 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001903 bool EnteringContext,
1904 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001905 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1906 !getLangOptions().CPlusPlus0x)
1907 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001908 << FixItHint::CreateRemoval(TemplateKWLoc);
1909
Douglas Gregor0707bc52010-01-19 16:01:07 +00001910 DeclContext *LookupCtx = 0;
1911 if (SS.isSet())
1912 LookupCtx = computeDeclContext(SS, EnteringContext);
1913 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00001914 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00001915 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001916 // C++0x [temp.names]p5:
1917 // If a name prefixed by the keyword template is not the name of
1918 // a template, the program is ill-formed. [Note: the keyword
1919 // template may not be applied to non-template members of class
1920 // templates. -end note ] [ Note: as is the case with the
1921 // typename prefix, the template prefix is allowed in cases
1922 // where it is not strictly necessary; i.e., when the
1923 // nested-name-specifier or the expression on the left of the ->
1924 // or . is not dependent on a template-parameter, or the use
1925 // does not appear in the scope of a template. -end note]
1926 //
1927 // Note: C++03 was more strict here, because it banned the use of
1928 // the "template" keyword prior to a template-name that was not a
1929 // dependent name. C++ DR468 relaxed this requirement (the
1930 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00001931 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001932 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001933 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
1934 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001935 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001936 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1937 isa<CXXRecordDecl>(LookupCtx) &&
1938 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00001939 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001940 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001941 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001942 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001943 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001944 << Name.getSourceRange()
1945 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001946 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001947 } else {
1948 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00001949 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001950 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00001951 }
1952
Mike Stump1eb44332009-09-09 15:08:12 +00001953 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001954 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001955
Douglas Gregor014e88d2009-11-03 23:16:33 +00001956 switch (Name.getKind()) {
1957 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001958 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001959 Name.Identifier));
1960 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001961
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001962 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00001963 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001964 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00001965 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00001966
1967 case UnqualifiedId::IK_LiteralOperatorId:
1968 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
1969
Douglas Gregor014e88d2009-11-03 23:16:33 +00001970 default:
1971 break;
1972 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001973
1974 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001975 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001976 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001977 << Name.getSourceRange()
1978 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001979 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001980}
1981
Mike Stump1eb44332009-09-09 15:08:12 +00001982bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00001983 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00001984 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00001985 const TemplateArgument &Arg = AL.getArgument();
1986
Anders Carlsson436b1562009-06-13 00:33:33 +00001987 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00001988 switch(Arg.getKind()) {
1989 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00001990 // C++ [temp.arg.type]p1:
1991 // A template-argument for a template-parameter which is a
1992 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00001993 break;
1994 case TemplateArgument::Template: {
1995 // We have a template type parameter but the template argument
1996 // is a template without any arguments.
1997 SourceRange SR = AL.getSourceRange();
1998 TemplateName Name = Arg.getAsTemplate();
1999 Diag(SR.getBegin(), diag::err_template_missing_args)
2000 << Name << SR;
2001 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2002 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002003
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002004 return true;
2005 }
2006 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002007 // We have a template type parameter but the template argument
2008 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002009 SourceRange SR = AL.getSourceRange();
2010 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002011 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Anders Carlsson436b1562009-06-13 00:33:33 +00002013 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002014 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002015 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002016
John McCalla93c9342009-12-07 02:54:59 +00002017 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002018 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Anders Carlsson436b1562009-06-13 00:33:33 +00002020 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002021 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002022 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002023 return false;
2024}
2025
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002026/// \brief Substitute template arguments into the default template argument for
2027/// the given template type parameter.
2028///
2029/// \param SemaRef the semantic analysis object for which we are performing
2030/// the substitution.
2031///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002032/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002033/// for.
2034///
2035/// \param TemplateLoc the location of the template name that started the
2036/// template-id we are checking.
2037///
2038/// \param RAngleLoc the location of the right angle bracket ('>') that
2039/// terminates the template-id.
2040///
2041/// \param Param the template template parameter whose default we are
2042/// substituting into.
2043///
2044/// \param Converted the list of template arguments provided for template
2045/// parameters that precede \p Param in the template parameter list.
2046///
2047/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002048static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002049SubstDefaultTemplateArgument(Sema &SemaRef,
2050 TemplateDecl *Template,
2051 SourceLocation TemplateLoc,
2052 SourceLocation RAngleLoc,
2053 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002054 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002055 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002056
2057 // If the argument type is dependent, instantiate it now based
2058 // on the previously-computed template arguments.
2059 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002060 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002061 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002062
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002063 MultiLevelTemplateArgumentList AllTemplateArgs
2064 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2065
2066 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002067 Template, Converted.data(),
2068 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002069 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002070
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002071 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2072 Param->getDefaultArgumentLoc(),
2073 Param->getDeclName());
2074 }
2075
2076 return ArgType;
2077}
2078
2079/// \brief Substitute template arguments into the default template argument for
2080/// the given non-type template parameter.
2081///
2082/// \param SemaRef the semantic analysis object for which we are performing
2083/// the substitution.
2084///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002085/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002086/// for.
2087///
2088/// \param TemplateLoc the location of the template name that started the
2089/// template-id we are checking.
2090///
2091/// \param RAngleLoc the location of the right angle bracket ('>') that
2092/// terminates the template-id.
2093///
Douglas Gregor788cd062009-11-11 01:00:40 +00002094/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002095/// substituting into.
2096///
2097/// \param Converted the list of template arguments provided for template
2098/// parameters that precede \p Param in the template parameter list.
2099///
2100/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002101static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002102SubstDefaultTemplateArgument(Sema &SemaRef,
2103 TemplateDecl *Template,
2104 SourceLocation TemplateLoc,
2105 SourceLocation RAngleLoc,
2106 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002107 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002108 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002109 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002110
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002111 MultiLevelTemplateArgumentList AllTemplateArgs
2112 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002113
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002114 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002115 Template, Converted.data(),
2116 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002117 SourceRange(TemplateLoc, RAngleLoc));
2118
2119 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2120}
2121
Douglas Gregor788cd062009-11-11 01:00:40 +00002122/// \brief Substitute template arguments into the default template argument for
2123/// the given template template parameter.
2124///
2125/// \param SemaRef the semantic analysis object for which we are performing
2126/// the substitution.
2127///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002128/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002129/// for.
2130///
2131/// \param TemplateLoc the location of the template name that started the
2132/// template-id we are checking.
2133///
2134/// \param RAngleLoc the location of the right angle bracket ('>') that
2135/// terminates the template-id.
2136///
2137/// \param Param the template template parameter whose default we are
2138/// substituting into.
2139///
2140/// \param Converted the list of template arguments provided for template
2141/// parameters that precede \p Param in the template parameter list.
2142///
2143/// \returns the substituted template argument, or NULL if an error occurred.
2144static TemplateName
2145SubstDefaultTemplateArgument(Sema &SemaRef,
2146 TemplateDecl *Template,
2147 SourceLocation TemplateLoc,
2148 SourceLocation RAngleLoc,
2149 TemplateTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002150 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002151 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002152 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002153
Douglas Gregor788cd062009-11-11 01:00:40 +00002154 MultiLevelTemplateArgumentList AllTemplateArgs
2155 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002156
Douglas Gregor788cd062009-11-11 01:00:40 +00002157 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002158 Template, Converted.data(),
2159 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002160 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002161
Douglas Gregor788cd062009-11-11 01:00:40 +00002162 return SemaRef.SubstTemplateName(
2163 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002164 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002165 AllTemplateArgs);
2166}
2167
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002168/// \brief If the given template parameter has a default template
2169/// argument, substitute into that default template argument and
2170/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002171TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002172Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2173 SourceLocation TemplateLoc,
2174 SourceLocation RAngleLoc,
2175 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002176 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2177 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002178 if (!TypeParm->hasDefaultArgument())
2179 return TemplateArgumentLoc();
2180
John McCalla93c9342009-12-07 02:54:59 +00002181 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002182 TemplateLoc,
2183 RAngleLoc,
2184 TypeParm,
2185 Converted);
2186 if (DI)
2187 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2188
2189 return TemplateArgumentLoc();
2190 }
2191
2192 if (NonTypeTemplateParmDecl *NonTypeParm
2193 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2194 if (!NonTypeParm->hasDefaultArgument())
2195 return TemplateArgumentLoc();
2196
John McCall60d7b3a2010-08-24 06:29:42 +00002197 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002198 TemplateLoc,
2199 RAngleLoc,
2200 NonTypeParm,
2201 Converted);
2202 if (Arg.isInvalid())
2203 return TemplateArgumentLoc();
2204
2205 Expr *ArgE = Arg.takeAs<Expr>();
2206 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2207 }
2208
2209 TemplateTemplateParmDecl *TempTempParm
2210 = cast<TemplateTemplateParmDecl>(Param);
2211 if (!TempTempParm->hasDefaultArgument())
2212 return TemplateArgumentLoc();
2213
2214 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002215 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002216 RAngleLoc,
2217 TempTempParm,
2218 Converted);
2219 if (TName.isNull())
2220 return TemplateArgumentLoc();
2221
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002222 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002223 TempTempParm->getDefaultArgument().getTemplateQualifierRange(),
2224 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2225}
2226
Douglas Gregore7526412009-11-11 19:31:23 +00002227/// \brief Check that the given template argument corresponds to the given
2228/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002229///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002230/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002231/// checked.
2232///
2233/// \param Arg The template argument.
2234///
2235/// \param Template The template in which the template argument resides.
2236///
2237/// \param TemplateLoc The location of the template name for the template
2238/// whose argument list we're matching.
2239///
2240/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2241/// the template argument list.
2242///
2243/// \param ArgumentPackIndex The index into the argument pack where this
2244/// argument will be placed. Only valid if the parameter is a parameter pack.
2245///
2246/// \param Converted The checked, converted argument will be added to the
2247/// end of this small vector.
2248///
2249/// \param CTAK Describes how we arrived at this particular template argument:
2250/// explicitly written, deduced, etc.
2251///
2252/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002253bool Sema::CheckTemplateArgument(NamedDecl *Param,
2254 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002255 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002256 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002257 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002258 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002259 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002260 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002261 // Check template type parameters.
2262 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002263 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002264
Douglas Gregord9e15302009-11-11 19:41:09 +00002265 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002266 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002267 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002268 // with the template arguments we've seen thus far. But if the
2269 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002270 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002271 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2272 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002273
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002274 if (NTTPType->isDependentType() &&
2275 !isa<TemplateTemplateParmDecl>(Template) &&
2276 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002277 // Do substitution on the type of the non-type template parameter.
2278 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002279 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002280 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002281
2282 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002283 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002284 NTTPType = SubstType(NTTPType,
2285 MultiLevelTemplateArgumentList(TemplateArgs),
2286 NTTP->getLocation(),
2287 NTTP->getDeclName());
2288 // If that worked, check the non-type template parameter type
2289 // for validity.
2290 if (!NTTPType.isNull())
2291 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2292 NTTP->getLocation());
2293 if (NTTPType.isNull())
2294 return true;
2295 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002296
Douglas Gregore7526412009-11-11 19:31:23 +00002297 switch (Arg.getArgument().getKind()) {
2298 case TemplateArgument::Null:
2299 assert(false && "Should never see a NULL template argument here");
2300 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002301
Douglas Gregore7526412009-11-11 19:31:23 +00002302 case TemplateArgument::Expression: {
2303 Expr *E = Arg.getArgument().getAsExpr();
2304 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002305 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002306 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002307
Douglas Gregor910f8002010-11-07 23:05:16 +00002308 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002309 break;
2310 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002311
Douglas Gregore7526412009-11-11 19:31:23 +00002312 case TemplateArgument::Declaration:
2313 case TemplateArgument::Integral:
2314 // We've already checked this template argument, so just copy
2315 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002316 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002317 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002318
Douglas Gregore7526412009-11-11 19:31:23 +00002319 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002320 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002321 // We were given a template template argument. It may not be ill-formed;
2322 // see below.
2323 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002324 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2325 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002326 // We have a template argument such as \c T::template X, which we
2327 // parsed as a template template argument. However, since we now
2328 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002329 // template name into an expression.
2330
2331 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2332 Arg.getTemplateNameLoc());
2333
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002334 // FIXME: TemplateArgumentLoc should store a NestedNameSpecifierLoc
2335 // for the template name.
2336 CXXScopeSpec SS;
2337 SS.MakeTrivial(Context, DTN->getQualifier(),
2338 Arg.getTemplateQualifierRange());
John McCallf7a1a742009-11-24 19:00:30 +00002339 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002340 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002341 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342
Douglas Gregora7fc9012011-01-05 18:58:31 +00002343 // If we parsed the template argument as a pack expansion, create a
2344 // pack expansion expression.
2345 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002346 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002347 Arg.getTemplateEllipsisLoc());
2348 if (Expansion.isInvalid())
2349 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002350
Douglas Gregora7fc9012011-01-05 18:58:31 +00002351 E = Expansion.get();
2352 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002353
Douglas Gregore7526412009-11-11 19:31:23 +00002354 TemplateArgument Result;
2355 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2356 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002357
Douglas Gregor910f8002010-11-07 23:05:16 +00002358 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002359 break;
2360 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002361
Douglas Gregore7526412009-11-11 19:31:23 +00002362 // We have a template argument that actually does refer to a class
2363 // template, template alias, or template template parameter, and
2364 // therefore cannot be a non-type template argument.
2365 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2366 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002367
Douglas Gregore7526412009-11-11 19:31:23 +00002368 Diag(Param->getLocation(), diag::note_template_param_here);
2369 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002370
Douglas Gregore7526412009-11-11 19:31:23 +00002371 case TemplateArgument::Type: {
2372 // We have a non-type template parameter but the template
2373 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002374
Douglas Gregore7526412009-11-11 19:31:23 +00002375 // C++ [temp.arg]p2:
2376 // In a template-argument, an ambiguity between a type-id and
2377 // an expression is resolved to a type-id, regardless of the
2378 // form of the corresponding template-parameter.
2379 //
2380 // We warn specifically about this case, since it can be rather
2381 // confusing for users.
2382 QualType T = Arg.getArgument().getAsType();
2383 SourceRange SR = Arg.getSourceRange();
2384 if (T->isFunctionType())
2385 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2386 else
2387 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2388 Diag(Param->getLocation(), diag::note_template_param_here);
2389 return true;
2390 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002391
Douglas Gregore7526412009-11-11 19:31:23 +00002392 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002393 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002394 break;
2395 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002396
Douglas Gregore7526412009-11-11 19:31:23 +00002397 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002398 }
2399
2400
Douglas Gregore7526412009-11-11 19:31:23 +00002401 // Check template template parameters.
2402 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002403
Douglas Gregore7526412009-11-11 19:31:23 +00002404 // Substitute into the template parameter list of the template
2405 // template parameter, since previously-supplied template arguments
2406 // may appear within the template template parameter.
2407 {
2408 // Set up a template instantiation context.
2409 LocalInstantiationScope Scope(*this);
2410 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002411 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002412 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002413
2414 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002415 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002416 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002417 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002418 MultiLevelTemplateArgumentList(TemplateArgs)));
2419 if (!TempParm)
2420 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002421 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002422
Douglas Gregore7526412009-11-11 19:31:23 +00002423 switch (Arg.getArgument().getKind()) {
2424 case TemplateArgument::Null:
2425 assert(false && "Should never see a NULL template argument here");
2426 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002427
Douglas Gregore7526412009-11-11 19:31:23 +00002428 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002429 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002430 if (CheckTemplateArgument(TempParm, Arg))
2431 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002432
Douglas Gregor910f8002010-11-07 23:05:16 +00002433 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002434 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002435
Douglas Gregore7526412009-11-11 19:31:23 +00002436 case TemplateArgument::Expression:
2437 case TemplateArgument::Type:
2438 // We have a template template parameter but the template
2439 // argument does not refer to a template.
2440 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2441 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002442
Douglas Gregore7526412009-11-11 19:31:23 +00002443 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002444 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002445 "Declaration argument with template template parameter");
2446 break;
2447 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002448 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002449 "Integral argument with template template parameter");
2450 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002451
Douglas Gregore7526412009-11-11 19:31:23 +00002452 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002453 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002454 break;
2455 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002456
Douglas Gregore7526412009-11-11 19:31:23 +00002457 return false;
2458}
2459
Douglas Gregorc15cb382009-02-09 23:23:08 +00002460/// \brief Check that the given template argument list is well-formed
2461/// for specializing the given template.
2462bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2463 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00002464 const TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002465 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002466 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002467 TemplateParameterList *Params = Template->getTemplateParameters();
2468 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002469 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002470 bool Invalid = false;
2471
John McCalld5532b62009-11-23 01:53:49 +00002472 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2473
Mike Stump1eb44332009-09-09 15:08:12 +00002474 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002475 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002476
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002477 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002478 (NumArgs < Params->getMinRequiredArguments() &&
2479 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002480 // FIXME: point at either the first arg beyond what we can handle,
2481 // or the '>', depending on whether we have too many or too few
2482 // arguments.
2483 SourceRange Range;
2484 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002485 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002486 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2487 << (NumArgs > NumParams)
2488 << (isa<ClassTemplateDecl>(Template)? 0 :
2489 isa<FunctionTemplateDecl>(Template)? 1 :
2490 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2491 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002492 Diag(Template->getLocation(), diag::note_template_decl_here)
2493 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002494 Invalid = true;
2495 }
Mike Stump1eb44332009-09-09 15:08:12 +00002496
2497 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002498 // [...] The type and form of each template-argument specified in
2499 // a template-id shall match the type and form specified for the
2500 // corresponding parameter declared by the template in its
2501 // template-parameter-list.
Douglas Gregor14be16b2010-12-20 16:57:52 +00002502 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2503 TemplateParameterList::iterator Param = Params->begin(),
2504 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002505 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002506 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002507 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002508 if (ArgIdx > NumArgs && PartialTemplateArgs)
2509 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002510
Douglas Gregorf35f8282009-11-11 21:54:23 +00002511 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002512 // If we have an expanded parameter pack, make sure we don't have too
2513 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002514 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002515 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002516 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002517 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2518 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2519 << true
2520 << (isa<ClassTemplateDecl>(Template)? 0 :
2521 isa<FunctionTemplateDecl>(Template)? 1 :
2522 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2523 << Template;
2524 Diag(Template->getLocation(), diag::note_template_decl_here)
2525 << Params->getSourceRange();
2526 return true;
2527 }
2528 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002529
Douglas Gregorf35f8282009-11-11 21:54:23 +00002530 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002531 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2532 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002533 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002534 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535
Douglas Gregor14be16b2010-12-20 16:57:52 +00002536 if ((*Param)->isTemplateParameterPack()) {
2537 // The template parameter was a template parameter pack, so take the
2538 // deduced argument and place it on the argument pack. Note that we
2539 // stay on the same template parameter so that we can deduce more
2540 // arguments.
2541 ArgumentPack.push_back(Converted.back());
2542 Converted.pop_back();
2543 } else {
2544 // Move to the next template parameter.
2545 ++Param;
2546 }
2547 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002548 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002549 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002550
2551 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002552 // arguments, just break out now and we'll fill in the argument pack below.
2553 if ((*Param)->isTemplateParameterPack())
2554 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002555
Douglas Gregorf35f8282009-11-11 21:54:23 +00002556 // We have a default template argument that we will use.
2557 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002558
Douglas Gregorf35f8282009-11-11 21:54:23 +00002559 // Retrieve the default template argument from the template
2560 // parameter. For each kind of template parameter, we substitute the
2561 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002562 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002563 // the default argument.
2564 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2565 if (!TTP->hasDefaultArgument()) {
2566 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2567 break;
2568 }
2569
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002570 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002571 Template,
2572 TemplateLoc,
2573 RAngleLoc,
2574 TTP,
2575 Converted);
2576 if (!ArgType)
2577 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002578
Douglas Gregorf35f8282009-11-11 21:54:23 +00002579 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2580 ArgType);
2581 } else if (NonTypeTemplateParmDecl *NTTP
2582 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2583 if (!NTTP->hasDefaultArgument()) {
2584 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2585 break;
2586 }
2587
John McCall60d7b3a2010-08-24 06:29:42 +00002588 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002589 TemplateLoc,
2590 RAngleLoc,
2591 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002592 Converted);
2593 if (E.isInvalid())
2594 return true;
2595
2596 Expr *Ex = E.takeAs<Expr>();
2597 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2598 } else {
2599 TemplateTemplateParmDecl *TempParm
2600 = cast<TemplateTemplateParmDecl>(*Param);
2601
2602 if (!TempParm->hasDefaultArgument()) {
2603 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2604 break;
2605 }
2606
2607 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002608 TemplateLoc,
2609 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002610 TempParm,
2611 Converted);
2612 if (Name.isNull())
2613 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002614
2615 Arg = TemplateArgumentLoc(TemplateArgument(Name),
Douglas Gregorf35f8282009-11-11 21:54:23 +00002616 TempParm->getDefaultArgument().getTemplateQualifierRange(),
2617 TempParm->getDefaultArgument().getTemplateNameLoc());
2618 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002619
Douglas Gregorf35f8282009-11-11 21:54:23 +00002620 // Introduce an instantiation record that describes where we are using
2621 // the default template argument.
2622 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002623 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002624 SourceRange(TemplateLoc, RAngleLoc));
2625
Douglas Gregorf35f8282009-11-11 21:54:23 +00002626 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002627 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002628 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002629 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002630
Douglas Gregor14be16b2010-12-20 16:57:52 +00002631 // Move to the next template parameter and argument.
2632 ++Param;
2633 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002634 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002635
Douglas Gregor14be16b2010-12-20 16:57:52 +00002636 // Form argument packs for each of the parameter packs remaining.
2637 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002638 // If we're checking a partial list of template arguments, don't fill
2639 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002640
2641 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002642 if (PartialTemplateArgs && ArgumentPack.empty()) {
2643 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002644 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002645 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002646 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002647 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2648 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002649 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002650 ArgumentPack.clear();
2651 }
2652 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653
Douglas Gregor14be16b2010-12-20 16:57:52 +00002654 ++Param;
2655 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002656
Douglas Gregorc15cb382009-02-09 23:23:08 +00002657 return Invalid;
2658}
2659
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002660namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002661 class UnnamedLocalNoLinkageFinder
2662 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002663 {
2664 Sema &S;
2665 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002666
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002667 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002668
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002669 public:
2670 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2671
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002672 bool Visit(QualType T) {
2673 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002674 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002675
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002676#define TYPE(Class, Parent) \
2677 bool Visit##Class##Type(const Class##Type *);
2678#define ABSTRACT_TYPE(Class, Parent) \
2679 bool Visit##Class##Type(const Class##Type *) { return false; }
2680#define NON_CANONICAL_TYPE(Class, Parent) \
2681 bool Visit##Class##Type(const Class##Type *) { return false; }
2682#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002683
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002684 bool VisitTagDecl(const TagDecl *Tag);
2685 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2686 };
2687}
2688
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002689bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002690 return false;
2691}
2692
2693bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2694 return Visit(T->getElementType());
2695}
2696
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002697bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002698 return Visit(T->getPointeeType());
2699}
2700
2701bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002702 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002703 return Visit(T->getPointeeType());
2704}
2705
2706bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002707 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002708 return Visit(T->getPointeeType());
2709}
2710
2711bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002712 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002713 return Visit(T->getPointeeType());
2714}
2715
2716bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002717 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002718 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2719}
2720
2721bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002722 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002723 return Visit(T->getElementType());
2724}
2725
2726bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002728 return Visit(T->getElementType());
2729}
2730
2731bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002733 return Visit(T->getElementType());
2734}
2735
2736bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002737 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002738 return Visit(T->getElementType());
2739}
2740
2741bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002742 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002743 return Visit(T->getElementType());
2744}
2745
2746bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2747 return Visit(T->getElementType());
2748}
2749
2750bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2751 return Visit(T->getElementType());
2752}
2753
2754bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2755 const FunctionProtoType* T) {
2756 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002757 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002758 A != AEnd; ++A) {
2759 if (Visit(*A))
2760 return true;
2761 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002762
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002763 return Visit(T->getResultType());
2764}
2765
2766bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2767 const FunctionNoProtoType* T) {
2768 return Visit(T->getResultType());
2769}
2770
2771bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2772 const UnresolvedUsingType*) {
2773 return false;
2774}
2775
2776bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2777 return false;
2778}
2779
2780bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2781 return Visit(T->getUnderlyingType());
2782}
2783
2784bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2785 return false;
2786}
2787
Richard Smith34b41d92011-02-20 03:19:35 +00002788bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2789 return Visit(T->getDeducedType());
2790}
2791
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002792bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2793 return VisitTagDecl(T->getDecl());
2794}
2795
2796bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2797 return VisitTagDecl(T->getDecl());
2798}
2799
2800bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2801 const TemplateTypeParmType*) {
2802 return false;
2803}
2804
Douglas Gregorc3069d62011-01-14 02:55:32 +00002805bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2806 const SubstTemplateTypeParmPackType *) {
2807 return false;
2808}
2809
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002810bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2811 const TemplateSpecializationType*) {
2812 return false;
2813}
2814
2815bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2816 const InjectedClassNameType* T) {
2817 return VisitTagDecl(T->getDecl());
2818}
2819
2820bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2821 const DependentNameType* T) {
2822 return VisitNestedNameSpecifier(T->getQualifier());
2823}
2824
2825bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2826 const DependentTemplateSpecializationType* T) {
2827 return VisitNestedNameSpecifier(T->getQualifier());
2828}
2829
Douglas Gregor7536dd52010-12-20 02:24:11 +00002830bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2831 const PackExpansionType* T) {
2832 return Visit(T->getPattern());
2833}
2834
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002835bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2836 return false;
2837}
2838
2839bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2840 const ObjCInterfaceType *) {
2841 return false;
2842}
2843
2844bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2845 const ObjCObjectPointerType *) {
2846 return false;
2847}
2848
2849bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2850 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2851 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2852 << S.Context.getTypeDeclType(Tag) << SR;
2853 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002854 }
2855
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002856 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2857 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2858 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2859 return true;
2860 }
2861
2862 return false;
2863}
2864
2865bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2866 NestedNameSpecifier *NNS) {
2867 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2868 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002869
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002870 switch (NNS->getKind()) {
2871 case NestedNameSpecifier::Identifier:
2872 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002873 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002874 case NestedNameSpecifier::Global:
2875 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002876
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002877 case NestedNameSpecifier::TypeSpec:
2878 case NestedNameSpecifier::TypeSpecWithTemplate:
2879 return Visit(QualType(NNS->getAsType(), 0));
2880 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002881 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002882}
2883
2884
Douglas Gregorc15cb382009-02-09 23:23:08 +00002885/// \brief Check a template argument against its corresponding
2886/// template type parameter.
2887///
2888/// This routine implements the semantics of C++ [temp.arg.type]. It
2889/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002890bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002891 TypeSourceInfo *ArgInfo) {
2892 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002893 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00002894 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00002895
2896 if (Arg->isVariablyModifiedType()) {
2897 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002898 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002899 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002900 }
2901
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002902 // C++03 [temp.arg.type]p2:
2903 // A local type, a type with no linkage, an unnamed type or a type
2904 // compounded from any of these types shall not be used as a
2905 // template-argument for a template type-parameter.
2906 //
2907 // C++0x allows these, and even in C++03 we allow them as an extension with
2908 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00002909 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002910 UnnamedLocalNoLinkageFinder Finder(*this, SR);
2911 (void)Finder.Visit(Context.getCanonicalType(Arg));
2912 }
2913
Douglas Gregorc15cb382009-02-09 23:23:08 +00002914 return false;
2915}
2916
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002917/// \brief Checks whether the given template argument is the address
2918/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002919static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00002920CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
2921 NonTypeTemplateParmDecl *Param,
2922 QualType ParamType,
2923 Expr *ArgIn,
2924 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002925 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002926 Expr *Arg = ArgIn;
2927 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002928
2929 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00002930 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002931 Arg = Cast->getSubExpr();
2932
2933 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00002934 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002935 // A template-argument for a non-type, non-template
2936 // template-parameter shall be one of: [...]
2937 //
2938 // -- the address of an object or function with external
2939 // linkage, including function templates and function
2940 // template-ids but excluding non-static class members,
2941 // expressed as & id-expression where the & is optional if
2942 // the name refers to a function or array, or if the
2943 // corresponding template-parameter is a reference; or
2944 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002945
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002946 // In C++98/03 mode, give an extension warning on any extra parentheses.
2947 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
2948 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002949 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002950 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00002951 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002952 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002953 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002954 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002955 }
2956
2957 Arg = Parens->getSubExpr();
2958 }
2959
Douglas Gregorb7a09262010-04-01 18:32:35 +00002960 bool AddressTaken = false;
2961 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002962 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00002963 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002964 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00002965 AddressTaken = true;
2966 AddrOpLoc = UnOp->getOperatorLoc();
2967 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002968 } else
2969 DRE = dyn_cast<DeclRefExpr>(Arg);
2970
Douglas Gregorb7a09262010-04-01 18:32:35 +00002971 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00002972 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
2973 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002974 S.Diag(Param->getLocation(), diag::note_template_param_here);
2975 return true;
2976 }
Chandler Carruth038cc392010-01-31 10:01:20 +00002977
2978 // Stop checking the precise nature of the argument if it is value dependent,
2979 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00002980 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00002981 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00002982 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002983 }
Chandler Carruth038cc392010-01-31 10:01:20 +00002984
Douglas Gregorb7a09262010-04-01 18:32:35 +00002985 if (!isa<ValueDecl>(DRE->getDecl())) {
2986 S.Diag(Arg->getSourceRange().getBegin(),
2987 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002988 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002989 S.Diag(Param->getLocation(), diag::note_template_param_here);
2990 return true;
2991 }
2992
2993 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002994
2995 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00002996 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
2997 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002998 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002999 S.Diag(Param->getLocation(), diag::note_template_param_here);
3000 return true;
3001 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003002
3003 // Cannot refer to non-static member functions
3004 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003005 if (!Method->isStatic()) {
3006 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003007 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003008 S.Diag(Param->getLocation(), diag::note_template_param_here);
3009 return true;
3010 }
Mike Stump1eb44332009-09-09 15:08:12 +00003011
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003012 // Functions must have external linkage.
3013 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003014 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003015 S.Diag(Arg->getSourceRange().getBegin(),
3016 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003017 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003018 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003019 << true;
3020 return true;
3021 }
3022
3023 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003024 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003025
Douglas Gregorb7a09262010-04-01 18:32:35 +00003026 // If the template parameter has pointer type, the function decays.
3027 if (ParamType->isPointerType() && !AddressTaken)
3028 ArgType = S.Context.getPointerType(Func->getType());
3029 else if (AddressTaken && ParamType->isReferenceType()) {
3030 // If we originally had an address-of operator, but the
3031 // parameter has reference type, complain and (if things look
3032 // like they will work) drop the address-of operator.
3033 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3034 ParamType.getNonReferenceType())) {
3035 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3036 << ParamType;
3037 S.Diag(Param->getLocation(), diag::note_template_param_here);
3038 return true;
3039 }
3040
3041 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3042 << ParamType
3043 << FixItHint::CreateRemoval(AddrOpLoc);
3044 S.Diag(Param->getLocation(), diag::note_template_param_here);
3045
3046 ArgType = Func->getType();
3047 }
3048 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003049 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003050 S.Diag(Arg->getSourceRange().getBegin(),
3051 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003052 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003053 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003054 << true;
3055 return true;
3056 }
3057
Douglas Gregorb7a09262010-04-01 18:32:35 +00003058 // A value of reference type is not an object.
3059 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003060 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003061 diag::err_template_arg_reference_var)
3062 << Var->getType() << Arg->getSourceRange();
3063 S.Diag(Param->getLocation(), diag::note_template_param_here);
3064 return true;
3065 }
3066
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003067 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003068 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003069
3070 // If the template parameter has pointer type, we must have taken
3071 // the address of this object.
3072 if (ParamType->isReferenceType()) {
3073 if (AddressTaken) {
3074 // If we originally had an address-of operator, but the
3075 // parameter has reference type, complain and (if things look
3076 // like they will work) drop the address-of operator.
3077 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3078 ParamType.getNonReferenceType())) {
3079 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3080 << ParamType;
3081 S.Diag(Param->getLocation(), diag::note_template_param_here);
3082 return true;
3083 }
3084
3085 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3086 << ParamType
3087 << FixItHint::CreateRemoval(AddrOpLoc);
3088 S.Diag(Param->getLocation(), diag::note_template_param_here);
3089
3090 ArgType = Var->getType();
3091 }
3092 } else if (!AddressTaken && ParamType->isPointerType()) {
3093 if (Var->getType()->isArrayType()) {
3094 // Array-to-pointer decay.
3095 ArgType = S.Context.getArrayDecayedType(Var->getType());
3096 } else {
3097 // If the template parameter has pointer type but the address of
3098 // this object was not taken, complain and (possibly) recover by
3099 // taking the address of the entity.
3100 ArgType = S.Context.getPointerType(Var->getType());
3101 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3102 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3103 << ParamType;
3104 S.Diag(Param->getLocation(), diag::note_template_param_here);
3105 return true;
3106 }
3107
3108 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3109 << ParamType
3110 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3111
3112 S.Diag(Param->getLocation(), diag::note_template_param_here);
3113 }
3114 }
3115 } else {
3116 // We found something else, but we don't know specifically what it is.
3117 S.Diag(Arg->getSourceRange().getBegin(),
3118 diag::err_template_arg_not_object_or_func)
3119 << Arg->getSourceRange();
3120 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3121 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003122 }
Mike Stump1eb44332009-09-09 15:08:12 +00003123
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003124 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003125 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003126 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003127 // For pointer-to-object types, qualification conversions are
3128 // permitted.
3129 } else {
3130 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3131 if (!ParamRef->getPointeeType()->isFunctionType()) {
3132 // C++ [temp.arg.nontype]p5b3:
3133 // For a non-type template-parameter of type reference to
3134 // object, no conversions apply. The type referred to by the
3135 // reference may be more cv-qualified than the (otherwise
3136 // identical) type of the template- argument. The
3137 // template-parameter is bound directly to the
3138 // template-argument, which shall be an lvalue.
3139
3140 // FIXME: Other qualifiers?
3141 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3142 unsigned ArgQuals = ArgType.getCVRQualifiers();
3143
3144 if ((ParamQuals | ArgQuals) != ParamQuals) {
3145 S.Diag(Arg->getSourceRange().getBegin(),
3146 diag::err_template_arg_ref_bind_ignores_quals)
3147 << ParamType << Arg->getType()
3148 << Arg->getSourceRange();
3149 S.Diag(Param->getLocation(), diag::note_template_param_here);
3150 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003151 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003152 }
3153 }
3154
3155 // At this point, the template argument refers to an object or
3156 // function with external linkage. We now need to check whether the
3157 // argument and parameter types are compatible.
3158 if (!S.Context.hasSameUnqualifiedType(ArgType,
3159 ParamType.getNonReferenceType())) {
3160 // We can't perform this conversion or binding.
3161 if (ParamType->isReferenceType())
3162 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3163 << ParamType << Arg->getType() << Arg->getSourceRange();
3164 else
3165 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3166 << Arg->getType() << ParamType << Arg->getSourceRange();
3167 S.Diag(Param->getLocation(), diag::note_template_param_here);
3168 return true;
3169 }
3170 }
3171
3172 // Create the template argument.
3173 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003174 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003175 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003176}
3177
3178/// \brief Checks whether the given template argument is a pointer to
3179/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003180bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003181 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003182 bool Invalid = false;
3183
3184 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003185 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003186 Arg = Cast->getSubExpr();
3187
3188 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003189 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003190 // A template-argument for a non-type, non-template
3191 // template-parameter shall be one of: [...]
3192 //
3193 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003194 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003195
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003196 // In C++98/03 mode, give an extension warning on any extra parentheses.
3197 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3198 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003199 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003200 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003201 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003202 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003203 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003204 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003205 }
3206
3207 Arg = Parens->getSubExpr();
3208 }
3209
Douglas Gregorcaddba02009-11-12 18:38:13 +00003210 // A pointer-to-member constant written &Class::member.
3211 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003212 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003213 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3214 if (DRE && !DRE->getQualifier())
3215 DRE = 0;
3216 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003217 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003218 // A constant of pointer-to-member type.
3219 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3220 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3221 if (VD->getType()->isMemberPointerType()) {
3222 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003223 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003224 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3225 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003226 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003227 else
3228 Converted = TemplateArgument(VD->getCanonicalDecl());
3229 return Invalid;
3230 }
3231 }
3232 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003233
Douglas Gregorcaddba02009-11-12 18:38:13 +00003234 DRE = 0;
3235 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003236
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003237 if (!DRE)
3238 return Diag(Arg->getSourceRange().getBegin(),
3239 diag::err_template_arg_not_pointer_to_member_form)
3240 << Arg->getSourceRange();
3241
3242 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3243 assert((isa<FieldDecl>(DRE->getDecl()) ||
3244 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3245 "Only non-static member pointers can make it here");
3246
3247 // Okay: this is the address of a non-static member, and therefore
3248 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003249 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003250 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003251 else
3252 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003253 return Invalid;
3254 }
3255
3256 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003257 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003258 diag::err_template_arg_not_pointer_to_member_form)
3259 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003260 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003261 diag::note_template_arg_refers_here);
3262 return true;
3263}
3264
Douglas Gregorc15cb382009-02-09 23:23:08 +00003265/// \brief Check a template argument against its corresponding
3266/// non-type template parameter.
3267///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003268/// This routine implements the semantics of C++ [temp.arg.nontype].
3269/// It returns true if an error occurred, and false otherwise. \p
3270/// InstantiatedParamType is the type of the non-type template
3271/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003272///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003273/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003274bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003275 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003276 TemplateArgument &Converted,
3277 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003278 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3279
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003280 // If either the parameter has a dependent type or the argument is
3281 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003282 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3283 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003284 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003285 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003286 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003287
3288 // C++ [temp.arg.nontype]p5:
3289 // The following conversions are performed on each expression used
3290 // as a non-type template-argument. If a non-type
3291 // template-argument cannot be converted to the type of the
3292 // corresponding template-parameter then the program is
3293 // ill-formed.
3294 //
3295 // -- for a non-type template-parameter of integral or
3296 // enumeration type, integral promotions (4.5) and integral
3297 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003298 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003299 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003300 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003301 // C++ [temp.arg.nontype]p1:
3302 // A template-argument for a non-type, non-template
3303 // template-parameter shall be one of:
3304 //
3305 // -- an integral constant-expression of integral or enumeration
3306 // type; or
3307 // -- the name of a non-type template-parameter; or
3308 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003309 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003310 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003311 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003312 diag::err_template_arg_not_integral_or_enumeral)
3313 << ArgType << Arg->getSourceRange();
3314 Diag(Param->getLocation(), diag::note_template_param_here);
3315 return true;
3316 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003317 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003318 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3319 << ArgType << Arg->getSourceRange();
3320 return true;
3321 }
3322
Douglas Gregor02024a92010-03-28 02:42:43 +00003323 // From here on out, all we care about are the unqualified forms
3324 // of the parameter and argument types.
3325 ParamType = ParamType.getUnqualifiedType();
3326 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003327
3328 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003329 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003330 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003331 } else if (CTAK == CTAK_Deduced) {
3332 // C++ [temp.deduct.type]p17:
3333 // If, in the declaration of a function template with a non-type
3334 // template-parameter, the non-type template- parameter is used
3335 // in an expression in the function parameter-list and, if the
3336 // corresponding template-argument is deduced, the
3337 // template-argument type shall match the type of the
3338 // template-parameter exactly, except that a template-argument
3339 // deduced from an array bound may be of any integral type.
3340 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3341 << ArgType << ParamType;
3342 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003343 return true;
3344 } else if (ParamType->isBooleanType()) {
3345 // This is an integral-to-boolean conversion.
3346 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003347 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3348 !ParamType->isEnumeralType()) {
3349 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003350 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003351 } else {
3352 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003353 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003354 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003355 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003356 Diag(Param->getLocation(), diag::note_template_param_here);
3357 return true;
3358 }
3359
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003360 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003361 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003362 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003363
3364 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003365 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003366
3367 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003368 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003369 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003370 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003371 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003372 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003373
3374 // Complain if an unsigned parameter received a negative value.
3375 if (IntegerType->isUnsignedIntegerType()
3376 && (OldValue.isSigned() && OldValue.isNegative())) {
3377 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3378 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3379 << Arg->getSourceRange();
3380 Diag(Param->getLocation(), diag::note_template_param_here);
3381 }
3382
3383 // Complain if we overflowed the template parameter's type.
3384 unsigned RequiredBits;
3385 if (IntegerType->isUnsignedIntegerType())
3386 RequiredBits = OldValue.getActiveBits();
3387 else if (OldValue.isUnsigned())
3388 RequiredBits = OldValue.getActiveBits() + 1;
3389 else
3390 RequiredBits = OldValue.getMinSignedBits();
3391 if (RequiredBits > AllowedBits) {
3392 Diag(Arg->getSourceRange().getBegin(),
3393 diag::warn_template_arg_too_large)
3394 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3395 << Arg->getSourceRange();
3396 Diag(Param->getLocation(), diag::note_template_param_here);
3397 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003398 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003399
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003400 // Add the value of this argument to the list of converted
3401 // arguments. We use the bitwidth and signedness of the template
3402 // parameter.
3403 if (Arg->isValueDependent()) {
3404 // The argument is value-dependent. Create a new
3405 // TemplateArgument with the converted expression.
3406 Converted = TemplateArgument(Arg);
3407 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003408 }
3409
John McCall833ca992009-10-29 08:12:44 +00003410 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003411 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003412 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003413 return false;
3414 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003415
John McCall6bb80172010-03-30 21:47:33 +00003416 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3417
Douglas Gregorb7a09262010-04-01 18:32:35 +00003418 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3419 // from a template argument of type std::nullptr_t to a non-type
3420 // template parameter of type pointer to object, pointer to
3421 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003422 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003423 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3424 Converted = TemplateArgument((NamedDecl *)0);
3425 return false;
3426 }
3427
Douglas Gregorb86b0572009-02-11 01:18:59 +00003428 // Handle pointer-to-function, reference-to-function, and
3429 // pointer-to-member-function all in (roughly) the same way.
3430 if (// -- For a non-type template-parameter of type pointer to
3431 // function, only the function-to-pointer conversion (4.3) is
3432 // applied. If the template-argument represents a set of
3433 // overloaded functions (or a pointer to such), the matching
3434 // function is selected from the set (13.4).
3435 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003436 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003437 // -- For a non-type template-parameter of type reference to
3438 // function, no conversions apply. If the template-argument
3439 // represents a set of overloaded functions, the matching
3440 // function is selected from the set (13.4).
3441 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003442 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003443 // -- For a non-type template-parameter of type pointer to
3444 // member function, no conversions apply. If the
3445 // template-argument represents a set of overloaded member
3446 // functions, the matching member function is selected from
3447 // the set (13.4).
3448 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003449 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003450 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003451
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003452 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003453 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003454 true,
3455 FoundResult)) {
3456 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3457 return true;
3458
3459 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3460 ArgType = Arg->getType();
3461 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003462 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003463 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003464
Douglas Gregorb7a09262010-04-01 18:32:35 +00003465 if (!ParamType->isMemberPointerType())
3466 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003467 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003468 Arg, Converted);
3469
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003470 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3471 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003472 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003473 } else if (!Context.hasSameUnqualifiedType(ArgType,
3474 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003475 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003476 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003477 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003478 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003479 Diag(Param->getLocation(), diag::note_template_param_here);
3480 return true;
3481 }
Mike Stump1eb44332009-09-09 15:08:12 +00003482
Douglas Gregorb7a09262010-04-01 18:32:35 +00003483 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003484 }
3485
Chris Lattnerfe90de72009-02-20 21:37:53 +00003486 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003487 // -- for a non-type template-parameter of type pointer to
3488 // object, qualification conversions (4.4) and the
3489 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003490 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003491 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003492 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003493
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003494 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003495 ParamType,
3496 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003497 }
Mike Stump1eb44332009-09-09 15:08:12 +00003498
Ted Kremenek6217b802009-07-29 21:53:49 +00003499 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003500 // -- For a non-type template-parameter of type reference to
3501 // object, no conversions apply. The type referred to by the
3502 // reference may be more cv-qualified than the (otherwise
3503 // identical) type of the template-argument. The
3504 // template-parameter is bound directly to the
3505 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003506 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003507 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003508
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003509 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003510 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3511 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003512 true,
3513 FoundResult)) {
3514 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3515 return true;
3516
3517 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3518 ArgType = Arg->getType();
3519 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003520 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003521 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003522
3523 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003524 ParamType,
3525 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003526 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003527
3528 // -- For a non-type template-parameter of type pointer to data
3529 // member, qualification conversions (4.4) are applied.
3530 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3531
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003532 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003533 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003534 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003535 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003536 } else {
3537 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003538 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003539 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003540 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003541 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003542 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003543 }
3544
Douglas Gregorcaddba02009-11-12 18:38:13 +00003545 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003546}
3547
3548/// \brief Check a template argument against its corresponding
3549/// template template parameter.
3550///
3551/// This routine implements the semantics of C++ [temp.arg.template].
3552/// It returns true if an error occurred, and false otherwise.
3553bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003554 const TemplateArgumentLoc &Arg) {
3555 TemplateName Name = Arg.getArgument().getAsTemplate();
3556 TemplateDecl *Template = Name.getAsTemplateDecl();
3557 if (!Template) {
3558 // Any dependent template name is fine.
3559 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3560 return false;
3561 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003562
3563 // C++ [temp.arg.template]p1:
3564 // A template-argument for a template template-parameter shall be
3565 // the name of a class template, expressed as id-expression. Only
3566 // primary class templates are considered when matching the
3567 // template template argument with the corresponding parameter;
3568 // partial specializations are not considered even if their
3569 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003570 //
3571 // Note that we also allow template template parameters here, which
3572 // will happen when we are dealing with, e.g., class template
3573 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003574 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003575 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003576 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003577 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003578 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003579 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003580 << Template;
3581 }
3582
3583 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3584 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003585 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003586 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003587 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003588}
3589
Douglas Gregor02024a92010-03-28 02:42:43 +00003590/// \brief Given a non-type template argument that refers to a
3591/// declaration and the type of its corresponding non-type template
3592/// parameter, produce an expression that properly refers to that
3593/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003594ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003595Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3596 QualType ParamType,
3597 SourceLocation Loc) {
3598 assert(Arg.getKind() == TemplateArgument::Declaration &&
3599 "Only declaration template arguments permitted here");
3600 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3601
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003602 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003603 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3604 // If the value is a class member, we might have a pointer-to-member.
3605 // Determine whether the non-type template template parameter is of
3606 // pointer-to-member type. If so, we need to build an appropriate
3607 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3608 // would refer to the member itself.
3609 if (ParamType->isMemberPointerType()) {
3610 QualType ClassType
3611 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3612 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003613 = NestedNameSpecifier::Create(Context, 0, false,
3614 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003615 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003616 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003617
3618 // The actual value-ness of this is unimportant, but for
3619 // internal consistency's sake, references to instance methods
3620 // are r-values.
3621 ExprValueKind VK = VK_LValue;
3622 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3623 VK = VK_RValue;
3624
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003625 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003626 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003627 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003628 Loc,
3629 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003630 if (RefExpr.isInvalid())
3631 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003632
John McCall2de56d12010-08-25 11:45:40 +00003633 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003634
Douglas Gregorc0c83002010-04-30 21:46:38 +00003635 // We might need to perform a trailing qualification conversion, since
3636 // the element type on the parameter could be more qualified than the
3637 // element type in the expression we constructed.
3638 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003639 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003640 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003641 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003642 RefExpr = Owned(RefE);
3643 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003644
Douglas Gregor02024a92010-03-28 02:42:43 +00003645 assert(!RefExpr.isInvalid() &&
3646 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003647 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003648 return move(RefExpr);
3649 }
3650 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003651
Douglas Gregor02024a92010-03-28 02:42:43 +00003652 QualType T = VD->getType().getNonReferenceType();
3653 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003654 // When the non-type template parameter is a pointer, take the
3655 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003656 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003657 if (RefExpr.isInvalid())
3658 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003659
3660 if (T->isFunctionType() || T->isArrayType()) {
3661 // Decay functions and arrays.
3662 Expr *RefE = (Expr *)RefExpr.get();
3663 DefaultFunctionArrayConversion(RefE);
3664 if (RefE != RefExpr.get()) {
3665 RefExpr.release();
3666 RefExpr = Owned(RefE);
3667 }
3668
3669 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003671
Douglas Gregorb7a09262010-04-01 18:32:35 +00003672 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003673 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003674 }
3675
John McCallf89e55a2010-11-18 06:31:45 +00003676 ExprValueKind VK = VK_RValue;
3677
Douglas Gregor02024a92010-03-28 02:42:43 +00003678 // If the non-type template parameter has reference type, qualify the
3679 // resulting declaration reference with the extra qualifiers on the
3680 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003681 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3682 VK = VK_LValue;
3683 T = Context.getQualifiedType(T,
3684 TargetRef->getPointeeType().getQualifiers());
3685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003686
John McCallf89e55a2010-11-18 06:31:45 +00003687 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003688}
3689
3690/// \brief Construct a new expression that refers to the given
3691/// integral template argument with the given source-location
3692/// information.
3693///
3694/// This routine takes care of the mapping from an integral template
3695/// argument (which may have any integral type) to the appropriate
3696/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003697ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003698Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3699 SourceLocation Loc) {
3700 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003701 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003702 QualType T = Arg.getIntegralType();
3703 if (T->isCharType() || T->isWideCharType())
3704 return Owned(new (Context) CharacterLiteral(
3705 Arg.getAsIntegral()->getZExtValue(),
3706 T->isWideCharType(),
3707 T,
3708 Loc));
3709 if (T->isBooleanType())
3710 return Owned(new (Context) CXXBoolLiteralExpr(
3711 Arg.getAsIntegral()->getBoolValue(),
3712 T,
3713 Loc));
3714
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003715 QualType BT;
3716 if (const EnumType *ET = T->getAs<EnumType>())
3717 BT = ET->getDecl()->getPromotionType();
3718 else
3719 BT = T;
3720
3721 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003722 if (T->isEnumeralType()) {
3723 // FIXME: This is a hack. We need a better way to handle substituted
3724 // non-type template parameters.
3725 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3726 E, 0,
3727 Context.getTrivialTypeSourceInfo(T, Loc),
3728 Loc, Loc);
3729 }
3730
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003731 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003732}
3733
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003734/// \brief Match two template parameters within template parameter lists.
3735static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3736 bool Complain,
3737 Sema::TemplateParameterListEqualKind Kind,
3738 SourceLocation TemplateArgLoc) {
3739 // Check the actual kind (type, non-type, template).
3740 if (Old->getKind() != New->getKind()) {
3741 if (Complain) {
3742 unsigned NextDiag = diag::err_template_param_different_kind;
3743 if (TemplateArgLoc.isValid()) {
3744 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3745 NextDiag = diag::note_template_param_different_kind;
3746 }
3747 S.Diag(New->getLocation(), NextDiag)
3748 << (Kind != Sema::TPL_TemplateMatch);
3749 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3750 << (Kind != Sema::TPL_TemplateMatch);
3751 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003752
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003753 return false;
3754 }
3755
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003756 // Check that both are parameter packs are neither are parameter packs.
3757 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003758 // template template parameter, the template template parameter can have
3759 // a parameter pack where the template template argument does not.
3760 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3761 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3762 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003763 if (Complain) {
3764 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3765 if (TemplateArgLoc.isValid()) {
3766 S.Diag(TemplateArgLoc,
3767 diag::err_template_arg_template_params_mismatch);
3768 NextDiag = diag::note_template_parameter_pack_non_pack;
3769 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003770
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003771 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3772 : isa<NonTypeTemplateParmDecl>(New)? 1
3773 : 2;
3774 S.Diag(New->getLocation(), NextDiag)
3775 << ParamKind << New->isParameterPack();
3776 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3777 << ParamKind << Old->isParameterPack();
3778 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003779
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003780 return false;
3781 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003782
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003783 // For non-type template parameters, check the type of the parameter.
3784 if (NonTypeTemplateParmDecl *OldNTTP
3785 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3786 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003787
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003788 // If we are matching a template template argument to a template
3789 // template parameter and one of the non-type template parameter types
3790 // is dependent, then we must wait until template instantiation time
3791 // to actually compare the arguments.
3792 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3793 (OldNTTP->getType()->isDependentType() ||
3794 NewNTTP->getType()->isDependentType()))
3795 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003796
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003797 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3798 if (Complain) {
3799 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3800 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003801 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003802 diag::err_template_arg_template_params_mismatch);
3803 NextDiag = diag::note_template_nontype_parm_different_type;
3804 }
3805 S.Diag(NewNTTP->getLocation(), NextDiag)
3806 << NewNTTP->getType()
3807 << (Kind != Sema::TPL_TemplateMatch);
3808 S.Diag(OldNTTP->getLocation(),
3809 diag::note_template_nontype_parm_prev_declaration)
3810 << OldNTTP->getType();
3811 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003812
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003813 return false;
3814 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003815
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003816 return true;
3817 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003818
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003819 // For template template parameters, check the template parameter types.
3820 // The template parameter lists of template template
3821 // parameters must agree.
3822 if (TemplateTemplateParmDecl *OldTTP
3823 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003824 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003825 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3826 OldTTP->getTemplateParameters(),
3827 Complain,
3828 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003829 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003830 : Kind),
3831 TemplateArgLoc);
3832 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003833
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003834 return true;
3835}
Douglas Gregor02024a92010-03-28 02:42:43 +00003836
Douglas Gregora0347822011-01-13 00:08:50 +00003837/// \brief Diagnose a known arity mismatch when comparing template argument
3838/// lists.
3839static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003840void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003841 TemplateParameterList *New,
3842 TemplateParameterList *Old,
3843 Sema::TemplateParameterListEqualKind Kind,
3844 SourceLocation TemplateArgLoc) {
3845 unsigned NextDiag = diag::err_template_param_list_different_arity;
3846 if (TemplateArgLoc.isValid()) {
3847 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3848 NextDiag = diag::note_template_param_list_different_arity;
3849 }
3850 S.Diag(New->getTemplateLoc(), NextDiag)
3851 << (New->size() > Old->size())
3852 << (Kind != Sema::TPL_TemplateMatch)
3853 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3854 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3855 << (Kind != Sema::TPL_TemplateMatch)
3856 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3857}
3858
Douglas Gregorddc29e12009-02-06 22:42:48 +00003859/// \brief Determine whether the given template parameter lists are
3860/// equivalent.
3861///
Mike Stump1eb44332009-09-09 15:08:12 +00003862/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003863/// source code as part of a new template declaration.
3864///
3865/// \param Old The old template parameter list, typically found via
3866/// name lookup of the template declared with this template parameter
3867/// list.
3868///
3869/// \param Complain If true, this routine will produce a diagnostic if
3870/// the template parameter lists are not equivalent.
3871///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003872/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003873///
3874/// \param TemplateArgLoc If this source location is valid, then we
3875/// are actually checking the template parameter list of a template
3876/// argument (New) against the template parameter list of its
3877/// corresponding template template parameter (Old). We produce
3878/// slightly different diagnostics in this scenario.
3879///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003880/// \returns True if the template parameter lists are equal, false
3881/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003882bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003883Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3884 TemplateParameterList *Old,
3885 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003886 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00003887 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00003888 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
3889 if (Complain)
3890 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3891 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00003892
3893 return false;
3894 }
3895
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003896 // C++0x [temp.arg.template]p3:
3897 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003898 // when each of the template parameters in the template-parameter-list of
3899 // the template-argument's corresponding class template or template alias
3900 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00003901 // template-parameter-list of P. [...]
3902 TemplateParameterList::iterator NewParm = New->begin();
3903 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003904 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00003905 OldParmEnd = Old->end();
3906 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00003907 if (Kind != TPL_TemplateTemplateArgumentMatch ||
3908 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00003909 if (NewParm == NewParmEnd) {
3910 if (Complain)
3911 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3912 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003913
Douglas Gregora0347822011-01-13 00:08:50 +00003914 return false;
3915 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003916
Douglas Gregora0347822011-01-13 00:08:50 +00003917 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3918 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003919 return false;
3920
Douglas Gregora0347822011-01-13 00:08:50 +00003921 ++NewParm;
3922 continue;
3923 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003924
Douglas Gregora0347822011-01-13 00:08:50 +00003925 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003926 // [...] When P's template- parameter-list contains a template parameter
3927 // pack (14.5.3), the template parameter pack will match zero or more
3928 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00003929 // template-parameter-list of A with the same type and form as the
3930 // template parameter pack in P (ignoring whether those template
3931 // parameters are template parameter packs).
3932 for (; NewParm != NewParmEnd; ++NewParm) {
3933 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3934 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003935 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00003936 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00003937 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003938
Douglas Gregora0347822011-01-13 00:08:50 +00003939 // Make sure we exhausted all of the arguments.
3940 if (NewParm != NewParmEnd) {
3941 if (Complain)
3942 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3943 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003944
Douglas Gregora0347822011-01-13 00:08:50 +00003945 return false;
3946 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003947
Douglas Gregorddc29e12009-02-06 22:42:48 +00003948 return true;
3949}
3950
3951/// \brief Check whether a template can be declared within this scope.
3952///
3953/// If the template declaration is valid in this scope, returns
3954/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00003955bool
Douglas Gregor05396e22009-08-25 17:23:04 +00003956Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00003957 // Find the nearest enclosing declaration scope.
3958 while ((S->getFlags() & Scope::DeclScope) == 0 ||
3959 (S->getFlags() & Scope::TemplateParamScope) != 0)
3960 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00003961
Douglas Gregorddc29e12009-02-06 22:42:48 +00003962 // C++ [temp]p2:
3963 // A template-declaration can appear only as a namespace scope or
3964 // class scope declaration.
3965 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00003966 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
3967 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00003968 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00003969 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003970
Eli Friedman1503f772009-07-31 01:43:05 +00003971 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00003972 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003973
3974 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
3975 return false;
3976
Mike Stump1eb44332009-09-09 15:08:12 +00003977 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00003978 diag::err_template_outside_namespace_or_class_scope)
3979 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003980}
Douglas Gregorcc636682009-02-17 23:15:12 +00003981
Douglas Gregord5cb8762009-10-07 00:13:32 +00003982/// \brief Determine what kind of template specialization the given declaration
3983/// is.
3984static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
3985 if (!D)
3986 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003987
Douglas Gregorf6b11852009-10-08 15:14:33 +00003988 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
3989 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00003990 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
3991 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003992 if (VarDecl *Var = dyn_cast<VarDecl>(D))
3993 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003994
Douglas Gregord5cb8762009-10-07 00:13:32 +00003995 return TSK_Undeclared;
3996}
3997
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003998/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00003999/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004000///
Douglas Gregor9302da62009-10-14 23:50:59 +00004001/// This routine determines whether a template specialization can be declared
4002/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004003///
4004/// \param S the semantic analysis object for which this check is being
4005/// performed.
4006///
4007/// \param Specialized the entity being specialized or instantiated, which
4008/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004009/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004010/// member class).
4011///
4012/// \param PrevDecl the previous declaration of this entity, if any.
4013///
4014/// \param Loc the location of the explicit specialization or instantiation of
4015/// this entity.
4016///
4017/// \param IsPartialSpecialization whether this is a partial specialization of
4018/// a class template.
4019///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004020/// \returns true if there was an error that we cannot recover from, false
4021/// otherwise.
4022static bool CheckTemplateSpecializationScope(Sema &S,
4023 NamedDecl *Specialized,
4024 NamedDecl *PrevDecl,
4025 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004026 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004027 // Keep these "kind" numbers in sync with the %select statements in the
4028 // various diagnostics emitted by this routine.
4029 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004030 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004031 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004032 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004033 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004034 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004035 EntityKind = 3;
4036 else if (isa<VarDecl>(Specialized))
4037 EntityKind = 4;
4038 else if (isa<RecordDecl>(Specialized))
4039 EntityKind = 5;
4040 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004041 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4042 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004043 return true;
4044 }
4045
Douglas Gregor88b70942009-02-25 22:02:03 +00004046 // C++ [temp.expl.spec]p2:
4047 // An explicit specialization shall be declared in the namespace
4048 // of which the template is a member, or, for member templates, in
4049 // the namespace of which the enclosing class or enclosing class
4050 // template is a member. An explicit specialization of a member
4051 // function, member class or static data member of a class
4052 // template shall be declared in the namespace of which the class
4053 // template is a member. Such a declaration may also be a
4054 // definition. If the declaration is not a definition, the
4055 // specialization may be defined later in the name- space in which
4056 // the explicit specialization was declared, or in a namespace
4057 // that encloses the one in which the explicit specialization was
4058 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004059 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004060 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004061 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004062 return true;
4063 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004064
Douglas Gregor0a407472009-10-07 17:30:37 +00004065 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4066 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004067 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004068 return true;
4069 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004070
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004071 // C++ [temp.class.spec]p6:
4072 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004073 // in any namespace scope in which its definition may be defined (14.5.1
4074 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004075 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004076 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004077 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004078 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004079 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004080 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4081 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004082 // C++ [temp.exp.spec]p2:
4083 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004084 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004085 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004086 // An explicit specialization of a member function, member class or
4087 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004088 // namespace of which the class template is a member.
4089 //
4090 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004091 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004092 // the specialized template.
4093 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4094 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004095 bool IsCPlusPlus0xExtension
4096 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004097 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004098 S.Diag(Loc, IsCPlusPlus0xExtension
4099 ? diag::ext_template_spec_decl_out_of_scope_global
4100 : diag::err_template_spec_decl_out_of_scope_global)
4101 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004102 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004103 S.Diag(Loc, IsCPlusPlus0xExtension
4104 ? diag::ext_template_spec_decl_out_of_scope
4105 : diag::err_template_spec_decl_out_of_scope)
4106 << EntityKind << Specialized
4107 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004108
Douglas Gregor9302da62009-10-14 23:50:59 +00004109 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4110 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004111 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004112 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004113
4114 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004115 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004116 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004117 // specializations of function templates, static data members, and member
4118 // functions, so we skip the check here for those kinds of entities.
4119 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004120 // Should we refactor that check, so that it occurs later?
4121 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004122 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4123 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004124 if (isa<TranslationUnitDecl>(SpecializedContext))
4125 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4126 << EntityKind << Specialized;
4127 else if (isa<NamespaceDecl>(SpecializedContext))
4128 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4129 << EntityKind << Specialized
4130 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004131
Douglas Gregor9302da62009-10-14 23:50:59 +00004132 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004133 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004134
Douglas Gregord5cb8762009-10-07 00:13:32 +00004135 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004136
Douglas Gregor88b70942009-02-25 22:02:03 +00004137 return false;
4138}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004139
Douglas Gregorbacb9492011-01-03 21:13:47 +00004140/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4141/// that checks non-type template partial specialization arguments.
4142static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4143 NonTypeTemplateParmDecl *Param,
4144 const TemplateArgument *Args,
4145 unsigned NumArgs) {
4146 for (unsigned I = 0; I != NumArgs; ++I) {
4147 if (Args[I].getKind() == TemplateArgument::Pack) {
4148 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004149 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004150 Args[I].pack_size()))
4151 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004152
Douglas Gregore94866f2009-06-12 21:21:02 +00004153 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004154 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004155
Douglas Gregorbacb9492011-01-03 21:13:47 +00004156 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004157 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004158 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004159 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004160
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004161 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004162 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4163 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004164
4165 // Strip off any implicit casts we added as part of type checking.
4166 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4167 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004168
Douglas Gregore94866f2009-06-12 21:21:02 +00004169 // C++ [temp.class.spec]p8:
4170 // A non-type argument is non-specialized if it is the name of a
4171 // non-type parameter. All other non-type arguments are
4172 // specialized.
4173 //
4174 // Below, we check the two conditions that only apply to
4175 // specialized non-type arguments, so skip any non-specialized
4176 // arguments.
4177 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004178 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004179 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004180
Douglas Gregore94866f2009-06-12 21:21:02 +00004181 // C++ [temp.class.spec]p9:
4182 // Within the argument list of a class template partial
4183 // specialization, the following restrictions apply:
4184 // -- A partially specialized non-type argument expression
4185 // shall not involve a template parameter of the partial
4186 // specialization except when the argument expression is a
4187 // simple identifier.
4188 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004189 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004190 diag::err_dependent_non_type_arg_in_partial_spec)
4191 << ArgExpr->getSourceRange();
4192 return true;
4193 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004194
Douglas Gregore94866f2009-06-12 21:21:02 +00004195 // -- The type of a template parameter corresponding to a
4196 // specialized non-type argument shall not be dependent on a
4197 // parameter of the specialization.
4198 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004199 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004200 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4201 << Param->getType()
4202 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004203 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004204 return true;
4205 }
4206 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004207
Douglas Gregorbacb9492011-01-03 21:13:47 +00004208 return false;
4209}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004210
Douglas Gregorbacb9492011-01-03 21:13:47 +00004211/// \brief Check the non-type template arguments of a class template
4212/// partial specialization according to C++ [temp.class.spec]p9.
4213///
4214/// \param TemplateParams the template parameters of the primary class
4215/// template.
4216///
4217/// \param TemplateArg the template arguments of the class template
4218/// partial specialization.
4219///
4220/// \returns true if there was an error, false otherwise.
4221static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4222 TemplateParameterList *TemplateParams,
4223 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4224 const TemplateArgument *ArgList = TemplateArgs.data();
4225
4226 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4227 NonTypeTemplateParmDecl *Param
4228 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4229 if (!Param)
4230 continue;
4231
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004232 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004233 &ArgList[I], 1))
4234 return true;
4235 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004236
4237 return false;
4238}
4239
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004240/// \brief Retrieve the previous declaration of the given declaration.
4241static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4242 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4243 return VD->getPreviousDeclaration();
4244 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4245 return FD->getPreviousDeclaration();
4246 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4247 return TD->getPreviousDeclaration();
4248 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4249 return TD->getPreviousDeclaration();
4250 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4251 return FTD->getPreviousDeclaration();
4252 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4253 return CTD->getPreviousDeclaration();
4254 return 0;
4255}
4256
John McCalld226f652010-08-21 09:40:31 +00004257DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004258Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4259 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004260 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004261 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004262 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004263 SourceLocation TemplateNameLoc,
4264 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004265 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004266 SourceLocation RAngleLoc,
4267 AttributeList *Attr,
4268 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004269 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004270
Douglas Gregorcc636682009-02-17 23:15:12 +00004271 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004272 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004273 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004274 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4275
4276 if (!ClassTemplate) {
4277 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004278 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004279 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4280 return true;
4281 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004282
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004283 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004284 bool isPartialSpecialization = false;
4285
Douglas Gregor88b70942009-02-25 22:02:03 +00004286 // Check the validity of the template headers that introduce this
4287 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004288 // FIXME: We probably shouldn't complain about these headers for
4289 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004290 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004291 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004292 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4293 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004294 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004295 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004296 isExplicitSpecialization,
4297 Invalid);
4298 if (Invalid)
4299 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004300
Abramo Bagnara9b934882010-06-12 08:15:14 +00004301 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4302 if (TemplateParams)
4303 --NumMatchedTemplateParamLists;
4304
Douglas Gregor05396e22009-08-25 17:23:04 +00004305 if (TemplateParams && TemplateParams->size() > 0) {
4306 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004307
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004308 if (TUK == TUK_Friend) {
4309 Diag(KWLoc, diag::err_partial_specialization_friend)
4310 << SourceRange(LAngleLoc, RAngleLoc);
4311 return true;
4312 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004313
Douglas Gregor05396e22009-08-25 17:23:04 +00004314 // C++ [temp.class.spec]p10:
4315 // The template parameter list of a specialization shall not
4316 // contain default template argument values.
4317 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4318 Decl *Param = TemplateParams->getParam(I);
4319 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4320 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004321 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004322 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004323 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004324 }
4325 } else if (NonTypeTemplateParmDecl *NTTP
4326 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4327 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004328 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004329 diag::err_default_arg_in_partial_spec)
4330 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004331 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004332 }
4333 } else {
4334 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004335 if (TTP->hasDefaultArgument()) {
4336 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004337 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004338 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004339 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004340 }
4341 }
4342 }
Douglas Gregora735b202009-10-13 14:39:41 +00004343 } else if (TemplateParams) {
4344 if (TUK == TUK_Friend)
4345 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004346 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004347 SourceRange(TemplateParams->getTemplateLoc(),
4348 TemplateParams->getRAngleLoc()))
4349 << SourceRange(LAngleLoc, RAngleLoc);
4350 else
4351 isExplicitSpecialization = true;
4352 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004353 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004354 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004355 isExplicitSpecialization = true;
4356 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004357
Douglas Gregorcc636682009-02-17 23:15:12 +00004358 // Check that the specialization uses the same tag kind as the
4359 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004360 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4361 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004362 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004363 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004364 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004365 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004366 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004367 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004368 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004369 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004370 diag::note_previous_use);
4371 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4372 }
4373
Douglas Gregor40808ce2009-03-09 23:48:35 +00004374 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004375 TemplateArgumentListInfo TemplateArgs;
4376 TemplateArgs.setLAngleLoc(LAngleLoc);
4377 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004378 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004379
Douglas Gregor925910d2011-01-03 20:35:03 +00004380 // Check for unexpanded parameter packs in any of the template arguments.
4381 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004382 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004383 UPPC_PartialSpecialization))
4384 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004385
Douglas Gregorcc636682009-02-17 23:15:12 +00004386 // Check that the template argument list is well-formed for this
4387 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004388 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004389 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4390 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004391 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004392
Douglas Gregor910f8002010-11-07 23:05:16 +00004393 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004394 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004395
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004396 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004397 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004398 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004399 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004400 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004401 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004402 return true;
4403
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004404 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004405 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004406 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004407 TemplateArgs.size())) {
4408 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4409 << ClassTemplate->getDeclName();
4410 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004411 }
4412 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004413
Douglas Gregorcc636682009-02-17 23:15:12 +00004414 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004415 ClassTemplateSpecializationDecl *PrevDecl = 0;
4416
4417 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004418 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004419 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004420 = ClassTemplate->findPartialSpecialization(Converted.data(),
4421 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004422 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004423 else
4424 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004425 = ClassTemplate->findSpecialization(Converted.data(),
4426 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004427
4428 ClassTemplateSpecializationDecl *Specialization = 0;
4429
Douglas Gregor88b70942009-02-25 22:02:03 +00004430 // Check whether we can declare a class template specialization in
4431 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004432 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004433 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4434 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004435 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004436 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004437
Douglas Gregorb88e8882009-07-30 17:40:51 +00004438 // The canonical type
4439 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004440 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004441 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004442 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004443 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004444 // arguments was referenced but not declared, or we're only
4445 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004446 // declaration node as our own, updating its source location to
4447 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004448 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004449 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004450 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004451 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004452 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004453 // Build the canonical type that describes the converted template
4454 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004455 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4456 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004457 Converted.data(),
4458 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004459
4460 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004461 ClassTemplate->getInjectedClassNameSpecialization())) {
4462 // C++ [temp.class.spec]p9b3:
4463 //
4464 // -- The argument list of the specialization shall not be identical
4465 // to the implicit argument list of the primary template.
4466 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4467 << (TUK == TUK_Definition)
4468 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4469 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4470 ClassTemplate->getIdentifier(),
4471 TemplateNameLoc,
4472 Attr,
4473 TemplateParams,
4474 AS_none);
4475 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004476
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004477 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004478 ClassTemplatePartialSpecializationDecl *PrevPartial
4479 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004480 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004481 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004482 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004483 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004484 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004485 TemplateNameLoc,
4486 TemplateParams,
4487 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004488 Converted.data(),
4489 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004490 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004491 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004492 PrevPartial,
4493 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004494 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004495 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004496 Partial->setTemplateParameterListsInfo(Context,
4497 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004498 (TemplateParameterList**) TemplateParameterLists.release());
4499 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004500
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004501 if (!PrevPartial)
4502 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004503 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004504
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004505 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004506 // template specialization, make a note of that.
4507 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4508 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004509
Douglas Gregor031a5882009-06-13 00:26:55 +00004510 // Check that all of the template parameters of the class template
4511 // partial specialization are deducible from the template
4512 // arguments. If not, this class template partial specialization
4513 // will never be used.
4514 llvm::SmallVector<bool, 8> DeducibleParams;
4515 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004516 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004517 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004518 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004519 unsigned NumNonDeducible = 0;
4520 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4521 if (!DeducibleParams[I])
4522 ++NumNonDeducible;
4523
4524 if (NumNonDeducible) {
4525 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4526 << (NumNonDeducible > 1)
4527 << SourceRange(TemplateNameLoc, RAngleLoc);
4528 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4529 if (!DeducibleParams[I]) {
4530 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4531 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004532 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004533 diag::note_partial_spec_unused_parameter)
4534 << Param->getDeclName();
4535 else
Mike Stump1eb44332009-09-09 15:08:12 +00004536 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004537 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004538 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004539 }
4540 }
4541 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004542 } else {
4543 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004544 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004545 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004546 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004547 ClassTemplate->getDeclContext(),
4548 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004549 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004550 Converted.data(),
4551 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004552 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004553 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004554 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004555 Specialization->setTemplateParameterListsInfo(Context,
4556 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004557 (TemplateParameterList**) TemplateParameterLists.release());
4558 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004559
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004560 if (!PrevDecl)
4561 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004562
4563 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004564 }
4565
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004566 // C++ [temp.expl.spec]p6:
4567 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004568 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004569 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004570 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004571 // use occurs; no diagnostic is required.
4572 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004573 bool Okay = false;
4574 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4575 // Is there any previous explicit specialization declaration?
4576 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4577 Okay = true;
4578 break;
4579 }
4580 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004581
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004582 if (!Okay) {
4583 SourceRange Range(TemplateNameLoc, RAngleLoc);
4584 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4585 << Context.getTypeDeclType(Specialization) << Range;
4586
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004587 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004588 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004589 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004590 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004591 return true;
4592 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004593 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004594
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004595 // If this is not a friend, note that this is an explicit specialization.
4596 if (TUK != TUK_Friend)
4597 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004598
4599 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004600 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004601 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004602 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004603 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004604 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004605 Diag(Def->getLocation(), diag::note_previous_definition);
4606 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004607 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004608 }
4609 }
4610
John McCall7f1b9872010-12-18 03:30:47 +00004611 if (Attr)
4612 ProcessDeclAttributeList(S, Specialization, Attr);
4613
Douglas Gregorfc705b82009-02-26 22:19:44 +00004614 // Build the fully-sugared type for this class template
4615 // specialization as the user wrote in the specialization
4616 // itself. This means that we'll pretty-print the type retrieved
4617 // from the specialization's declaration the way that the user
4618 // actually wrote the specialization, rather than formatting the
4619 // name based on the "canonical" representation used to store the
4620 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004621 TypeSourceInfo *WrittenTy
4622 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4623 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004624 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004625 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004626 if (TemplateParams)
4627 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004628 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004629 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004630
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004631 // C++ [temp.expl.spec]p9:
4632 // A template explicit specialization is in the scope of the
4633 // namespace in which the template was defined.
4634 //
4635 // We actually implement this paragraph where we set the semantic
4636 // context (in the creation of the ClassTemplateSpecializationDecl),
4637 // but we also maintain the lexical context where the actual
4638 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004639 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004640
Douglas Gregorcc636682009-02-17 23:15:12 +00004641 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004642 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004643 Specialization->startDefinition();
4644
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004645 if (TUK == TUK_Friend) {
4646 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4647 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004648 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004649 /*FIXME:*/KWLoc);
4650 Friend->setAccess(AS_public);
4651 CurContext->addDecl(Friend);
4652 } else {
4653 // Add the specialization into its lexical context, so that it can
4654 // be seen when iterating through the list of declarations in that
4655 // context. However, specializations are not found by name lookup.
4656 CurContext->addDecl(Specialization);
4657 }
John McCalld226f652010-08-21 09:40:31 +00004658 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004659}
Douglas Gregord57959a2009-03-27 23:10:48 +00004660
John McCalld226f652010-08-21 09:40:31 +00004661Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004662 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004663 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004664 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4665}
4666
John McCalld226f652010-08-21 09:40:31 +00004667Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004668 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004669 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004670 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004671 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004672
Douglas Gregor52591bf2009-06-24 00:54:41 +00004673 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004674 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004675 }
Mike Stump1eb44332009-09-09 15:08:12 +00004676
Douglas Gregor52591bf2009-06-24 00:54:41 +00004677 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004678
John McCalld226f652010-08-21 09:40:31 +00004679 Decl *DP = HandleDeclarator(ParentScope, D,
4680 move(TemplateParameterLists),
4681 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004682 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004683 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004684 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004685 FunctionTemplate->getTemplatedDecl());
4686 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4687 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4688 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004689}
4690
John McCall75042392010-02-11 01:33:53 +00004691/// \brief Strips various properties off an implicit instantiation
4692/// that has just been explicitly specialized.
4693static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004694 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004695
4696 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4697 FD->setInlineSpecified(false);
4698 }
4699}
4700
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004701/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004702/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004703/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004704/// new specialization/instantiation will have any effect.
4705///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004706/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004707/// instantiation.
4708///
4709/// \param NewTSK the kind of the new explicit specialization or instantiation.
4710///
4711/// \param PrevDecl the previous declaration of the entity.
4712///
4713/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4714///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004715/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004716/// declaration was instantiated (either implicitly or explicitly).
4717///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004718/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004719/// specialization or instantiation has no effect and should be ignored.
4720///
4721/// \returns true if there was an error that should prevent the introduction of
4722/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004723bool
4724Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4725 TemplateSpecializationKind NewTSK,
4726 NamedDecl *PrevDecl,
4727 TemplateSpecializationKind PrevTSK,
4728 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004729 bool &HasNoEffect) {
4730 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004731
Douglas Gregor454885e2009-10-15 15:54:05 +00004732 switch (NewTSK) {
4733 case TSK_Undeclared:
4734 case TSK_ImplicitInstantiation:
4735 assert(false && "Don't check implicit instantiations here");
4736 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004737
Douglas Gregor454885e2009-10-15 15:54:05 +00004738 case TSK_ExplicitSpecialization:
4739 switch (PrevTSK) {
4740 case TSK_Undeclared:
4741 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004742 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004743 // explicitly specialized or has merely been mentioned without any
4744 // instantiation.
4745 return false;
4746
4747 case TSK_ImplicitInstantiation:
4748 if (PrevPointOfInstantiation.isInvalid()) {
4749 // The declaration itself has not actually been instantiated, so it is
4750 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004751 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004752 return false;
4753 }
4754 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004755
Douglas Gregor454885e2009-10-15 15:54:05 +00004756 case TSK_ExplicitInstantiationDeclaration:
4757 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004758 assert((PrevTSK == TSK_ImplicitInstantiation ||
4759 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004760 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004761
Douglas Gregor454885e2009-10-15 15:54:05 +00004762 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004763 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004764 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004765 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004766 // implicit instantiation to take place, in every translation unit in
4767 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004768 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4769 // Is there any previous explicit specialization declaration?
4770 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4771 return false;
4772 }
4773
Douglas Gregor0d035142009-10-27 18:42:08 +00004774 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004775 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004776 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004777 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004778
Douglas Gregor454885e2009-10-15 15:54:05 +00004779 return true;
4780 }
4781 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004782
Douglas Gregor454885e2009-10-15 15:54:05 +00004783 case TSK_ExplicitInstantiationDeclaration:
4784 switch (PrevTSK) {
4785 case TSK_ExplicitInstantiationDeclaration:
4786 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004787 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004788 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004789
Douglas Gregor454885e2009-10-15 15:54:05 +00004790 case TSK_Undeclared:
4791 case TSK_ImplicitInstantiation:
4792 // We're explicitly instantiating something that may have already been
4793 // implicitly instantiated; that's fine.
4794 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004795
Douglas Gregor454885e2009-10-15 15:54:05 +00004796 case TSK_ExplicitSpecialization:
4797 // C++0x [temp.explicit]p4:
4798 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004799 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004800 // specialization for that template, the explicit instantiation has no
4801 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004802 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004803 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004804
Douglas Gregor454885e2009-10-15 15:54:05 +00004805 case TSK_ExplicitInstantiationDefinition:
4806 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004807 // If an entity is the subject of both an explicit instantiation
4808 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004809 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004810 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004811 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004812 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004813 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004814 assert(PrevPointOfInstantiation.isValid() &&
4815 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004816 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004817 return false;
4818 }
4819 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004820
Douglas Gregor454885e2009-10-15 15:54:05 +00004821 case TSK_ExplicitInstantiationDefinition:
4822 switch (PrevTSK) {
4823 case TSK_Undeclared:
4824 case TSK_ImplicitInstantiation:
4825 // We're explicitly instantiating something that may have already been
4826 // implicitly instantiated; that's fine.
4827 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004828
Douglas Gregor454885e2009-10-15 15:54:05 +00004829 case TSK_ExplicitSpecialization:
4830 // C++ DR 259, C++0x [temp.explicit]p4:
4831 // For a given set of template parameters, if an explicit
4832 // instantiation of a template appears after a declaration of
4833 // an explicit specialization for that template, the explicit
4834 // instantiation has no effect.
4835 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004836 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004837 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004838 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004839 if (!getLangOptions().CPlusPlus0x) {
4840 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004841 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004842 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004843 diag::note_previous_template_specialization);
4844 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004845 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004846 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004847
Douglas Gregor454885e2009-10-15 15:54:05 +00004848 case TSK_ExplicitInstantiationDeclaration:
4849 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004850 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004851 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004852
Douglas Gregor454885e2009-10-15 15:54:05 +00004853 case TSK_ExplicitInstantiationDefinition:
4854 // C++0x [temp.spec]p5:
4855 // For a given template and a given set of template-arguments,
4856 // - an explicit instantiation definition shall appear at most once
4857 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004858 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004859 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004860 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004861 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004862 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004863 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004864 }
4865 break;
4866 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004867
Douglas Gregor454885e2009-10-15 15:54:05 +00004868 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004869
Douglas Gregor454885e2009-10-15 15:54:05 +00004870 return false;
4871}
4872
John McCallaf2094e2010-04-08 09:05:18 +00004873/// \brief Perform semantic analysis for the given dependent function
4874/// template specialization. The only possible way to get a dependent
4875/// function template specialization is with a friend declaration,
4876/// like so:
4877///
4878/// template <class T> void foo(T);
4879/// template <class T> class A {
4880/// friend void foo<>(T);
4881/// };
4882///
4883/// There really isn't any useful analysis we can do here, so we
4884/// just store the information.
4885bool
4886Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
4887 const TemplateArgumentListInfo &ExplicitTemplateArgs,
4888 LookupResult &Previous) {
4889 // Remove anything from Previous that isn't a function template in
4890 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004891 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00004892 LookupResult::Filter F = Previous.makeFilter();
4893 while (F.hasNext()) {
4894 NamedDecl *D = F.next()->getUnderlyingDecl();
4895 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00004896 !FDLookupContext->InEnclosingNamespaceSetOf(
4897 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00004898 F.erase();
4899 }
4900 F.done();
4901
4902 // Should this be diagnosed here?
4903 if (Previous.empty()) return true;
4904
4905 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
4906 ExplicitTemplateArgs);
4907 return false;
4908}
4909
Abramo Bagnarae03db982010-05-20 15:32:11 +00004910/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004911/// specialization.
4912///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004913/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004914/// explicit function template specialization. On successful completion,
4915/// the function declaration \p FD will become a function template
4916/// specialization.
4917///
4918/// \param FD the function declaration, which will be updated to become a
4919/// function template specialization.
4920///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004921/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
4922/// if any. Note that this may be valid info even when 0 arguments are
4923/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
4924/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004925///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004926/// \param PrevDecl the set of declarations that may be specialized by
4927/// this function specialization.
4928bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004929Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
John McCalld5532b62009-11-23 01:53:49 +00004930 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00004931 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004932 // The set of function template specializations that could match this
4933 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00004934 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004935
Sebastian Redl7a126a42010-08-31 00:36:30 +00004936 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00004937 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
4938 I != E; ++I) {
4939 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
4940 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004941 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004942 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004943 if (!FDLookupContext->InEnclosingNamespaceSetOf(
4944 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004945 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004946
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004947 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004948 // A trailing template-argument can be left unspecified in the
4949 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004950 // provided it can be deduced from the function argument type.
4951 // Perform template argument deduction to determine whether we may be
4952 // specializing this template.
4953 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00004954 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004955 FunctionDecl *Specialization = 0;
4956 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00004957 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004958 FD->getType(),
4959 Specialization,
4960 Info)) {
4961 // FIXME: Template argument deduction failed; record why it failed, so
4962 // that we can provide nifty diagnostics.
4963 (void)TDK;
4964 continue;
4965 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004966
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004967 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00004968 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004969 }
4970 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004971
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004972 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00004973 UnresolvedSetIterator Result
4974 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00004975 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004976 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004977 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004978 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00004979 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004980 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00004981 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004982 return true;
John McCallc373d482010-01-27 01:50:18 +00004983
4984 // Ignore access information; it doesn't figure into redeclaration checking.
4985 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregorc42b6522010-04-09 21:02:29 +00004986 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004987
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004988 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004989 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00004990
4991 // If this is a friend declaration, then we're not really declaring
4992 // an explicit specialization.
4993 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004994
Douglas Gregord5cb8762009-10-07 00:13:32 +00004995 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00004996 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004997 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004998 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004999 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005000 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005001 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005002
5003 // C++ [temp.expl.spec]p6:
5004 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005005 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005006 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005007 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005008 // use occurs; no diagnostic is required.
5009 FunctionTemplateSpecializationInfo *SpecInfo
5010 = Specialization->getTemplateSpecializationInfo();
5011 assert(SpecInfo && "Function template specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005012
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005013 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005014 if (!isFriend &&
5015 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005016 TSK_ExplicitSpecialization,
5017 Specialization,
5018 SpecInfo->getTemplateSpecializationKind(),
5019 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005020 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005021 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005022
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005023 // Mark the prior declaration as an explicit specialization, so that later
5024 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005025 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005026 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005027 MarkUnusedFileScopedDecl(Specialization);
5028 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005029
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005030 // Turn the given function declaration into a function template
5031 // specialization, with the template arguments from the previous
5032 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005033 // Take copies of (semantic and syntactic) template argument lists.
5034 const TemplateArgumentList* TemplArgs = new (Context)
5035 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5036 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5037 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005038 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005039 TemplArgs, /*InsertPos=*/0,
5040 SpecInfo->getTemplateSpecializationKind(),
5041 TemplArgsAsWritten);
5042
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005043 // The "previous declaration" for this function template specialization is
5044 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005045 Previous.clear();
5046 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005047 return false;
5048}
5049
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005050/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005051/// specialization.
5052///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005053/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005054/// explicit member function specialization. On successful completion,
5055/// the function declaration \p FD will become a member function
5056/// specialization.
5057///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005058/// \param Member the member declaration, which will be updated to become a
5059/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005060///
John McCall68263142009-11-18 22:49:29 +00005061/// \param Previous the set of declarations, one of which may be specialized
5062/// by this function specialization; the set will be modified to contain the
5063/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005064bool
John McCall68263142009-11-18 22:49:29 +00005065Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005066 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005067
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005068 // Try to find the member we are instantiating.
5069 NamedDecl *Instantiation = 0;
5070 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005071 MemberSpecializationInfo *MSInfo = 0;
5072
John McCall68263142009-11-18 22:49:29 +00005073 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005074 // Nowhere to look anyway.
5075 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005076 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5077 I != E; ++I) {
5078 NamedDecl *D = (*I)->getUnderlyingDecl();
5079 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005080 if (Context.hasSameType(Function->getType(), Method->getType())) {
5081 Instantiation = Method;
5082 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005083 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005084 break;
5085 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005086 }
5087 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005088 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005089 VarDecl *PrevVar;
5090 if (Previous.isSingleResult() &&
5091 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005092 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005093 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005094 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005095 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005096 }
5097 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005098 CXXRecordDecl *PrevRecord;
5099 if (Previous.isSingleResult() &&
5100 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5101 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005102 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005103 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005104 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005105 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005106
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005107 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005108 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005109 // specializations are always out-of-line, the caller will complain about
5110 // this mismatch later.
5111 return false;
5112 }
John McCall77e8b112010-04-13 20:37:33 +00005113
5114 // If this is a friend, just bail out here before we start turning
5115 // things into explicit specializations.
5116 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5117 // Preserve instantiation information.
5118 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5119 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5120 cast<CXXMethodDecl>(InstantiatedFrom),
5121 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5122 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5123 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5124 cast<CXXRecordDecl>(InstantiatedFrom),
5125 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5126 }
5127
5128 Previous.clear();
5129 Previous.addDecl(Instantiation);
5130 return false;
5131 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005132
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005133 // Make sure that this is a specialization of a member.
5134 if (!InstantiatedFrom) {
5135 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5136 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005137 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5138 return true;
5139 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005140
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005141 // C++ [temp.expl.spec]p6:
5142 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005143 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005144 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005145 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005146 // use occurs; no diagnostic is required.
5147 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005148
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005149 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005150 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5151 TSK_ExplicitSpecialization,
5152 Instantiation,
5153 MSInfo->getTemplateSpecializationKind(),
5154 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005155 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005156 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005157
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005158 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005159 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005160 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005161 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005162 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005163 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005164
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005165 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005166 // the original declaration to note that it is an explicit specialization
5167 // (if it was previously an implicit instantiation). This latter step
5168 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005169 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005170 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5171 if (InstantiationFunction->getTemplateSpecializationKind() ==
5172 TSK_ImplicitInstantiation) {
5173 InstantiationFunction->setTemplateSpecializationKind(
5174 TSK_ExplicitSpecialization);
5175 InstantiationFunction->setLocation(Member->getLocation());
5176 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005177
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005178 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5179 cast<CXXMethodDecl>(InstantiatedFrom),
5180 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005181 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005182 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005183 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5184 if (InstantiationVar->getTemplateSpecializationKind() ==
5185 TSK_ImplicitInstantiation) {
5186 InstantiationVar->setTemplateSpecializationKind(
5187 TSK_ExplicitSpecialization);
5188 InstantiationVar->setLocation(Member->getLocation());
5189 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005190
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005191 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5192 cast<VarDecl>(InstantiatedFrom),
5193 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005194 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005195 } else {
5196 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005197 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5198 if (InstantiationClass->getTemplateSpecializationKind() ==
5199 TSK_ImplicitInstantiation) {
5200 InstantiationClass->setTemplateSpecializationKind(
5201 TSK_ExplicitSpecialization);
5202 InstantiationClass->setLocation(Member->getLocation());
5203 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005204
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005205 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005206 cast<CXXRecordDecl>(InstantiatedFrom),
5207 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005208 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005209
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005210 // Save the caller the trouble of having to figure out which declaration
5211 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005212 Previous.clear();
5213 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005214 return false;
5215}
5216
Douglas Gregor558c0322009-10-14 23:41:34 +00005217/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005218///
5219/// \returns true if a serious error occurs, false otherwise.
5220static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005221 SourceLocation InstLoc,
5222 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005223 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5224 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005225
Douglas Gregor669eed82010-07-13 00:10:04 +00005226 if (CurContext->isRecord()) {
5227 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5228 << D;
5229 return true;
5230 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005231
Douglas Gregor558c0322009-10-14 23:41:34 +00005232 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005233 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005234 // template.
5235 //
5236 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005237 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005238 !CurContext->Encloses(OrigContext)) {
5239 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005240 S.Diag(InstLoc,
5241 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005242 diag::err_explicit_instantiation_out_of_scope
5243 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005244 << D << NS;
5245 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005246 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005247 S.getLangOptions().CPlusPlus0x?
5248 diag::err_explicit_instantiation_must_be_global
5249 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005250 << D;
5251 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005252 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005253 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005254
Douglas Gregor558c0322009-10-14 23:41:34 +00005255 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005256 // If the name declared in the explicit instantiation is an unqualified
5257 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005258 // its template is declared or, if that namespace is inline (7.3.1), any
5259 // namespace from its enclosing namespace set.
5260 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005261 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005262
5263 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005264 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005265
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005266 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005267 S.getLangOptions().CPlusPlus0x?
5268 diag::err_explicit_instantiation_unqualified_wrong_namespace
5269 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005270 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005271 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005272 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005273}
5274
5275/// \brief Determine whether the given scope specifier has a template-id in it.
5276static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5277 if (!SS.isSet())
5278 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005279
Douglas Gregor558c0322009-10-14 23:41:34 +00005280 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005281 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005282 // or a static data member of a class template specialization, the name of
5283 // the class template specialization in the qualified-id for the member
5284 // name shall be a simple-template-id.
5285 //
5286 // C++98 has the same restriction, just worded differently.
5287 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5288 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005289 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005290 if (isa<TemplateSpecializationType>(T))
5291 return true;
5292
5293 return false;
5294}
5295
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005296// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005297DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005298Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005299 SourceLocation ExternLoc,
5300 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005301 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005302 SourceLocation KWLoc,
5303 const CXXScopeSpec &SS,
5304 TemplateTy TemplateD,
5305 SourceLocation TemplateNameLoc,
5306 SourceLocation LAngleLoc,
5307 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005308 SourceLocation RAngleLoc,
5309 AttributeList *Attr) {
5310 // Find the class template we're specializing
5311 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005312 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005313 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5314
5315 // Check that the specialization uses the same tag kind as the
5316 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005317 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5318 assert(Kind != TTK_Enum &&
5319 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005320 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005321 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005322 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005323 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005324 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005325 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005326 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005327 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005328 diag::note_previous_use);
5329 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5330 }
5331
Douglas Gregor558c0322009-10-14 23:41:34 +00005332 // C++0x [temp.explicit]p2:
5333 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005334 // definition and an explicit instantiation declaration. An explicit
5335 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005336 TemplateSpecializationKind TSK
5337 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5338 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005339
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005340 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005341 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005342 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005343
5344 // Check that the template argument list is well-formed for this
5345 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005346 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005347 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5348 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005349 return true;
5350
Douglas Gregor910f8002010-11-07 23:05:16 +00005351 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005352 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005353
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005354 // Find the class template specialization declaration that
5355 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005356 void *InsertPos = 0;
5357 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005358 = ClassTemplate->findSpecialization(Converted.data(),
5359 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005360
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005361 TemplateSpecializationKind PrevDecl_TSK
5362 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5363
Douglas Gregord5cb8762009-10-07 00:13:32 +00005364 // C++0x [temp.explicit]p2:
5365 // [...] An explicit instantiation shall appear in an enclosing
5366 // namespace of its template. [...]
5367 //
5368 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005369 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5370 SS.isSet()))
5371 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005372
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005373 ClassTemplateSpecializationDecl *Specialization = 0;
5374
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005375 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005376 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005377 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005378 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005379 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005380 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005381 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005382
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005383 // Even though HasNoEffect == true means that this explicit instantiation
5384 // has no effect on semantics, we go on to put its syntax in the AST.
5385
5386 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5387 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005388 // Since the only prior class template specialization with these
5389 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005390 // declaration node as our own, updating the source location
5391 // for the template name to reflect our new declaration.
5392 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005393 Specialization = PrevDecl;
5394 Specialization->setLocation(TemplateNameLoc);
5395 PrevDecl = 0;
5396 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005397 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005398
Douglas Gregor52604ab2009-09-11 21:19:12 +00005399 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005400 // Create a new class template specialization declaration node for
5401 // this explicit specialization.
5402 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005403 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005404 ClassTemplate->getDeclContext(),
5405 TemplateNameLoc,
5406 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005407 Converted.data(),
5408 Converted.size(),
5409 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005410 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005411
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005412 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005413 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005414 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005415 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005416 }
5417
5418 // Build the fully-sugared type for this explicit instantiation as
5419 // the user wrote in the explicit instantiation itself. This means
5420 // that we'll pretty-print the type retrieved from the
5421 // specialization's declaration the way that the user actually wrote
5422 // the explicit instantiation, rather than formatting the name based
5423 // on the "canonical" representation used to store the template
5424 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005425 TypeSourceInfo *WrittenTy
5426 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5427 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005428 Context.getTypeDeclType(Specialization));
5429 Specialization->setTypeAsWritten(WrittenTy);
5430 TemplateArgsIn.release();
5431
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005432 // Set source locations for keywords.
5433 Specialization->setExternLoc(ExternLoc);
5434 Specialization->setTemplateKeywordLoc(TemplateLoc);
5435
5436 // Add the explicit instantiation into its lexical context. However,
5437 // since explicit instantiations are never found by name lookup, we
5438 // just put it into the declaration context directly.
5439 Specialization->setLexicalDeclContext(CurContext);
5440 CurContext->addDecl(Specialization);
5441
5442 // Syntax is now OK, so return if it has no other effect on semantics.
5443 if (HasNoEffect) {
5444 // Set the template specialization kind.
5445 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005446 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005447 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005448
5449 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005450 // A definition of a class template or class member template
5451 // shall be in scope at the point of the explicit instantiation of
5452 // the class template or class member template.
5453 //
5454 // This check comes when we actually try to perform the
5455 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005456 ClassTemplateSpecializationDecl *Def
5457 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005458 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005459 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005460 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005461 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005462 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005463 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5464 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005465
Douglas Gregor0d035142009-10-27 18:42:08 +00005466 // Instantiate the members of this class template specialization.
5467 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005468 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005469 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005470 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5471
5472 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5473 // TSK_ExplicitInstantiationDefinition
5474 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5475 TSK == TSK_ExplicitInstantiationDefinition)
5476 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005477
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005478 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005479 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005480
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005481 // Set the template specialization kind.
5482 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005483 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005484}
5485
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005486// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005487DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005488Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005489 SourceLocation ExternLoc,
5490 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005491 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005492 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005493 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005494 IdentifierInfo *Name,
5495 SourceLocation NameLoc,
5496 AttributeList *Attr) {
5497
Douglas Gregor402abb52009-05-28 23:31:59 +00005498 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005499 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005500 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005501 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5502 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005503 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005504 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005505 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5506
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005507 if (!TagD)
5508 return true;
5509
John McCalld226f652010-08-21 09:40:31 +00005510 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005511 if (Tag->isEnum()) {
5512 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5513 << Context.getTypeDeclType(Tag);
5514 return true;
5515 }
5516
Douglas Gregord0c87372009-05-27 17:30:49 +00005517 if (Tag->isInvalidDecl())
5518 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005519
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005520 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5521 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5522 if (!Pattern) {
5523 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5524 << Context.getTypeDeclType(Record);
5525 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5526 return true;
5527 }
5528
Douglas Gregor558c0322009-10-14 23:41:34 +00005529 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005530 // If the explicit instantiation is for a class or member class, the
5531 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005532 // simple-template-id.
5533 //
5534 // C++98 has the same restriction, just worded differently.
5535 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005536 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005537 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005538
Douglas Gregor558c0322009-10-14 23:41:34 +00005539 // C++0x [temp.explicit]p2:
5540 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005541 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005542 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005543 TemplateSpecializationKind TSK
5544 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5545 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005546
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005547 // C++0x [temp.explicit]p2:
5548 // [...] An explicit instantiation shall appear in an enclosing
5549 // namespace of its template. [...]
5550 //
5551 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005552 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005553
Douglas Gregor454885e2009-10-15 15:54:05 +00005554 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005555 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005556 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005557 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005558 PrevDecl = Record;
5559 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005560 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005561 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005562 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005563 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005564 PrevDecl,
5565 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005566 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005567 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005568 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005569 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005570 return TagD;
5571 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005572
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005573 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005574 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005575 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005576 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005577 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005578 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005579 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005580 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005581 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005582 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5583 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005584 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5585 << Pattern;
5586 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005587 } else {
5588 if (InstantiateClass(NameLoc, Record, Def,
5589 getTemplateInstantiationArgs(Record),
5590 TSK))
5591 return true;
5592
Douglas Gregor952b0172010-02-11 01:04:33 +00005593 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005594 if (!RecordDef)
5595 return true;
5596 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005597 }
5598
Douglas Gregor0d035142009-10-27 18:42:08 +00005599 // Instantiate all of the members of the class.
5600 InstantiateClassMembers(NameLoc, RecordDef,
5601 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005602
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005603 if (TSK == TSK_ExplicitInstantiationDefinition)
5604 MarkVTableUsed(NameLoc, RecordDef, true);
5605
Mike Stump390b4cc2009-05-16 07:39:55 +00005606 // FIXME: We don't have any representation for explicit instantiations of
5607 // member classes. Such a representation is not needed for compilation, but it
5608 // should be available for clients that want to see all of the declarations in
5609 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005610 return TagD;
5611}
5612
John McCallf312b1e2010-08-26 23:41:50 +00005613DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5614 SourceLocation ExternLoc,
5615 SourceLocation TemplateLoc,
5616 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005617 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005618 // TODO: check if/when DNInfo should replace Name.
5619 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5620 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005621 if (!Name) {
5622 if (!D.isInvalidType())
5623 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5624 diag::err_explicit_instantiation_requires_name)
5625 << D.getDeclSpec().getSourceRange()
5626 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005627
Douglas Gregord5a423b2009-09-25 18:43:00 +00005628 return true;
5629 }
5630
5631 // The scope passed in may not be a decl scope. Zip up the scope tree until
5632 // we find one that is.
5633 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5634 (S->getFlags() & Scope::TemplateParamScope) != 0)
5635 S = S->getParent();
5636
5637 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005638 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5639 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005640 if (R.isNull())
5641 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005642
Douglas Gregord5a423b2009-09-25 18:43:00 +00005643 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5644 // Cannot explicitly instantiate a typedef.
5645 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5646 << Name;
5647 return true;
5648 }
5649
Douglas Gregor663b5a02009-10-14 20:14:33 +00005650 // C++0x [temp.explicit]p1:
5651 // [...] An explicit instantiation of a function template shall not use the
5652 // inline or constexpr specifiers.
5653 // Presumably, this also applies to member functions of class templates as
5654 // well.
5655 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005656 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005657 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005658 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005659
Douglas Gregor663b5a02009-10-14 20:14:33 +00005660 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005661
Douglas Gregor558c0322009-10-14 23:41:34 +00005662 // C++0x [temp.explicit]p2:
5663 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005664 // definition and an explicit instantiation declaration. An explicit
5665 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005666 TemplateSpecializationKind TSK
5667 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5668 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005669
Abramo Bagnara25777432010-08-11 22:01:17 +00005670 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005671 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005672
5673 if (!R->isFunctionType()) {
5674 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005675 // A [...] static data member of a class template can be explicitly
5676 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005677 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005678 if (Previous.isAmbiguous())
5679 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005680
John McCall1bcee0a2009-12-02 08:25:40 +00005681 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005682 if (!Prev || !Prev->isStaticDataMember()) {
5683 // We expect to see a data data member here.
5684 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5685 << Name;
5686 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5687 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005688 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005689 return true;
5690 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005691
Douglas Gregord5a423b2009-09-25 18:43:00 +00005692 if (!Prev->getInstantiatedFromStaticDataMember()) {
5693 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005694 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005695 diag::err_explicit_instantiation_data_member_not_instantiated)
5696 << Prev;
5697 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5698 // FIXME: Can we provide a note showing where this was declared?
5699 return true;
5700 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005701
Douglas Gregor558c0322009-10-14 23:41:34 +00005702 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005703 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005704 // or a static data member of a class template specialization, the name of
5705 // the class template specialization in the qualified-id for the member
5706 // name shall be a simple-template-id.
5707 //
5708 // C++98 has the same restriction, just worded differently.
5709 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005710 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005711 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005712 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005713
Douglas Gregor558c0322009-10-14 23:41:34 +00005714 // Check the scope of this explicit instantiation.
5715 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005716
Douglas Gregor454885e2009-10-15 15:54:05 +00005717 // Verify that it is okay to explicitly instantiate here.
5718 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5719 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005720 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005721 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005722 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005723 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005724 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005725 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005726 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005727 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005728
Douglas Gregord5a423b2009-09-25 18:43:00 +00005729 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005730 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005731 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005732 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005733
Douglas Gregord5a423b2009-09-25 18:43:00 +00005734 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005735 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005736 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005737
5738 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005739 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005740 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005741 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005742 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5743 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005744 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5745 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005746 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5747 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005748 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005749 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005750 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005751 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005752 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005753
Douglas Gregord5a423b2009-09-25 18:43:00 +00005754 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005755 // A [...] function [...] can be explicitly instantiated from its template.
5756 // A member function [...] of a class template can be explicitly
5757 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005758 // template.
John McCallc373d482010-01-27 01:50:18 +00005759 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005760 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5761 P != PEnd; ++P) {
5762 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005763 if (!HasExplicitTemplateArgs) {
5764 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5765 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5766 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005767
John McCallc373d482010-01-27 01:50:18 +00005768 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005769 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5770 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005771 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005772 }
5773 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005774
Douglas Gregord5a423b2009-09-25 18:43:00 +00005775 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5776 if (!FunTmpl)
5777 continue;
5778
John McCall5769d612010-02-08 23:07:23 +00005779 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005780 FunctionDecl *Specialization = 0;
5781 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005782 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005783 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005784 R, Specialization, Info)) {
5785 // FIXME: Keep track of almost-matches?
5786 (void)TDK;
5787 continue;
5788 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005789
John McCallc373d482010-01-27 01:50:18 +00005790 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005791 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005792
Douglas Gregord5a423b2009-09-25 18:43:00 +00005793 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005794 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005795 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005796 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005797 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5798 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5799 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005800
John McCallc373d482010-01-27 01:50:18 +00005801 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005802 return true;
John McCallc373d482010-01-27 01:50:18 +00005803
5804 // Ignore access control bits, we don't need them for redeclaration checking.
5805 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005806
Douglas Gregor0a897e32009-10-15 17:21:20 +00005807 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005808 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005809 diag::err_explicit_instantiation_member_function_not_instantiated)
5810 << Specialization
5811 << (Specialization->getTemplateSpecializationKind() ==
5812 TSK_ExplicitSpecialization);
5813 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5814 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005815 }
5816
Douglas Gregor0a897e32009-10-15 17:21:20 +00005817 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005818 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5819 PrevDecl = Specialization;
5820
Douglas Gregor0a897e32009-10-15 17:21:20 +00005821 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005822 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005823 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005824 PrevDecl,
5825 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005826 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005827 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005828 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005829
Douglas Gregor0a897e32009-10-15 17:21:20 +00005830 // FIXME: We may still want to build some representation of this
5831 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005832 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005833 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005834 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005835
5836 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005837
Douglas Gregor0a897e32009-10-15 17:21:20 +00005838 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005839 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005840
Douglas Gregor558c0322009-10-14 23:41:34 +00005841 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005842 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005843 // or a static data member of a class template specialization, the name of
5844 // the class template specialization in the qualified-id for the member
5845 // name shall be a simple-template-id.
5846 //
5847 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005848 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005849 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005850 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005851 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005852 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005853 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005854 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005855
Douglas Gregor558c0322009-10-14 23:41:34 +00005856 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005857 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005858 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005859 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005860 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005861
Douglas Gregord5a423b2009-09-25 18:43:00 +00005862 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005863 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005864}
5865
John McCallf312b1e2010-08-26 23:41:50 +00005866TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005867Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5868 const CXXScopeSpec &SS, IdentifierInfo *Name,
5869 SourceLocation TagLoc, SourceLocation NameLoc) {
5870 // This has to hold, because SS is expected to be defined.
5871 assert(Name && "Expected a name in a dependent tag");
5872
5873 NestedNameSpecifier *NNS
5874 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5875 if (!NNS)
5876 return true;
5877
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005878 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005879
Douglas Gregor48c89f42010-04-24 16:38:41 +00005880 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
5881 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005882 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00005883 return true;
5884 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005885
5886 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
John McCallb3d87482010-08-24 05:47:05 +00005887 return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name));
John McCallc4e70192009-09-11 04:59:25 +00005888}
5889
John McCallf312b1e2010-08-26 23:41:50 +00005890TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005891Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5892 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005893 SourceLocation IdLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005894 NestedNameSpecifier *NNS
Douglas Gregord57959a2009-03-27 23:10:48 +00005895 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5896 if (!NNS)
5897 return true;
5898
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005899 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5900 !getLangOptions().CPlusPlus0x)
5901 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005902 << FixItHint::CreateRemoval(TypenameLoc);
5903
Douglas Gregor107de902010-04-24 15:35:55 +00005904 QualType T = CheckTypenameType(ETK_Typename, NNS, II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005905 TypenameLoc, SS.getRange(), IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00005906 if (T.isNull())
5907 return true;
John McCall63b43852010-04-29 23:50:39 +00005908
5909 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
5910 if (isa<DependentNameType>(T)) {
5911 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005912 TL.setKeywordLoc(TypenameLoc);
5913 TL.setQualifierRange(SS.getRange());
5914 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005915 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005916 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005917 TL.setKeywordLoc(TypenameLoc);
5918 TL.setQualifierRange(SS.getRange());
5919 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005920 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005921
John McCallb3d87482010-08-24 05:47:05 +00005922 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00005923}
5924
John McCallf312b1e2010-08-26 23:41:50 +00005925TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00005926Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5927 const CXXScopeSpec &SS,
5928 SourceLocation TemplateLoc,
5929 TemplateTy TemplateIn,
5930 SourceLocation TemplateNameLoc,
5931 SourceLocation LAngleLoc,
5932 ASTTemplateArgsPtr TemplateArgsIn,
5933 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005934 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5935 !getLangOptions().CPlusPlus0x)
5936 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00005937 << FixItHint::CreateRemoval(TypenameLoc);
5938
5939 // Translate the parser's template argument list in our AST format.
5940 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
5941 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
5942
5943 TemplateName Template = TemplateIn.get();
5944
Douglas Gregor6946baf2009-09-02 13:05:45 +00005945 if (computeDeclContext(SS, false)) {
5946 // If we can compute a declaration context, then the "typename"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005947 // keyword was superfluous. Just build an ElaboratedType to keep
Douglas Gregor6946baf2009-09-02 13:05:45 +00005948 // track of the nested-name-specifier.
Douglas Gregora02411e2011-02-27 22:46:49 +00005949
5950 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
5951 if (T.isNull())
5952 return true;
5953
5954 // Provide source-location information for the template specialization
5955 // type.
John McCall4e449832010-05-28 23:32:21 +00005956 TypeLocBuilder Builder;
Douglas Gregora02411e2011-02-27 22:46:49 +00005957 TemplateSpecializationTypeLoc SpecTL
5958 = Builder.push<TemplateSpecializationTypeLoc>(T);
5959
5960 // FIXME: No place to set the location of the 'template' keyword!
5961 SpecTL.setLAngleLoc(LAngleLoc);
5962 SpecTL.setRAngleLoc(RAngleLoc);
5963 SpecTL.setTemplateNameLoc(TemplateNameLoc);
5964 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
5965 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
5966
5967 // FIXME: This is a hack. We really want to push the nested-name-specifier
5968 // into TemplateSpecializationType.
5969
Abramo Bagnara22f638a2010-08-10 13:46:45 +00005970 /* Note: NNS already embedded in template specialization type T. */
5971 T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T);
John McCall4e449832010-05-28 23:32:21 +00005972 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
5973 TL.setKeywordLoc(TypenameLoc);
5974 TL.setQualifierRange(SS.getRange());
Douglas Gregora02411e2011-02-27 22:46:49 +00005975
John McCall4e449832010-05-28 23:32:21 +00005976 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
John McCallb3d87482010-08-24 05:47:05 +00005977 return CreateParsedType(T, TSI);
Douglas Gregor6946baf2009-09-02 13:05:45 +00005978 }
Douglas Gregora02411e2011-02-27 22:46:49 +00005979
5980 // Construct a dependent template specialization type.
5981 DependentTemplateName *DTN = Template.getAsDependentTemplateName();
John McCall33500952010-06-11 00:33:02 +00005982 assert(DTN && "dependent template has non-dependent name?");
Abramo Bagnara22f638a2010-08-10 13:46:45 +00005983 assert(DTN->getQualifier()
5984 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
Douglas Gregora02411e2011-02-27 22:46:49 +00005985 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
5986 DTN->getQualifier(),
5987 DTN->getIdentifier(),
5988 TemplateArgs);
5989
5990 // Create source-location information for this type.
5991 TypeLocBuilder Builder;
5992 DependentTemplateSpecializationTypeLoc SpecTL
5993 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
5994 SpecTL.setLAngleLoc(LAngleLoc);
5995 SpecTL.setRAngleLoc(RAngleLoc);
5996 SpecTL.setKeywordLoc(TypenameLoc);
5997 SpecTL.setNameLoc(TemplateNameLoc);
5998 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
5999 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6000
6001 // FIXME: Nested-name-specifier source locations.
6002 SpecTL.setQualifierRange(SS.getRange());
6003 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor17343172009-04-01 00:28:59 +00006004}
6005
Douglas Gregora02411e2011-02-27 22:46:49 +00006006
Douglas Gregord57959a2009-03-27 23:10:48 +00006007/// \brief Build the type that describes a C++ typename specifier,
6008/// e.g., "typename T::type".
6009QualType
Douglas Gregor107de902010-04-24 15:35:55 +00006010Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6011 NestedNameSpecifier *NNS, const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006012 SourceLocation KeywordLoc, SourceRange NNSRange,
6013 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006014 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00006015 SS.MakeTrivial(Context, NNS, NNSRange);
Douglas Gregord57959a2009-03-27 23:10:48 +00006016
John McCall77bb1aa2010-05-01 00:40:08 +00006017 DeclContext *Ctx = computeDeclContext(SS);
6018 if (!Ctx) {
6019 // If the nested-name-specifier is dependent and couldn't be
6020 // resolved to a type, build a typename type.
6021 assert(NNS->isDependent());
6022 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006023 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006024
John McCall77bb1aa2010-05-01 00:40:08 +00006025 // If the nested-name-specifier refers to the current instantiation,
6026 // the "typename" keyword itself is superfluous. In C++03, the
6027 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6028 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006029 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006030
John McCall77bb1aa2010-05-01 00:40:08 +00006031 if (RequireCompleteDeclContext(SS, Ctx))
6032 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006033
6034 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006035 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006036 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006037 unsigned DiagID = 0;
6038 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006039 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006040 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006041 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006042 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006043
6044 case LookupResult::FoundUnresolvedValue: {
6045 // We found a using declaration that is a value. Most likely, the using
6046 // declaration itself is meant to have the 'typename' keyword.
6047 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6048 IILoc);
6049 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6050 << Name << Ctx << FullRange;
6051 if (UnresolvedUsingValueDecl *Using
6052 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006053 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006054 Diag(Loc, diag::note_using_value_decl_missing_typename)
6055 << FixItHint::CreateInsertion(Loc, "typename ");
6056 }
6057 }
6058 // Fall through to create a dependent typename type, from which we can recover
6059 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006060
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006061 case LookupResult::NotFoundInCurrentInstantiation:
6062 // Okay, it's a member of an unknown instantiation.
Douglas Gregor107de902010-04-24 15:35:55 +00006063 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006064
6065 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006066 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006067 // We found a type. Build an ElaboratedType, since the
6068 // typename-specifier was just sugar.
6069 return Context.getElaboratedType(ETK_Typename, NNS,
6070 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006071 }
6072
6073 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006074 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006075 break;
6076
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006077
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006078 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006079 return QualType();
6080
Douglas Gregord57959a2009-03-27 23:10:48 +00006081 case LookupResult::FoundOverloaded:
6082 DiagID = diag::err_typename_nested_not_type;
6083 Referenced = *Result.begin();
6084 break;
6085
John McCall6e247262009-10-10 05:48:19 +00006086 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006087 return QualType();
6088 }
6089
6090 // If we get here, it's because name lookup did not find a
6091 // type. Emit an appropriate diagnostic and return an error.
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006092 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6093 IILoc);
6094 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006095 if (Referenced)
6096 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6097 << Name;
6098 return QualType();
6099}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006100
6101namespace {
6102 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006103 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006104 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006105 SourceLocation Loc;
6106 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006107
Douglas Gregor4a959d82009-08-06 16:20:37 +00006108 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006109 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006110
Mike Stump1eb44332009-09-09 15:08:12 +00006111 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006112 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006113 DeclarationName Entity)
6114 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006115 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006116
6117 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006118 /// transformed.
6119 ///
6120 /// For the purposes of type reconstruction, a type has already been
6121 /// transformed if it is NULL or if it is not dependent.
6122 bool AlreadyTransformed(QualType T) {
6123 return T.isNull() || !T->isDependentType();
6124 }
Mike Stump1eb44332009-09-09 15:08:12 +00006125
6126 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006127 /// rebuilt.
6128 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006129
Douglas Gregor4a959d82009-08-06 16:20:37 +00006130 /// \brief Returns the name of the entity whose type is being rebuilt.
6131 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006132
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006133 /// \brief Sets the "base" location and entity when that
6134 /// information is known based on another transformation.
6135 void setBase(SourceLocation Loc, DeclarationName Entity) {
6136 this->Loc = Loc;
6137 this->Entity = Entity;
6138 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006139 };
6140}
6141
Douglas Gregor4a959d82009-08-06 16:20:37 +00006142/// \brief Rebuilds a type within the context of the current instantiation.
6143///
Mike Stump1eb44332009-09-09 15:08:12 +00006144/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006145/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006146/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006147/// partial specialization thereof). This routine will rebuild that type now
6148/// that we have entered the declarator's scope, which may produce different
6149/// canonical types, e.g.,
6150///
6151/// \code
6152/// template<typename T>
6153/// struct X {
6154/// typedef T* pointer;
6155/// pointer data();
6156/// };
6157///
6158/// template<typename T>
6159/// typename X<T>::pointer X<T>::data() { ... }
6160/// \endcode
6161///
Douglas Gregor4714c122010-03-31 17:34:00 +00006162/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006163/// since we do not know that we can look into X<T> when we parsed the type.
6164/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006165/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006166/// as the canonical type of T*, allowing the return types of the out-of-line
6167/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006168TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6169 SourceLocation Loc,
6170 DeclarationName Name) {
6171 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006172 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006173
Douglas Gregor4a959d82009-08-06 16:20:37 +00006174 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6175 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006176}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006177
John McCall60d7b3a2010-08-24 06:29:42 +00006178ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006179 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6180 DeclarationName());
6181 return Rebuilder.TransformExpr(E);
6182}
6183
John McCall63b43852010-04-29 23:50:39 +00006184bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006185 if (SS.isInvalid())
6186 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006187
Douglas Gregor7e384942011-02-25 16:07:42 +00006188 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006189 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6190 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006191 NestedNameSpecifierLoc Rebuilt
6192 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6193 if (!Rebuilt)
6194 return true;
John McCall63b43852010-04-29 23:50:39 +00006195
Douglas Gregor7e384942011-02-25 16:07:42 +00006196 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006197 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006198}
6199
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006200/// \brief Produces a formatted string that describes the binding of
6201/// template parameters to template arguments.
6202std::string
6203Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6204 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006205 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006206}
6207
6208std::string
6209Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6210 const TemplateArgument *Args,
6211 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006212 llvm::SmallString<128> Str;
6213 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006214
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006215 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006216 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006217
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006218 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006219 if (I >= NumArgs)
6220 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006221
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006222 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006223 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006224 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006225 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006226
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006227 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006228 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006229 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006230 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006231 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006232
Douglas Gregor87dd6972010-12-20 16:52:59 +00006233 Out << " = ";
6234 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006235 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006236
6237 Out << ']';
6238 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006239}