blob: 7a4e54de0f7e7d43dbce33e7ca5ac0cf25eb8da0 [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
Douglas Gregor312eadb2011-04-24 05:37:28 +000079void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
Douglas Gregor312eadb2011-04-24 05:37:28 +000085 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
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 Gregor312eadb2011-04-24 05:37:28 +0000117bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
118 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
119 if (isAcceptableTemplateName(Context, *I))
120 return true;
121
Douglas Gregor3b887352011-04-27 04:48:22 +0000122 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000123}
124
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000125TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000126 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000127 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000128 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000129 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000130 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000131 TemplateTy &TemplateResult,
132 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000133 assert(getLangOptions().CPlusPlus && "No template names in C!");
134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000136 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000137
Douglas Gregor014e88d2009-11-03 23:16:33 +0000138 switch (Name.getKind()) {
139 case UnqualifiedId::IK_Identifier:
140 TName = DeclarationName(Name.Identifier);
141 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000142
Douglas Gregor014e88d2009-11-03 23:16:33 +0000143 case UnqualifiedId::IK_OperatorFunctionId:
144 TName = Context.DeclarationNames.getCXXOperatorName(
145 Name.OperatorFunctionId.Operator);
146 break;
147
Sean Hunte6252d12009-11-28 08:58:14 +0000148 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000149 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
150 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000151
Douglas Gregor014e88d2009-11-03 23:16:33 +0000152 default:
153 return TNK_Non_template;
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
John McCallb3d87482010-08-24 05:47:05 +0000156 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000157
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000158 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000159 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000160 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
161 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000162 if (R.empty()) return TNK_Non_template;
163 if (R.isAmbiguous()) {
164 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000165 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000166
167 // FIXME: we might have ambiguous templates, in which case we
168 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000169 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000170 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000171
John McCall0bd6feb2009-12-02 08:04:21 +0000172 TemplateName Template;
173 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
John McCall0bd6feb2009-12-02 08:04:21 +0000175 unsigned ResultCount = R.end() - R.begin();
176 if (ResultCount > 1) {
177 // We assume that we'll preserve the qualifier from a function
178 // template name in other ways.
179 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
180 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000181
182 // We'll do this lookup again later.
183 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000184 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000185 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
186
187 if (SS.isSet() && !SS.isInvalid()) {
188 NestedNameSpecifier *Qualifier
189 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000190 Template = Context.getQualifiedTemplateName(Qualifier,
191 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000192 } else {
193 Template = TemplateName(TD);
194 }
195
John McCallb8592062010-08-13 02:23:42 +0000196 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000197 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000198
199 // We'll do this lookup again later.
200 R.suppressDiagnostics();
201 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000202 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
203 TemplateKind = TNK_Type_template;
204 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
John McCall0bd6feb2009-12-02 08:04:21 +0000207 TemplateResult = TemplateTy::make(Template);
208 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000209}
210
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000211bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000212 SourceLocation IILoc,
213 Scope *S,
214 const CXXScopeSpec *SS,
215 TemplateTy &SuggestedTemplate,
216 TemplateNameKind &SuggestedKind) {
217 // We can't recover unless there's a dependent scope specifier preceding the
218 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000219 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000220 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
221 computeDeclContext(*SS))
222 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000223
Douglas Gregor84d0a192010-01-12 21:28:44 +0000224 // The code is missing a 'template' keyword prior to the dependent template
225 // name.
226 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
227 Diag(IILoc, diag::err_template_kw_missing)
228 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000229 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000230 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000231 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
232 SuggestedKind = TNK_Dependent_template_name;
233 return true;
234}
235
John McCallf7a1a742009-11-24 19:00:30 +0000236void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000237 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000238 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000239 bool EnteringContext,
240 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000241 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000242 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000243 DeclContext *LookupCtx = 0;
244 bool isDependent = false;
245 if (!ObjectType.isNull()) {
246 // This nested-name-specifier occurs in a member access expression, e.g.,
247 // x->B::f, and we are looking into the type of the object.
248 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
249 LookupCtx = computeDeclContext(ObjectType);
250 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000251 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000252 "Caller should have completed object type");
253 } else if (SS.isSet()) {
254 // This nested-name-specifier occurs after another nested-name-specifier,
255 // so long into the context associated with the prior nested-name-specifier.
256 LookupCtx = computeDeclContext(SS, EnteringContext);
257 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000258
John McCallf7a1a742009-11-24 19:00:30 +0000259 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000260 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000261 return;
262 }
263
264 bool ObjectTypeSearchedInScope = false;
265 if (LookupCtx) {
266 // Perform "qualified" name lookup into the declaration context we
267 // computed, which is either the type of the base of a member access
268 // expression or the declaration context associated with a prior
269 // nested-name-specifier.
270 LookupQualifiedName(Found, LookupCtx);
271
272 if (!ObjectType.isNull() && Found.empty()) {
273 // C++ [basic.lookup.classref]p1:
274 // In a class member access expression (5.2.5), if the . or -> token is
275 // immediately followed by an identifier followed by a <, the
276 // identifier must be looked up to determine whether the < is the
277 // beginning of a template argument list (14.2) or a less-than operator.
278 // The identifier is first looked up in the class of the object
279 // expression. If the identifier is not found, it is then looked up in
280 // the context of the entire postfix-expression and shall name a class
281 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000282 if (S) LookupName(Found, S);
283 ObjectTypeSearchedInScope = true;
284 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000285 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000286 // We cannot look into a dependent object type or nested nme
287 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000288 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000289 return;
290 } else {
291 // Perform unqualified name lookup in the current scope.
292 LookupName(Found, S);
293 }
294
Douglas Gregor2e933882010-01-12 17:06:20 +0000295 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000296 // If we did not find any names, attempt to correct any typos.
297 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000298 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000299 false, CTC_CXXCasts)) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000300 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000301 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000302 if (LookupCtx)
303 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
304 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000305 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000306 Found.getLookupName().getAsString());
307 else
308 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
309 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000310 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000311 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000312 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
313 Diag(Template->getLocation(), diag::note_previous_decl)
314 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000315 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000316 } else {
317 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000318 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000319 }
320 }
321
Douglas Gregor312eadb2011-04-24 05:37:28 +0000322 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000323 if (Found.empty()) {
324 if (isDependent)
325 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000326 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000327 }
John McCallf7a1a742009-11-24 19:00:30 +0000328
329 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
330 // C++ [basic.lookup.classref]p1:
331 // [...] If the lookup in the class of the object expression finds a
332 // template, the name is also looked up in the context of the entire
333 // postfix-expression and [...]
334 //
335 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
336 LookupOrdinaryName);
337 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000338 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000339
John McCallf7a1a742009-11-24 19:00:30 +0000340 if (FoundOuter.empty()) {
341 // - if the name is not found, the name found in the class of the
342 // object expression is used, otherwise
343 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
344 // - if the name is found in the context of the entire
345 // postfix-expression and does not name a class template, the name
346 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000347 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000348 // - if the name found is a class template, it must refer to the same
349 // entity as the one found in the class of the object expression,
350 // otherwise the program is ill-formed.
351 if (!Found.isSingleResult() ||
352 Found.getFoundDecl()->getCanonicalDecl()
353 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000354 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000355 diag::ext_nested_name_member_ref_lookup_ambiguous)
356 << Found.getLookupName()
357 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000358 Diag(Found.getRepresentativeDecl()->getLocation(),
359 diag::note_ambig_member_ref_object_type)
360 << ObjectType;
361 Diag(FoundOuter.getFoundDecl()->getLocation(),
362 diag::note_ambig_member_ref_scope);
363
364 // Recover by taking the template that we found in the object
365 // expression's type.
366 }
367 }
368 }
369}
370
John McCall2f841ba2009-12-02 03:53:29 +0000371/// ActOnDependentIdExpression - Handle a dependent id-expression that
372/// was just parsed. This is only possible with an explicit scope
373/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000374ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000375Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000376 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000377 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000378 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000379 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000380
John McCall2f841ba2009-12-02 03:53:29 +0000381 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000382 isa<CXXMethodDecl>(DC) &&
383 cast<CXXMethodDecl>(DC)->isInstance()) {
384 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000385
John McCallf7a1a742009-11-24 19:00:30 +0000386 // Since the 'this' expression is synthesized, we don't need to
387 // perform the double-lookup check.
388 NamedDecl *FirstQualifierInScope = 0;
389
John McCallaa81e162009-12-01 22:10:20 +0000390 return Owned(CXXDependentScopeMemberExpr::Create(Context,
391 /*This*/ 0, ThisType,
392 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000393 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000394 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000395 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000396 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000397 TemplateArgs));
398 }
399
Abramo Bagnara25777432010-08-11 22:01:17 +0000400 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000401}
402
John McCall60d7b3a2010-08-24 06:29:42 +0000403ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000404Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000405 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000406 const TemplateArgumentListInfo *TemplateArgs) {
407 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000408 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000409 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000410 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000411}
412
Douglas Gregor72c3f312008-12-05 18:15:24 +0000413/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
414/// that the template parameter 'PrevDecl' is being shadowed by a new
415/// declaration at location Loc. Returns true to indicate that this is
416/// an error, and false otherwise.
417bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000418 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000419
420 // Microsoft Visual C++ permits template parameters to be shadowed.
421 if (getLangOptions().Microsoft)
422 return false;
423
424 // C++ [temp.local]p4:
425 // A template-parameter shall not be redeclared within its
426 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000427 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000428 << cast<NamedDecl>(PrevDecl)->getDeclName();
429 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
430 return true;
431}
432
Douglas Gregor2943aed2009-03-03 04:44:36 +0000433/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000434/// the parameter D to reference the templated declaration and return a pointer
435/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000436TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
437 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
438 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000439 return Temp;
440 }
441 return 0;
442}
443
Douglas Gregorba68eca2011-01-05 17:40:24 +0000444ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
445 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000446 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000447 "Only template template arguments can be pack expansions here");
448 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
449 "Template template argument pack expansion without packs");
450 ParsedTemplateArgument Result(*this);
451 Result.EllipsisLoc = EllipsisLoc;
452 return Result;
453}
454
Douglas Gregor788cd062009-11-11 01:00:40 +0000455static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
456 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000457
Douglas Gregor788cd062009-11-11 01:00:40 +0000458 switch (Arg.getKind()) {
459 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000460 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000461 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000462 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000463 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 return TemplateArgumentLoc(TemplateArgument(T), DI);
465 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000466
Douglas Gregor788cd062009-11-11 01:00:40 +0000467 case ParsedTemplateArgument::NonType: {
468 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
469 return TemplateArgumentLoc(TemplateArgument(E), E);
470 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000471
Douglas Gregor788cd062009-11-11 01:00:40 +0000472 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000473 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000474 TemplateArgument TArg;
475 if (Arg.getEllipsisLoc().isValid())
476 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
477 else
478 TArg = Template;
479 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000480 Arg.getScopeSpec().getWithLocInContext(
481 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000482 Arg.getLocation(),
483 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000484 }
485 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000486
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000487 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000488 return TemplateArgumentLoc();
489}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000490
Douglas Gregor788cd062009-11-11 01:00:40 +0000491/// \brief Translates template arguments as provided by the parser
492/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000493void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
494 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000495 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000496 TemplateArgs.addArgument(translateTemplateArgument(*this,
497 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000498}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000499
Douglas Gregor72c3f312008-12-05 18:15:24 +0000500/// ActOnTypeParameter - Called when a C++ template type parameter
501/// (e.g., "typename T") has been parsed. Typename specifies whether
502/// the keyword "typename" was used to declare the type parameter
503/// (otherwise, "class" was used), and KeyLoc is the location of the
504/// "class" or "typename" keyword. ParamName is the name of the
505/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000506/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000507/// If the type parameter has a default argument, it will be added
508/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000509Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
510 SourceLocation EllipsisLoc,
511 SourceLocation KeyLoc,
512 IdentifierInfo *ParamName,
513 SourceLocation ParamNameLoc,
514 unsigned Depth, unsigned Position,
515 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000516 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000517 assert(S->isTemplateParamScope() &&
518 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000519 bool Invalid = false;
520
521 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000522 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000523 LookupOrdinaryName,
524 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000525 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000526 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000527 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000528 }
529
Douglas Gregorddc29e12009-02-06 22:42:48 +0000530 SourceLocation Loc = ParamNameLoc;
531 if (!ParamName)
532 Loc = KeyLoc;
533
Douglas Gregor72c3f312008-12-05 18:15:24 +0000534 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000535 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000536 KeyLoc, Loc, Depth, Position, ParamName,
537 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000538 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000539 if (Invalid)
540 Param->setInvalidDecl();
541
542 if (ParamName) {
543 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000544 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000545 IdResolver.AddDecl(Param);
546 }
547
Douglas Gregor61c4d282011-01-05 15:48:55 +0000548 // C++0x [temp.param]p9:
549 // A default template-argument may be specified for any kind of
550 // template-parameter that is not a template parameter pack.
551 if (DefaultArg && Ellipsis) {
552 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
553 DefaultArg = ParsedType();
554 }
555
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000556 // Handle the default argument, if provided.
557 if (DefaultArg) {
558 TypeSourceInfo *DefaultTInfo;
559 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000560
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000561 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000562
Douglas Gregor6f526752010-12-16 08:48:57 +0000563 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000564 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000565 UPPC_DefaultArgument))
566 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000568 // Check the template argument itself.
569 if (CheckTemplateArgument(Param, DefaultTInfo)) {
570 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000571 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000572 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000573
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000574 Param->setDefaultArgument(DefaultTInfo, false);
575 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000576
John McCalld226f652010-08-21 09:40:31 +0000577 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000578}
579
Douglas Gregor2943aed2009-03-03 04:44:36 +0000580/// \brief Check that the type of a non-type template parameter is
581/// well-formed.
582///
583/// \returns the (possibly-promoted) parameter type if valid;
584/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000585QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000586Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000587 // We don't allow variably-modified types as the type of non-type template
588 // parameters.
589 if (T->isVariablyModifiedType()) {
590 Diag(Loc, diag::err_variably_modified_nontype_template_param)
591 << T;
592 return QualType();
593 }
594
Douglas Gregor2943aed2009-03-03 04:44:36 +0000595 // C++ [temp.param]p4:
596 //
597 // A non-type template-parameter shall have one of the following
598 // (optionally cv-qualified) types:
599 //
600 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000601 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000602 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000603 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000604 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000605 T->isReferenceType() ||
606 // -- pointer to member.
607 T->isMemberPointerType() ||
608 // If T is a dependent type, we can't do the check now, so we
609 // assume that it is well-formed.
610 T->isDependentType())
611 return T;
612 // C++ [temp.param]p8:
613 //
614 // A non-type template-parameter of type "array of T" or
615 // "function returning T" is adjusted to be of type "pointer to
616 // T" or "pointer to function returning T", respectively.
617 else if (T->isArrayType())
618 // FIXME: Keep the type prior to promotion?
619 return Context.getArrayDecayedType(T);
620 else if (T->isFunctionType())
621 // FIXME: Keep the type prior to promotion?
622 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000623
Douglas Gregor2943aed2009-03-03 04:44:36 +0000624 Diag(Loc, diag::err_template_nontype_parm_bad_type)
625 << T;
626
627 return QualType();
628}
629
John McCalld226f652010-08-21 09:40:31 +0000630Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
631 unsigned Depth,
632 unsigned Position,
633 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000634 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000635 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
636 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000637
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000638 assert(S->isTemplateParamScope() &&
639 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000640 bool Invalid = false;
641
642 IdentifierInfo *ParamName = D.getIdentifier();
643 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000644 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000645 LookupOrdinaryName,
646 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000647 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000648 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000649 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000650 }
651
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000652 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
653 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000654 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000655 Invalid = true;
656 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000657
Douglas Gregor10738d32010-12-23 23:51:58 +0000658 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000659 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000660 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000661 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000662 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000663 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000664 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000665 Param->setAccess(AS_public);
666
Douglas Gregor72c3f312008-12-05 18:15:24 +0000667 if (Invalid)
668 Param->setInvalidDecl();
669
670 if (D.getIdentifier()) {
671 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000672 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000673 IdResolver.AddDecl(Param);
674 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000675
Douglas Gregor61c4d282011-01-05 15:48:55 +0000676 // C++0x [temp.param]p9:
677 // A default template-argument may be specified for any kind of
678 // template-parameter that is not a template parameter pack.
679 if (Default && IsParameterPack) {
680 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
681 Default = 0;
682 }
683
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000684 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000685 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000686 // Check for unexpanded parameter packs.
687 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
688 return Param;
689
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000690 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000691 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
692 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000693 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000694 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000695 }
John Wiegley429bb272011-04-08 18:41:53 +0000696 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000697
John McCall9ae2f072010-08-23 23:25:46 +0000698 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000699 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000700
John McCalld226f652010-08-21 09:40:31 +0000701 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000702}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000703
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000704/// ActOnTemplateTemplateParameter - Called when a C++ template template
705/// parameter (e.g. T in template <template <typename> class T> class array)
706/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000707Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
708 SourceLocation TmpLoc,
709 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000710 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000711 IdentifierInfo *Name,
712 SourceLocation NameLoc,
713 unsigned Depth,
714 unsigned Position,
715 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000716 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000717 assert(S->isTemplateParamScope() &&
718 "Template template parameter not in template parameter scope!");
719
720 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000721 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000722 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000723 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000724 NameLoc.isInvalid()? TmpLoc : NameLoc,
725 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000726 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000727 Param->setAccess(AS_public);
728
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000729 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000730 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000731 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000732 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000733 IdResolver.AddDecl(Param);
734 }
735
Douglas Gregor6f526752010-12-16 08:48:57 +0000736 if (Params->size() == 0) {
737 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
738 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
739 Param->setInvalidDecl();
740 }
741
Douglas Gregor61c4d282011-01-05 15:48:55 +0000742 // C++0x [temp.param]p9:
743 // A default template-argument may be specified for any kind of
744 // template-parameter that is not a template parameter pack.
745 if (IsParameterPack && !Default.isInvalid()) {
746 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
747 Default = ParsedTemplateArgument();
748 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000749
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000750 if (!Default.isInvalid()) {
751 // Check only that we have a template template argument. We don't want to
752 // try to check well-formedness now, because our template template parameter
753 // might have dependent types in its template parameters, which we wouldn't
754 // be able to match now.
755 //
756 // If none of the template template parameter's template arguments mention
757 // other template parameters, we could actually perform more checking here.
758 // However, it isn't worth doing.
759 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
760 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
761 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
762 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000763 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000764 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000765
Douglas Gregor6f526752010-12-16 08:48:57 +0000766 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000767 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000768 DefaultArg.getArgument().getAsTemplate(),
769 UPPC_DefaultArgument))
770 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000771
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000772 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000773 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000774
John McCalld226f652010-08-21 09:40:31 +0000775 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000776}
777
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000778/// ActOnTemplateParameterList - Builds a TemplateParameterList that
779/// contains the template parameters in Params/NumParams.
780Sema::TemplateParamsTy *
781Sema::ActOnTemplateParameterList(unsigned Depth,
782 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000783 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000784 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000785 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000786 SourceLocation RAngleLoc) {
787 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000788 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000789
Douglas Gregorddc29e12009-02-06 22:42:48 +0000790 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000791 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000792 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000793}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000794
John McCallb6217662010-03-15 10:12:16 +0000795static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
796 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000797 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000798}
799
John McCallf312b1e2010-08-26 23:41:50 +0000800DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000801Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000802 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000803 IdentifierInfo *Name, SourceLocation NameLoc,
804 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000805 TemplateParameterList *TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000806 AccessSpecifier AS,
807 unsigned NumOuterTemplateParamLists,
808 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000809 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000810 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000811 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000812 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000813
814 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000815 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000816 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000817
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000818 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
819 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000820
821 // There is no such thing as an unnamed class template.
822 if (!Name) {
823 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000824 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000825 }
826
827 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000828 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000829 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000830 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000831 if (SS.isNotEmpty() && !SS.isInvalid()) {
832 SemanticContext = computeDeclContext(SS, true);
833 if (!SemanticContext) {
834 // FIXME: Produce a reasonable diagnostic here
835 return true;
836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
John McCall77bb1aa2010-05-01 00:40:08 +0000838 if (RequireCompleteDeclContext(SS, SemanticContext))
839 return true;
840
John McCalla24dc2e2009-11-17 02:14:36 +0000841 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000842 } else {
843 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000844 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000845 }
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Douglas Gregor57265e32010-04-12 16:00:01 +0000847 if (Previous.isAmbiguous())
848 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000849
Douglas Gregorddc29e12009-02-06 22:42:48 +0000850 NamedDecl *PrevDecl = 0;
851 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000852 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000853
Douglas Gregorddc29e12009-02-06 22:42:48 +0000854 // If there is a previous declaration with the same name, check
855 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000856 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000857 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000858
859 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000860 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000861 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000862 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000863 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
864 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000865 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000866 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
867 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
868 PrevClassTemplate
869 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
870 ->getSpecializedTemplate();
871 }
872 }
873
John McCall65c49462009-12-18 11:25:59 +0000874 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000875 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000876 // [...] When looking for a prior declaration of a class or a function
877 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000878 // function is neither a qualified name nor a template-id, scopes outside
879 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000880 if (!SS.isSet()) {
881 DeclContext *OutermostContext = CurContext;
882 while (!OutermostContext->isFileContext())
883 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000884
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000885 if (PrevDecl &&
886 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
887 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
888 SemanticContext = PrevDecl->getDeclContext();
889 } else {
890 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000891 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000892 // declaration.
893 PrevDecl = PrevClassTemplate = 0;
894 SemanticContext = OutermostContext;
895 }
John McCalle129d442009-12-17 23:21:11 +0000896 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000897
John McCalle129d442009-12-17 23:21:11 +0000898 if (CurContext->isDependentContext()) {
899 // If this is a dependent context, we don't want to link the friend
900 // class template to the template in scope, because that would perform
901 // checking of the template parameter lists that can't be performed
902 // until the outer context is instantiated.
903 PrevDecl = PrevClassTemplate = 0;
904 }
905 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
906 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000907
Douglas Gregorddc29e12009-02-06 22:42:48 +0000908 if (PrevClassTemplate) {
909 // Ensure that the template parameter lists are compatible.
910 if (!TemplateParameterListsAreEqual(TemplateParams,
911 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000912 /*Complain=*/true,
913 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000914 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000915
916 // C++ [temp.class]p4:
917 // In a redeclaration, partial specialization, explicit
918 // specialization or explicit instantiation of a class template,
919 // the class-key shall agree in kind with the original class
920 // template declaration (7.1.5.3).
921 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000922 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000923 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000924 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000925 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000926 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000927 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000928 }
929
Douglas Gregorddc29e12009-02-06 22:42:48 +0000930 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000931 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000932 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000933 Diag(NameLoc, diag::err_redefinition) << Name;
934 Diag(Def->getLocation(), diag::note_previous_definition);
935 // FIXME: Would it make sense to try to "forget" the previous
936 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000937 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000938 }
939 }
940 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
941 // Maybe we will complain about the shadowed template parameter.
942 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
943 // Just pretend that we didn't see the previous declaration.
944 PrevDecl = 0;
945 } else if (PrevDecl) {
946 // C++ [temp]p5:
947 // A class template shall not have the same name as any other
948 // template, class, function, object, enumeration, enumerator,
949 // namespace, or type in the same scope (3.3), except as specified
950 // in (14.5.4).
951 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
952 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000953 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000954 }
955
Douglas Gregord684b002009-02-10 19:49:53 +0000956 // Check the template parameter list of this declaration, possibly
957 // merging in the template parameter list from the previous class
958 // template declaration.
959 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000960 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000961 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000962 SemanticContext->isRecord() &&
963 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000964 ? TPC_ClassTemplateMember
965 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000966 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Douglas Gregor57265e32010-04-12 16:00:01 +0000968 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000969 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000970 // template out-of-line.
971 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
972 !(TUK == TUK_Friend && CurContext->isDependentContext()))
973 Diag(NameLoc, diag::err_member_def_does_not_match)
974 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000975 }
976
Mike Stump1eb44332009-09-09 15:08:12 +0000977 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000978 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000979 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000980 PrevClassTemplate->getTemplatedDecl() : 0,
981 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000982 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000983 if (NumOuterTemplateParamLists > 0)
984 NewClass->setTemplateParameterListsInfo(Context,
985 NumOuterTemplateParamLists,
986 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000987
988 ClassTemplateDecl *NewTemplate
989 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
990 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000991 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000992 NewClass->setDescribedClassTemplate(NewTemplate);
993
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000994 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000995 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000996 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000997 assert(T->isDependentType() && "Class template type is not dependent?");
998 (void)T;
999
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001000 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001001 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001002 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001003 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1004 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001005
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001006 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001007 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001008 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Douglas Gregorddc29e12009-02-06 22:42:48 +00001010 // Set the lexical context of these templates
1011 NewClass->setLexicalDeclContext(CurContext);
1012 NewTemplate->setLexicalDeclContext(CurContext);
1013
John McCall0f434ec2009-07-31 02:45:11 +00001014 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001015 NewClass->startDefinition();
1016
1017 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001018 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001019
John McCall05b23ea2009-09-14 21:59:20 +00001020 if (TUK != TUK_Friend)
1021 PushOnScopeChains(NewTemplate, S);
1022 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001023 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001024 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001025 NewClass->setAccess(PrevClassTemplate->getAccess());
1026 }
John McCall05b23ea2009-09-14 21:59:20 +00001027
Douglas Gregord85bea22009-09-26 06:47:28 +00001028 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1029 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001030
John McCall05b23ea2009-09-14 21:59:20 +00001031 // Friend templates are visible in fairly strange ways.
1032 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001033 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001034 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1035 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1036 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001037 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001038 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001039
Douglas Gregord85bea22009-09-26 06:47:28 +00001040 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1041 NewClass->getLocation(),
1042 NewTemplate,
1043 /*FIXME:*/NewClass->getLocation());
1044 Friend->setAccess(AS_public);
1045 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001046 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001047
Douglas Gregord684b002009-02-10 19:49:53 +00001048 if (Invalid) {
1049 NewTemplate->setInvalidDecl();
1050 NewClass->setInvalidDecl();
1051 }
John McCalld226f652010-08-21 09:40:31 +00001052 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001053}
1054
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001055/// \brief Diagnose the presence of a default template argument on a
1056/// template parameter, which is ill-formed in certain contexts.
1057///
1058/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001059static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001060 Sema::TemplateParamListContext TPC,
1061 SourceLocation ParamLoc,
1062 SourceRange DefArgRange) {
1063 switch (TPC) {
1064 case Sema::TPC_ClassTemplate:
1065 return false;
1066
1067 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001068 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001069 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001070 // A default template-argument shall not be specified in a
1071 // function template declaration or a function template
1072 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001073 // If a friend function template declaration specifies a default
1074 // template-argument, that declaration shall be a definition and shall be
1075 // the only declaration of the function template in the translation unit.
1076 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001077 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001078 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001079 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001080 << DefArgRange;
1081 return false;
1082
1083 case Sema::TPC_ClassTemplateMember:
1084 // C++0x [temp.param]p9:
1085 // A default template-argument shall not be specified in the
1086 // template-parameter-lists of the definition of a member of a
1087 // class template that appears outside of the member's class.
1088 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1089 << DefArgRange;
1090 return true;
1091
1092 case Sema::TPC_FriendFunctionTemplate:
1093 // C++ [temp.param]p9:
1094 // A default template-argument shall not be specified in a
1095 // friend template declaration.
1096 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1097 << DefArgRange;
1098 return true;
1099
1100 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1101 // for friend function templates if there is only a single
1102 // declaration (and it is a definition). Strange!
1103 }
1104
1105 return false;
1106}
1107
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001108/// \brief Check for unexpanded parameter packs within the template parameters
1109/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001110static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1111 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001112 TemplateParameterList *Params = TTP->getTemplateParameters();
1113 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1114 NamedDecl *P = Params->getParam(I);
1115 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001116 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001117 NTTP->getTypeSourceInfo(),
1118 Sema::UPPC_NonTypeTemplateParameterType))
1119 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001120
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001121 continue;
1122 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001123
1124 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001125 = dyn_cast<TemplateTemplateParmDecl>(P))
1126 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1127 return true;
1128 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001129
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001130 return false;
1131}
1132
Douglas Gregord684b002009-02-10 19:49:53 +00001133/// \brief Checks the validity of a template parameter list, possibly
1134/// considering the template parameter list from a previous
1135/// declaration.
1136///
1137/// If an "old" template parameter list is provided, it must be
1138/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1139/// template parameter list.
1140///
1141/// \param NewParams Template parameter list for a new template
1142/// declaration. This template parameter list will be updated with any
1143/// default arguments that are carried through from the previous
1144/// template parameter list.
1145///
1146/// \param OldParams If provided, template parameter list from a
1147/// previous declaration of the same template. Default template
1148/// arguments will be merged from the old template parameter list to
1149/// the new template parameter list.
1150///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001151/// \param TPC Describes the context in which we are checking the given
1152/// template parameter list.
1153///
Douglas Gregord684b002009-02-10 19:49:53 +00001154/// \returns true if an error occurred, false otherwise.
1155bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001156 TemplateParameterList *OldParams,
1157 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001158 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregord684b002009-02-10 19:49:53 +00001160 // C++ [temp.param]p10:
1161 // The set of default template-arguments available for use with a
1162 // template declaration or definition is obtained by merging the
1163 // default arguments from the definition (if in scope) and all
1164 // declarations in scope in the same way default function
1165 // arguments are (8.3.6).
1166 bool SawDefaultArgument = false;
1167 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001168
Anders Carlsson49d25572009-06-12 23:20:15 +00001169 bool SawParameterPack = false;
1170 SourceLocation ParameterPackLoc;
1171
Mike Stump1a35fde2009-02-11 23:03:27 +00001172 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001173 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001174 if (OldParams)
1175 OldParam = OldParams->begin();
1176
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001177 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001178 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1179 NewParamEnd = NewParams->end();
1180 NewParam != NewParamEnd; ++NewParam) {
1181 // Variables used to diagnose redundant default arguments
1182 bool RedundantDefaultArg = false;
1183 SourceLocation OldDefaultLoc;
1184 SourceLocation NewDefaultLoc;
1185
1186 // Variables used to diagnose missing default arguments
1187 bool MissingDefaultArg = false;
1188
Anders Carlsson49d25572009-06-12 23:20:15 +00001189 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001190 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001191 // parameter pack, it shall be the last template parameter.
1192 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001193 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001194 diag::err_template_param_pack_must_be_last_template_parameter);
1195 Invalid = true;
1196 }
1197
Douglas Gregord684b002009-02-10 19:49:53 +00001198 if (TemplateTypeParmDecl *NewTypeParm
1199 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001200 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001201 if (NewTypeParm->hasDefaultArgument() &&
1202 DiagnoseDefaultTemplateArgument(*this, TPC,
1203 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001204 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001205 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001206 NewTypeParm->removeDefaultArgument();
1207
1208 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001209 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001210 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Anders Carlsson49d25572009-06-12 23:20:15 +00001212 if (NewTypeParm->isParameterPack()) {
1213 assert(!NewTypeParm->hasDefaultArgument() &&
1214 "Parameter packs can't have a default argument!");
1215 SawParameterPack = true;
1216 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001217 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001218 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001219 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1220 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1221 SawDefaultArgument = true;
1222 RedundantDefaultArg = true;
1223 PreviousDefaultArgLoc = NewDefaultLoc;
1224 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1225 // Merge the default argument from the old declaration to the
1226 // new declaration.
1227 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001228 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001229 true);
1230 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1231 } else if (NewTypeParm->hasDefaultArgument()) {
1232 SawDefaultArgument = true;
1233 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1234 } else if (SawDefaultArgument)
1235 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001236 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001237 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001238 // Check for unexpanded parameter packs.
1239 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001240 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001241 UPPC_NonTypeTemplateParameterType)) {
1242 Invalid = true;
1243 continue;
1244 }
1245
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001246 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001247 if (NewNonTypeParm->hasDefaultArgument() &&
1248 DiagnoseDefaultTemplateArgument(*this, TPC,
1249 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001250 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001251 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001252 }
1253
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001254 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001255 NonTypeTemplateParmDecl *OldNonTypeParm
1256 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001257 if (NewNonTypeParm->isParameterPack()) {
1258 assert(!NewNonTypeParm->hasDefaultArgument() &&
1259 "Parameter packs can't have a default argument!");
1260 SawParameterPack = true;
1261 ParameterPackLoc = NewNonTypeParm->getLocation();
1262 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001263 NewNonTypeParm->hasDefaultArgument()) {
1264 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1265 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1266 SawDefaultArgument = true;
1267 RedundantDefaultArg = true;
1268 PreviousDefaultArgLoc = NewDefaultLoc;
1269 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1270 // Merge the default argument from the old declaration to the
1271 // new declaration.
1272 SawDefaultArgument = true;
1273 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001274 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001275 // parameter.
1276 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001277 OldNonTypeParm->getDefaultArgument(),
1278 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001279 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1280 } else if (NewNonTypeParm->hasDefaultArgument()) {
1281 SawDefaultArgument = true;
1282 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1283 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001284 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001285 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001286 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001287 TemplateTemplateParmDecl *NewTemplateParm
1288 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001289
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001290 // Check for unexpanded parameter packs, recursively.
1291 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1292 Invalid = true;
1293 continue;
1294 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001295
1296 if (NewTemplateParm->hasDefaultArgument() &&
1297 DiagnoseDefaultTemplateArgument(*this, TPC,
1298 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001299 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001300 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001301
1302 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001303 TemplateTemplateParmDecl *OldTemplateParm
1304 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001305 if (NewTemplateParm->isParameterPack()) {
1306 assert(!NewTemplateParm->hasDefaultArgument() &&
1307 "Parameter packs can't have a default argument!");
1308 SawParameterPack = true;
1309 ParameterPackLoc = NewTemplateParm->getLocation();
1310 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001311 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001312 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1313 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001314 SawDefaultArgument = true;
1315 RedundantDefaultArg = true;
1316 PreviousDefaultArgLoc = NewDefaultLoc;
1317 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1318 // Merge the default argument from the old declaration to the
1319 // new declaration.
1320 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001321 // FIXME: We need to create a new kind of "default argument" expression
1322 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001323 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001324 OldTemplateParm->getDefaultArgument(),
1325 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001326 PreviousDefaultArgLoc
1327 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001328 } else if (NewTemplateParm->hasDefaultArgument()) {
1329 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001330 PreviousDefaultArgLoc
1331 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001332 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001333 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001334 }
1335
1336 if (RedundantDefaultArg) {
1337 // C++ [temp.param]p12:
1338 // A template-parameter shall not be given default arguments
1339 // by two different declarations in the same scope.
1340 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1341 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1342 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001343 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001344 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001345 // If a template-parameter of a class template has a default
1346 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001347 // have a default template-argument supplied or be a template parameter
1348 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001349 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001350 diag::err_template_param_default_arg_missing);
1351 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1352 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001353 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001354 }
1355
1356 // If we have an old template parameter list that we're merging
1357 // in, move on to the next parameter.
1358 if (OldParams)
1359 ++OldParam;
1360 }
1361
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001362 // We were missing some default arguments at the end of the list, so remove
1363 // all of the default arguments.
1364 if (RemoveDefaultArguments) {
1365 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1366 NewParamEnd = NewParams->end();
1367 NewParam != NewParamEnd; ++NewParam) {
1368 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1369 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001370 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001371 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1372 NTTP->removeDefaultArgument();
1373 else
1374 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1375 }
1376 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001377
Douglas Gregord684b002009-02-10 19:49:53 +00001378 return Invalid;
1379}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001380
John McCall4e2cbb22010-10-20 05:44:58 +00001381namespace {
1382
1383/// A class which looks for a use of a certain level of template
1384/// parameter.
1385struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1386 typedef RecursiveASTVisitor<DependencyChecker> super;
1387
1388 unsigned Depth;
1389 bool Match;
1390
1391 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1392 NamedDecl *ND = Params->getParam(0);
1393 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1394 Depth = PD->getDepth();
1395 } else if (NonTypeTemplateParmDecl *PD =
1396 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1397 Depth = PD->getDepth();
1398 } else {
1399 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1400 }
1401 }
1402
1403 bool Matches(unsigned ParmDepth) {
1404 if (ParmDepth >= Depth) {
1405 Match = true;
1406 return true;
1407 }
1408 return false;
1409 }
1410
1411 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1412 return !Matches(T->getDepth());
1413 }
1414
1415 bool TraverseTemplateName(TemplateName N) {
1416 if (TemplateTemplateParmDecl *PD =
1417 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1418 if (Matches(PD->getDepth())) return false;
1419 return super::TraverseTemplateName(N);
1420 }
1421
1422 bool VisitDeclRefExpr(DeclRefExpr *E) {
1423 if (NonTypeTemplateParmDecl *PD =
1424 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1425 if (PD->getDepth() == Depth) {
1426 Match = true;
1427 return false;
1428 }
1429 }
1430 return super::VisitDeclRefExpr(E);
1431 }
1432};
1433}
1434
1435/// Determines whether a template-id depends on the given parameter
1436/// list.
1437static bool
1438DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1439 TemplateParameterList *Params) {
1440 DependencyChecker Checker(Params);
1441 Checker.TraverseType(QualType(TemplateId, 0));
1442 return Checker.Match;
1443}
1444
Mike Stump1eb44332009-09-09 15:08:12 +00001445/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001446/// specifier, returning the template parameter list that applies to the
1447/// name.
1448///
1449/// \param DeclStartLoc the start of the declaration that has a scope
1450/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001451///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001452/// \param SS the scope specifier that will be matched to the given template
1453/// parameter lists. This scope specifier precedes a qualified name that is
1454/// being declared.
1455///
1456/// \param ParamLists the template parameter lists, from the outermost to the
1457/// innermost template parameter lists.
1458///
1459/// \param NumParamLists the number of template parameter lists in ParamLists.
1460///
John McCall77e8b112010-04-13 20:37:33 +00001461/// \param IsFriend Whether to apply the slightly different rules for
1462/// matching template parameters to scope specifiers in friend
1463/// declarations.
1464///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001465/// \param IsExplicitSpecialization will be set true if the entity being
1466/// declared is an explicit specialization, false otherwise.
1467///
Mike Stump1eb44332009-09-09 15:08:12 +00001468/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001469/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001470/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001471/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001472/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001473/// itself a template).
1474TemplateParameterList *
1475Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1476 const CXXScopeSpec &SS,
1477 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001478 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001479 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001480 bool &IsExplicitSpecialization,
1481 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001482 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001483
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001484 // Find the template-ids that occur within the nested-name-specifier. These
1485 // template-ids will match up with the template parameter lists.
1486 llvm::SmallVector<const TemplateSpecializationType *, 4>
1487 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001488 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1489 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001490 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1491 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001492 const Type *T = NNS->getAsType();
1493 if (!T) break;
1494
1495 // C++0x [temp.expl.spec]p17:
1496 // A member or a member template may be nested within many
1497 // enclosing class templates. In an explicit specialization for
1498 // such a member, the member declaration shall be preceded by a
1499 // template<> for each enclosing class template that is
1500 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001501 //
1502 // Following the existing practice of GNU and EDG, we allow a typedef of a
1503 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001504 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1505 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001506
Mike Stump1eb44332009-09-09 15:08:12 +00001507 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001508 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001509 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1510 if (!Template)
1511 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Ted Kremenek6217b802009-07-29 21:53:49 +00001513 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001514 ClassTemplateSpecializationDecl *SpecDecl
1515 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1516 // If the nested name specifier refers to an explicit specialization,
1517 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001518 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1519 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001520 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001521 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001522 }
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001524 TemplateIdsInSpecifier.push_back(SpecType);
1525 }
1526 }
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001528 // Reverse the list of template-ids in the scope specifier, so that we can
1529 // more easily match up the template-ids and the template parameter lists.
1530 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001532 SourceLocation FirstTemplateLoc = DeclStartLoc;
1533 if (NumParamLists)
1534 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001536 // Match the template-ids found in the specifier to the template parameter
1537 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001538 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001539 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001540 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1541 const TemplateSpecializationType *TemplateId
1542 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001543 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001544
1545 // In friend declarations we can have template-ids which don't
1546 // depend on the corresponding template parameter lists. But
1547 // assume that empty parameter lists are supposed to match this
1548 // template-id.
1549 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1550 if (!DependentTemplateId ||
1551 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1552 continue;
1553 }
1554
1555 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001556 // We have a template-id without a corresponding template parameter
1557 // list.
John McCall77e8b112010-04-13 20:37:33 +00001558
1559 // ...which is fine if this is a friend declaration.
1560 if (IsFriend) {
1561 IsExplicitSpecialization = true;
1562 break;
1563 }
1564
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001565 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001566 // FIXME: the location information here isn't great.
1567 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001568 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001569 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001570 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001571 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001572 } else {
1573 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1574 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001575 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001576 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001577 }
1578 return 0;
1579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001581 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001582 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001583 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001584
John McCall31f17ec2010-04-27 00:57:59 +00001585 // Are there cases in (e.g.) friends where this won't match?
1586 if (const InjectedClassNameType *Injected
1587 = TemplateId->getAs<InjectedClassNameType>()) {
1588 CXXRecordDecl *Record = Injected->getDecl();
1589 if (ClassTemplatePartialSpecializationDecl *Partial =
1590 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1591 ExpectedTemplateParams = Partial->getTemplateParameters();
1592 else
1593 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1594 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001595 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001596
John McCall31f17ec2010-04-27 00:57:59 +00001597 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001598 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001599 ExpectedTemplateParams,
1600 true, TPL_TemplateMatch);
1601
John McCall4e2cbb22010-10-20 05:44:58 +00001602 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1603 TPC_ClassTemplateMember);
1604 } else if (ParamLists[ParamIdx]->size() > 0)
1605 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001606 diag::err_template_param_list_matches_nontemplate)
1607 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001608 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001609 else
1610 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001611
1612 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001613 }
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001615 // If there were at least as many template-ids as there were template
1616 // parameter lists, then there are no template parameter lists remaining for
1617 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001618 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001619 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001621 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001622 if (ParamIdx != NumParamLists - 1) {
1623 while (ParamIdx < NumParamLists - 1) {
1624 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1625 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001626 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1627 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001628 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1629 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001630
1631 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1632 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1633 diag::note_explicit_template_spec_does_not_need_header)
1634 << ExplicitSpecializationsInSpecifier.back();
1635 ExplicitSpecializationsInSpecifier.pop_back();
1636 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001637
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001638 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001639 // means that the resulting template declaration can't be instantiated
1640 // properly (we'll end up with dependent nodes when we shouldn't).
1641 if (!isExplicitSpecHeader)
1642 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001643
John McCall4e2cbb22010-10-20 05:44:58 +00001644 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001645 }
1646 }
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001648 // Return the last template parameter list, which corresponds to the
1649 // entity being declared.
1650 return ParamLists[NumParamLists - 1];
1651}
1652
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001653void Sema::NoteAllFoundTemplates(TemplateName Name) {
1654 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1655 Diag(Template->getLocation(), diag::note_template_declared_here)
1656 << (isa<FunctionTemplateDecl>(Template)? 0
1657 : isa<ClassTemplateDecl>(Template)? 1
1658 : 2)
1659 << Template->getDeclName();
1660 return;
1661 }
1662
1663 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1664 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1665 IEnd = OST->end();
1666 I != IEnd; ++I)
1667 Diag((*I)->getLocation(), diag::note_template_declared_here)
1668 << 0 << (*I)->getDeclName();
1669
1670 return;
1671 }
1672}
1673
1674
Douglas Gregor7532dc62009-03-30 22:58:21 +00001675QualType Sema::CheckTemplateIdType(TemplateName Name,
1676 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001677 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001678 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001679 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1680 // We might have a substituted template template parameter pack. If so,
1681 // build a template specialization type for it.
1682 if (Name.getAsSubstTemplateTemplateParmPack())
1683 return Context.getTemplateSpecializationType(Name, TemplateArgs);
1684
1685 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1686 << Name;
1687 NoteAllFoundTemplates(Name);
1688 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001689 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001690
Douglas Gregor40808ce2009-03-09 23:48:35 +00001691 // Check that the template argument list is well-formed for this
1692 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001693 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001694 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001695 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001696 return QualType();
1697
Douglas Gregor910f8002010-11-07 23:05:16 +00001698 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001699 "Converted template argument list is too short!");
1700
1701 QualType CanonType;
1702
Douglas Gregorcaddba02009-11-12 18:38:13 +00001703 if (Name.isDependent() ||
1704 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001705 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001706 // This class template specialization is a dependent
1707 // type. Therefore, its canonical type is another class template
1708 // specialization type that contains all of the converted
1709 // arguments in canonical form. This ensures that, e.g., A<T> and
1710 // A<T, T> have identical types when A is declared as:
1711 //
1712 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001713 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001714 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001715 Converted.data(),
1716 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001717
Douglas Gregor1275ae02009-07-28 23:00:59 +00001718 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001719 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001720 // In the future, we need to teach getTemplateSpecializationType to only
1721 // build the canonical type and return that to us.
1722 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001723
1724 // This might work out to be a current instantiation, in which
1725 // case the canonical type needs to be the InjectedClassNameType.
1726 //
1727 // TODO: in theory this could be a simple hashtable lookup; most
1728 // changes to CurContext don't change the set of current
1729 // instantiations.
1730 if (isa<ClassTemplateDecl>(Template)) {
1731 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1732 // If we get out to a namespace, we're done.
1733 if (Ctx->isFileContext()) break;
1734
1735 // If this isn't a record, keep looking.
1736 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1737 if (!Record) continue;
1738
1739 // Look for one of the two cases with InjectedClassNameTypes
1740 // and check whether it's the same template.
1741 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1742 !Record->getDescribedClassTemplate())
1743 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001744
John McCall31f17ec2010-04-27 00:57:59 +00001745 // Fetch the injected class name type and check whether its
1746 // injected type is equal to the type we just built.
1747 QualType ICNT = Context.getTypeDeclType(Record);
1748 QualType Injected = cast<InjectedClassNameType>(ICNT)
1749 ->getInjectedSpecializationType();
1750
1751 if (CanonType != Injected->getCanonicalTypeInternal())
1752 continue;
1753
1754 // If so, the canonical type of this TST is the injected
1755 // class name type of the record we just found.
1756 assert(ICNT.isCanonical());
1757 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001758 break;
1759 }
1760 }
Mike Stump1eb44332009-09-09 15:08:12 +00001761 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001762 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001763 // Find the class template specialization declaration that
1764 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001765 void *InsertPos = 0;
1766 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001767 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001768 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001769 if (!Decl) {
1770 // This is the first time we have referenced this class template
1771 // specialization. Create the canonical declaration and add it to
1772 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001773 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001774 ClassTemplate->getTemplatedDecl()->getTagKind(),
1775 ClassTemplate->getDeclContext(),
1776 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001777 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001778 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001779 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001780 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001781 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001782 Decl->setLexicalDeclContext(CurContext);
1783 }
1784
1785 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001786 assert(isa<RecordType>(CanonType) &&
1787 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001788 }
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Douglas Gregor40808ce2009-03-09 23:48:35 +00001790 // Build the fully-sugared type for this class template
1791 // specialization, which refers back to the class template
1792 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001793 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001794}
1795
John McCallf312b1e2010-08-26 23:41:50 +00001796TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001797Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1798 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001799 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001800 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001801 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001802 if (SS.isInvalid())
1803 return true;
1804
Douglas Gregor7532dc62009-03-30 22:58:21 +00001805 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001806
Douglas Gregor40808ce2009-03-09 23:48:35 +00001807 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001808 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001809 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001810
Douglas Gregora88f09f2011-02-28 17:23:35 +00001811 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1812 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1813 DTN->getQualifier(),
1814 DTN->getIdentifier(),
1815 TemplateArgs);
1816
1817 // Build type-source information.
1818 TypeLocBuilder TLB;
1819 DependentTemplateSpecializationTypeLoc SpecTL
1820 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001821 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001822 SpecTL.setNameLoc(TemplateLoc);
1823 SpecTL.setLAngleLoc(LAngleLoc);
1824 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001825 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001826 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1827 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1828 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1829 }
1830
John McCalld5532b62009-11-23 01:53:49 +00001831 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001832 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001833
1834 if (Result.isNull())
1835 return true;
1836
Douglas Gregor059101f2011-03-02 00:47:37 +00001837 // Build type-source information.
1838 TypeLocBuilder TLB;
1839 TemplateSpecializationTypeLoc SpecTL
1840 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1841 SpecTL.setTemplateNameLoc(TemplateLoc);
1842 SpecTL.setLAngleLoc(LAngleLoc);
1843 SpecTL.setRAngleLoc(RAngleLoc);
1844 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1845 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001846
Douglas Gregor059101f2011-03-02 00:47:37 +00001847 if (SS.isNotEmpty()) {
1848 // Create an elaborated-type-specifier containing the nested-name-specifier.
1849 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1850 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1851 ElabTL.setKeywordLoc(SourceLocation());
1852 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1853 }
1854
1855 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001856}
John McCallf1bbbb42009-09-04 01:14:41 +00001857
Douglas Gregor059101f2011-03-02 00:47:37 +00001858TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001859 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001860 SourceLocation TagLoc,
1861 CXXScopeSpec &SS,
1862 TemplateTy TemplateD,
1863 SourceLocation TemplateLoc,
1864 SourceLocation LAngleLoc,
1865 ASTTemplateArgsPtr TemplateArgsIn,
1866 SourceLocation RAngleLoc) {
1867 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1868
1869 // Translate the parser's template argument list in our AST format.
1870 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1871 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1872
1873 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001874 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001875 ElaboratedTypeKeyword Keyword
1876 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Douglas Gregor059101f2011-03-02 00:47:37 +00001878 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1879 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1880 DTN->getQualifier(),
1881 DTN->getIdentifier(),
1882 TemplateArgs);
1883
1884 // Build type-source information.
1885 TypeLocBuilder TLB;
1886 DependentTemplateSpecializationTypeLoc SpecTL
1887 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1888 SpecTL.setKeywordLoc(TagLoc);
1889 SpecTL.setNameLoc(TemplateLoc);
1890 SpecTL.setLAngleLoc(LAngleLoc);
1891 SpecTL.setRAngleLoc(RAngleLoc);
1892 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1893 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1894 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1895 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1896 }
1897
1898 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1899 if (Result.isNull())
1900 return TypeResult();
1901
1902 // Check the tag kind
1903 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001904 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001905
John McCall6b2becf2009-09-08 17:47:29 +00001906 IdentifierInfo *Id = D->getIdentifier();
1907 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001908
John McCall6b2becf2009-09-08 17:47:29 +00001909 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1910 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001911 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001912 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001913 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001914 }
1915 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001916
1917 // Provide source-location information for the template specialization.
1918 TypeLocBuilder TLB;
1919 TemplateSpecializationTypeLoc SpecTL
1920 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1921 SpecTL.setTemplateNameLoc(TemplateLoc);
1922 SpecTL.setLAngleLoc(LAngleLoc);
1923 SpecTL.setRAngleLoc(RAngleLoc);
1924 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1925 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001926
Douglas Gregor059101f2011-03-02 00:47:37 +00001927 // Construct an elaborated type containing the nested-name-specifier (if any)
1928 // and keyword.
1929 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1930 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1931 ElabTL.setKeywordLoc(TagLoc);
1932 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1933 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001934}
1935
John McCall60d7b3a2010-08-24 06:29:42 +00001936ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001937 LookupResult &R,
1938 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001939 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001940 // FIXME: Can we do any checking at this point? I guess we could check the
1941 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001942 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001943 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001944 // foo<int> could identify a single function unambiguously
1945 // This approach does NOT work, since f<int>(1);
1946 // gets resolved prior to resorting to overload resolution
1947 // i.e., template<class T> void f(double);
1948 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001949
1950 // These should be filtered out by our callers.
1951 assert(!R.empty() && "empty lookup results when building templateid");
1952 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1953
John McCallc373d482010-01-27 01:50:18 +00001954 // We don't want lookup warnings at this point.
1955 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001956
John McCallf7a1a742009-11-24 19:00:30 +00001957 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001958 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001959 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001960 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001961 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001962 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001963
1964 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001965}
1966
John McCallf7a1a742009-11-24 19:00:30 +00001967// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001968ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001969Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001970 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001971 const TemplateArgumentListInfo &TemplateArgs) {
1972 DeclContext *DC;
1973 if (!(DC = computeDeclContext(SS, false)) ||
1974 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001975 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001976 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001978 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001979 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001980 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1981 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001982
John McCallf7a1a742009-11-24 19:00:30 +00001983 if (R.isAmbiguous())
1984 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001985
John McCallf7a1a742009-11-24 19:00:30 +00001986 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001987 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1988 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001989 return ExprError();
1990 }
1991
1992 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001993 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1994 << (NestedNameSpecifier*) SS.getScopeRep()
1995 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001996 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1997 return ExprError();
1998 }
1999
2000 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002001}
2002
Douglas Gregorc45c2322009-03-31 00:43:58 +00002003/// \brief Form a dependent template name.
2004///
2005/// This action forms a dependent template name given the template
2006/// name and its (presumably dependent) scope specifier. For
2007/// example, given "MetaFun::template apply", the scope specifier \p
2008/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2009/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002010TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002011 SourceLocation TemplateKWLoc,
2012 CXXScopeSpec &SS,
2013 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002014 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002015 bool EnteringContext,
2016 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002017 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2018 !getLangOptions().CPlusPlus0x)
2019 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002020 << FixItHint::CreateRemoval(TemplateKWLoc);
2021
Douglas Gregor0707bc52010-01-19 16:01:07 +00002022 DeclContext *LookupCtx = 0;
2023 if (SS.isSet())
2024 LookupCtx = computeDeclContext(SS, EnteringContext);
2025 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002026 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002027 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002028 // C++0x [temp.names]p5:
2029 // If a name prefixed by the keyword template is not the name of
2030 // a template, the program is ill-formed. [Note: the keyword
2031 // template may not be applied to non-template members of class
2032 // templates. -end note ] [ Note: as is the case with the
2033 // typename prefix, the template prefix is allowed in cases
2034 // where it is not strictly necessary; i.e., when the
2035 // nested-name-specifier or the expression on the left of the ->
2036 // or . is not dependent on a template-parameter, or the use
2037 // does not appear in the scope of a template. -end note]
2038 //
2039 // Note: C++03 was more strict here, because it banned the use of
2040 // the "template" keyword prior to a template-name that was not a
2041 // dependent name. C++ DR468 relaxed this requirement (the
2042 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002043 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002044 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002045 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2046 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002047 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002048 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2049 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002050 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2051 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002052 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002053 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002054 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002055 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002056 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002057 << Name.getSourceRange()
2058 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002059 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002060 } else {
2061 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002062 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002063 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002064 }
2065
Mike Stump1eb44332009-09-09 15:08:12 +00002066 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002067 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002068
Douglas Gregor014e88d2009-11-03 23:16:33 +00002069 switch (Name.getKind()) {
2070 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002071 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002072 Name.Identifier));
2073 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002074
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002075 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002076 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002077 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002078 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002079
2080 case UnqualifiedId::IK_LiteralOperatorId:
2081 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2082
Douglas Gregor014e88d2009-11-03 23:16:33 +00002083 default:
2084 break;
2085 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002086
2087 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002088 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002089 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002090 << Name.getSourceRange()
2091 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002092 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002093}
2094
Mike Stump1eb44332009-09-09 15:08:12 +00002095bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002096 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002097 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002098 const TemplateArgument &Arg = AL.getArgument();
2099
Anders Carlsson436b1562009-06-13 00:33:33 +00002100 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002101 switch(Arg.getKind()) {
2102 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002103 // C++ [temp.arg.type]p1:
2104 // A template-argument for a template-parameter which is a
2105 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002106 break;
2107 case TemplateArgument::Template: {
2108 // We have a template type parameter but the template argument
2109 // is a template without any arguments.
2110 SourceRange SR = AL.getSourceRange();
2111 TemplateName Name = Arg.getAsTemplate();
2112 Diag(SR.getBegin(), diag::err_template_missing_args)
2113 << Name << SR;
2114 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2115 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002116
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002117 return true;
2118 }
2119 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002120 // We have a template type parameter but the template argument
2121 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002122 SourceRange SR = AL.getSourceRange();
2123 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002124 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Anders Carlsson436b1562009-06-13 00:33:33 +00002126 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002127 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002128 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002129
John McCalla93c9342009-12-07 02:54:59 +00002130 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002131 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002132
Anders Carlsson436b1562009-06-13 00:33:33 +00002133 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002134 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002135 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002136 return false;
2137}
2138
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002139/// \brief Substitute template arguments into the default template argument for
2140/// the given template type parameter.
2141///
2142/// \param SemaRef the semantic analysis object for which we are performing
2143/// the substitution.
2144///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002145/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002146/// for.
2147///
2148/// \param TemplateLoc the location of the template name that started the
2149/// template-id we are checking.
2150///
2151/// \param RAngleLoc the location of the right angle bracket ('>') that
2152/// terminates the template-id.
2153///
2154/// \param Param the template template parameter whose default we are
2155/// substituting into.
2156///
2157/// \param Converted the list of template arguments provided for template
2158/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002159/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002160static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002161SubstDefaultTemplateArgument(Sema &SemaRef,
2162 TemplateDecl *Template,
2163 SourceLocation TemplateLoc,
2164 SourceLocation RAngleLoc,
2165 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002166 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002167 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002168
2169 // If the argument type is dependent, instantiate it now based
2170 // on the previously-computed template arguments.
2171 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002172 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002173 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002174
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002175 MultiLevelTemplateArgumentList AllTemplateArgs
2176 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2177
2178 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002179 Template, Converted.data(),
2180 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002181 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002182
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002183 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2184 Param->getDefaultArgumentLoc(),
2185 Param->getDeclName());
2186 }
2187
2188 return ArgType;
2189}
2190
2191/// \brief Substitute template arguments into the default template argument for
2192/// the given non-type template parameter.
2193///
2194/// \param SemaRef the semantic analysis object for which we are performing
2195/// the substitution.
2196///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002197/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002198/// for.
2199///
2200/// \param TemplateLoc the location of the template name that started the
2201/// template-id we are checking.
2202///
2203/// \param RAngleLoc the location of the right angle bracket ('>') that
2204/// terminates the template-id.
2205///
Douglas Gregor788cd062009-11-11 01:00:40 +00002206/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002207/// substituting into.
2208///
2209/// \param Converted the list of template arguments provided for template
2210/// parameters that precede \p Param in the template parameter list.
2211///
2212/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002213static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002214SubstDefaultTemplateArgument(Sema &SemaRef,
2215 TemplateDecl *Template,
2216 SourceLocation TemplateLoc,
2217 SourceLocation RAngleLoc,
2218 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002219 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002220 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002221 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002222
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002223 MultiLevelTemplateArgumentList AllTemplateArgs
2224 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002225
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002226 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002227 Template, Converted.data(),
2228 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002229 SourceRange(TemplateLoc, RAngleLoc));
2230
2231 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2232}
2233
Douglas Gregor788cd062009-11-11 01:00:40 +00002234/// \brief Substitute template arguments into the default template argument for
2235/// the given template template parameter.
2236///
2237/// \param SemaRef the semantic analysis object for which we are performing
2238/// the substitution.
2239///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002240/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002241/// for.
2242///
2243/// \param TemplateLoc the location of the template name that started the
2244/// template-id we are checking.
2245///
2246/// \param RAngleLoc the location of the right angle bracket ('>') that
2247/// terminates the template-id.
2248///
2249/// \param Param the template template parameter whose default we are
2250/// substituting into.
2251///
2252/// \param Converted the list of template arguments provided for template
2253/// parameters that precede \p Param in the template parameter list.
2254///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002255/// \param QualifierLoc Will be set to the nested-name-specifier (with
2256/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002257///
Douglas Gregor788cd062009-11-11 01:00:40 +00002258/// \returns the substituted template argument, or NULL if an error occurred.
2259static TemplateName
2260SubstDefaultTemplateArgument(Sema &SemaRef,
2261 TemplateDecl *Template,
2262 SourceLocation TemplateLoc,
2263 SourceLocation RAngleLoc,
2264 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002265 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2266 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002267 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002268 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002269
Douglas Gregor788cd062009-11-11 01:00:40 +00002270 MultiLevelTemplateArgumentList AllTemplateArgs
2271 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002272
Douglas Gregor788cd062009-11-11 01:00:40 +00002273 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002274 Template, Converted.data(),
2275 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002276 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002277
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002278 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002279 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002280 if (QualifierLoc) {
2281 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2282 AllTemplateArgs);
2283 if (!QualifierLoc)
2284 return TemplateName();
2285 }
2286
Douglas Gregor1d752d72011-03-02 18:46:51 +00002287 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002288 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002289 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002290 AllTemplateArgs);
2291}
2292
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002293/// \brief If the given template parameter has a default template
2294/// argument, substitute into that default template argument and
2295/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002296TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002297Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2298 SourceLocation TemplateLoc,
2299 SourceLocation RAngleLoc,
2300 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002301 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2302 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002303 if (!TypeParm->hasDefaultArgument())
2304 return TemplateArgumentLoc();
2305
John McCalla93c9342009-12-07 02:54:59 +00002306 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002307 TemplateLoc,
2308 RAngleLoc,
2309 TypeParm,
2310 Converted);
2311 if (DI)
2312 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2313
2314 return TemplateArgumentLoc();
2315 }
2316
2317 if (NonTypeTemplateParmDecl *NonTypeParm
2318 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2319 if (!NonTypeParm->hasDefaultArgument())
2320 return TemplateArgumentLoc();
2321
John McCall60d7b3a2010-08-24 06:29:42 +00002322 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002323 TemplateLoc,
2324 RAngleLoc,
2325 NonTypeParm,
2326 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002327 if (Arg.isInvalid())
2328 return TemplateArgumentLoc();
2329
2330 Expr *ArgE = Arg.takeAs<Expr>();
2331 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2332 }
2333
2334 TemplateTemplateParmDecl *TempTempParm
2335 = cast<TemplateTemplateParmDecl>(Param);
2336 if (!TempTempParm->hasDefaultArgument())
2337 return TemplateArgumentLoc();
2338
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002339
Douglas Gregor1d752d72011-03-02 18:46:51 +00002340 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002341 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002343 RAngleLoc,
2344 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002345 Converted,
2346 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002347 if (TName.isNull())
2348 return TemplateArgumentLoc();
2349
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002350 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002351 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002352 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2353}
2354
Douglas Gregore7526412009-11-11 19:31:23 +00002355/// \brief Check that the given template argument corresponds to the given
2356/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002357///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002358/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002359/// checked.
2360///
2361/// \param Arg The template argument.
2362///
2363/// \param Template The template in which the template argument resides.
2364///
2365/// \param TemplateLoc The location of the template name for the template
2366/// whose argument list we're matching.
2367///
2368/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2369/// the template argument list.
2370///
2371/// \param ArgumentPackIndex The index into the argument pack where this
2372/// argument will be placed. Only valid if the parameter is a parameter pack.
2373///
2374/// \param Converted The checked, converted argument will be added to the
2375/// end of this small vector.
2376///
2377/// \param CTAK Describes how we arrived at this particular template argument:
2378/// explicitly written, deduced, etc.
2379///
2380/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002381bool Sema::CheckTemplateArgument(NamedDecl *Param,
2382 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002383 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002384 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002385 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002386 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002387 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002388 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002389 // Check template type parameters.
2390 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002391 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002392
Douglas Gregord9e15302009-11-11 19:41:09 +00002393 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002394 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002395 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002396 // with the template arguments we've seen thus far. But if the
2397 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002398 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002399 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2400 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002401
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002402 if (NTTPType->isDependentType() &&
2403 !isa<TemplateTemplateParmDecl>(Template) &&
2404 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002405 // Do substitution on the type of the non-type template parameter.
2406 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002407 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002408 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002409
2410 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002411 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002412 NTTPType = SubstType(NTTPType,
2413 MultiLevelTemplateArgumentList(TemplateArgs),
2414 NTTP->getLocation(),
2415 NTTP->getDeclName());
2416 // If that worked, check the non-type template parameter type
2417 // for validity.
2418 if (!NTTPType.isNull())
2419 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2420 NTTP->getLocation());
2421 if (NTTPType.isNull())
2422 return true;
2423 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002424
Douglas Gregore7526412009-11-11 19:31:23 +00002425 switch (Arg.getArgument().getKind()) {
2426 case TemplateArgument::Null:
2427 assert(false && "Should never see a NULL template argument here");
2428 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002429
Douglas Gregore7526412009-11-11 19:31:23 +00002430 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002431 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002432 ExprResult Res =
2433 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2434 Result, CTAK);
2435 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002436 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002437
Douglas Gregor910f8002010-11-07 23:05:16 +00002438 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002439 break;
2440 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002441
Douglas Gregore7526412009-11-11 19:31:23 +00002442 case TemplateArgument::Declaration:
2443 case TemplateArgument::Integral:
2444 // We've already checked this template argument, so just copy
2445 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002446 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002447 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002448
Douglas Gregore7526412009-11-11 19:31:23 +00002449 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002450 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002451 // We were given a template template argument. It may not be ill-formed;
2452 // see below.
2453 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002454 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2455 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002456 // We have a template argument such as \c T::template X, which we
2457 // parsed as a template template argument. However, since we now
2458 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002459 // template name into an expression.
2460
2461 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2462 Arg.getTemplateNameLoc());
2463
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002464 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002465 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002466 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002467 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002468 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002469
Douglas Gregora7fc9012011-01-05 18:58:31 +00002470 // If we parsed the template argument as a pack expansion, create a
2471 // pack expansion expression.
2472 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002473 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2474 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002475 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002477
Douglas Gregore7526412009-11-11 19:31:23 +00002478 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002479 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2480 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002481 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002482
Douglas Gregor910f8002010-11-07 23:05:16 +00002483 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002484 break;
2485 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002486
Douglas Gregore7526412009-11-11 19:31:23 +00002487 // We have a template argument that actually does refer to a class
2488 // template, template alias, or template template parameter, and
2489 // therefore cannot be a non-type template argument.
2490 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2491 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002492
Douglas Gregore7526412009-11-11 19:31:23 +00002493 Diag(Param->getLocation(), diag::note_template_param_here);
2494 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002495
Douglas Gregore7526412009-11-11 19:31:23 +00002496 case TemplateArgument::Type: {
2497 // We have a non-type template parameter but the template
2498 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002499
Douglas Gregore7526412009-11-11 19:31:23 +00002500 // C++ [temp.arg]p2:
2501 // In a template-argument, an ambiguity between a type-id and
2502 // an expression is resolved to a type-id, regardless of the
2503 // form of the corresponding template-parameter.
2504 //
2505 // We warn specifically about this case, since it can be rather
2506 // confusing for users.
2507 QualType T = Arg.getArgument().getAsType();
2508 SourceRange SR = Arg.getSourceRange();
2509 if (T->isFunctionType())
2510 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2511 else
2512 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2513 Diag(Param->getLocation(), diag::note_template_param_here);
2514 return true;
2515 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002516
Douglas Gregore7526412009-11-11 19:31:23 +00002517 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002518 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002519 break;
2520 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002521
Douglas Gregore7526412009-11-11 19:31:23 +00002522 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002523 }
2524
2525
Douglas Gregore7526412009-11-11 19:31:23 +00002526 // Check template template parameters.
2527 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002528
Douglas Gregore7526412009-11-11 19:31:23 +00002529 // Substitute into the template parameter list of the template
2530 // template parameter, since previously-supplied template arguments
2531 // may appear within the template template parameter.
2532 {
2533 // Set up a template instantiation context.
2534 LocalInstantiationScope Scope(*this);
2535 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002536 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002537 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002538
2539 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002540 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002541 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002542 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002543 MultiLevelTemplateArgumentList(TemplateArgs)));
2544 if (!TempParm)
2545 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002546 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002547
Douglas Gregore7526412009-11-11 19:31:23 +00002548 switch (Arg.getArgument().getKind()) {
2549 case TemplateArgument::Null:
2550 assert(false && "Should never see a NULL template argument here");
2551 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002552
Douglas Gregore7526412009-11-11 19:31:23 +00002553 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002554 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002555 if (CheckTemplateArgument(TempParm, Arg))
2556 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002557
Douglas Gregor910f8002010-11-07 23:05:16 +00002558 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002559 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002560
Douglas Gregore7526412009-11-11 19:31:23 +00002561 case TemplateArgument::Expression:
2562 case TemplateArgument::Type:
2563 // We have a template template parameter but the template
2564 // argument does not refer to a template.
2565 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2566 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002567
Douglas Gregore7526412009-11-11 19:31:23 +00002568 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002569 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002570 "Declaration argument with template template parameter");
2571 break;
2572 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002573 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002574 "Integral argument with template template parameter");
2575 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002576
Douglas Gregore7526412009-11-11 19:31:23 +00002577 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002578 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002579 break;
2580 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002581
Douglas Gregore7526412009-11-11 19:31:23 +00002582 return false;
2583}
2584
Douglas Gregorc15cb382009-02-09 23:23:08 +00002585/// \brief Check that the given template argument list is well-formed
2586/// for specializing the given template.
2587bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2588 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002589 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002590 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002591 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002592 TemplateParameterList *Params = Template->getTemplateParameters();
2593 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002594 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002595 bool Invalid = false;
2596
John McCalld5532b62009-11-23 01:53:49 +00002597 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2598
Mike Stump1eb44332009-09-09 15:08:12 +00002599 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002600 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002601
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002602 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002603 (NumArgs < Params->getMinRequiredArguments() &&
2604 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002605 // FIXME: point at either the first arg beyond what we can handle,
2606 // or the '>', depending on whether we have too many or too few
2607 // arguments.
2608 SourceRange Range;
2609 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002610 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002611 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2612 << (NumArgs > NumParams)
2613 << (isa<ClassTemplateDecl>(Template)? 0 :
2614 isa<FunctionTemplateDecl>(Template)? 1 :
2615 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2616 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002617 Diag(Template->getLocation(), diag::note_template_decl_here)
2618 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002619 Invalid = true;
2620 }
Mike Stump1eb44332009-09-09 15:08:12 +00002621
2622 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002623 // [...] The type and form of each template-argument specified in
2624 // a template-id shall match the type and form specified for the
2625 // corresponding parameter declared by the template in its
2626 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002627 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002628 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2629 TemplateParameterList::iterator Param = Params->begin(),
2630 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002631 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002632 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002633 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002634 if (ArgIdx > NumArgs && PartialTemplateArgs)
2635 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002636
Douglas Gregorf35f8282009-11-11 21:54:23 +00002637 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002638 // If we have an expanded parameter pack, make sure we don't have too
2639 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002640 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002641 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002642 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002643 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2644 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2645 << true
2646 << (isa<ClassTemplateDecl>(Template)? 0 :
2647 isa<FunctionTemplateDecl>(Template)? 1 :
2648 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2649 << Template;
2650 Diag(Template->getLocation(), diag::note_template_decl_here)
2651 << Params->getSourceRange();
2652 return true;
2653 }
2654 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002655
Douglas Gregorf35f8282009-11-11 21:54:23 +00002656 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002657 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2658 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002659 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002660 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002661
Douglas Gregor14be16b2010-12-20 16:57:52 +00002662 if ((*Param)->isTemplateParameterPack()) {
2663 // The template parameter was a template parameter pack, so take the
2664 // deduced argument and place it on the argument pack. Note that we
2665 // stay on the same template parameter so that we can deduce more
2666 // arguments.
2667 ArgumentPack.push_back(Converted.back());
2668 Converted.pop_back();
2669 } else {
2670 // Move to the next template parameter.
2671 ++Param;
2672 }
2673 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002674 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002676
2677 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002678 // arguments, just break out now and we'll fill in the argument pack below.
2679 if ((*Param)->isTemplateParameterPack())
2680 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002681
Douglas Gregorf35f8282009-11-11 21:54:23 +00002682 // We have a default template argument that we will use.
2683 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002684
Douglas Gregorf35f8282009-11-11 21:54:23 +00002685 // Retrieve the default template argument from the template
2686 // parameter. For each kind of template parameter, we substitute the
2687 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002688 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002689 // the default argument.
2690 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2691 if (!TTP->hasDefaultArgument()) {
2692 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2693 break;
2694 }
2695
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002697 Template,
2698 TemplateLoc,
2699 RAngleLoc,
2700 TTP,
2701 Converted);
2702 if (!ArgType)
2703 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002704
Douglas Gregorf35f8282009-11-11 21:54:23 +00002705 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2706 ArgType);
2707 } else if (NonTypeTemplateParmDecl *NTTP
2708 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2709 if (!NTTP->hasDefaultArgument()) {
2710 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2711 break;
2712 }
2713
John McCall60d7b3a2010-08-24 06:29:42 +00002714 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002715 TemplateLoc,
2716 RAngleLoc,
2717 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002718 Converted);
2719 if (E.isInvalid())
2720 return true;
2721
2722 Expr *Ex = E.takeAs<Expr>();
2723 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2724 } else {
2725 TemplateTemplateParmDecl *TempParm
2726 = cast<TemplateTemplateParmDecl>(*Param);
2727
2728 if (!TempParm->hasDefaultArgument()) {
2729 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2730 break;
2731 }
2732
Douglas Gregor1d752d72011-03-02 18:46:51 +00002733 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002734 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002735 TemplateLoc,
2736 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002737 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002738 Converted,
2739 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002740 if (Name.isNull())
2741 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002742
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002743 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2744 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002745 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002746
Douglas Gregorf35f8282009-11-11 21:54:23 +00002747 // Introduce an instantiation record that describes where we are using
2748 // the default template argument.
2749 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002750 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002751 SourceRange(TemplateLoc, RAngleLoc));
2752
Douglas Gregorf35f8282009-11-11 21:54:23 +00002753 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002754 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002755 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002756 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002757
Douglas Gregor67714232011-03-03 02:41:12 +00002758 // Core issue 150 (assumed resolution): if this is a template template
2759 // parameter, keep track of the default template arguments from the
2760 // template definition.
2761 if (isTemplateTemplateParameter)
2762 TemplateArgs.addArgument(Arg);
2763
Douglas Gregor14be16b2010-12-20 16:57:52 +00002764 // Move to the next template parameter and argument.
2765 ++Param;
2766 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002768
Douglas Gregor14be16b2010-12-20 16:57:52 +00002769 // Form argument packs for each of the parameter packs remaining.
2770 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002771 // If we're checking a partial list of template arguments, don't fill
2772 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002773
2774 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002775 if (PartialTemplateArgs && ArgumentPack.empty()) {
2776 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002777 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002778 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002779 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002780 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2781 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002782 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002783 ArgumentPack.clear();
2784 }
2785 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002786
Douglas Gregor14be16b2010-12-20 16:57:52 +00002787 ++Param;
2788 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789
Douglas Gregorc15cb382009-02-09 23:23:08 +00002790 return Invalid;
2791}
2792
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002793namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002794 class UnnamedLocalNoLinkageFinder
2795 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002796 {
2797 Sema &S;
2798 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002799
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002800 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002801
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002802 public:
2803 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2804
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002805 bool Visit(QualType T) {
2806 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002807 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002808
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002809#define TYPE(Class, Parent) \
2810 bool Visit##Class##Type(const Class##Type *);
2811#define ABSTRACT_TYPE(Class, Parent) \
2812 bool Visit##Class##Type(const Class##Type *) { return false; }
2813#define NON_CANONICAL_TYPE(Class, Parent) \
2814 bool Visit##Class##Type(const Class##Type *) { return false; }
2815#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002816
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002817 bool VisitTagDecl(const TagDecl *Tag);
2818 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2819 };
2820}
2821
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002822bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002823 return false;
2824}
2825
2826bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2827 return Visit(T->getElementType());
2828}
2829
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002830bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002831 return Visit(T->getPointeeType());
2832}
2833
2834bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002835 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002836 return Visit(T->getPointeeType());
2837}
2838
2839bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002840 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002841 return Visit(T->getPointeeType());
2842}
2843
2844bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002845 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002846 return Visit(T->getPointeeType());
2847}
2848
2849bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002850 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002851 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2852}
2853
2854bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002855 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002856 return Visit(T->getElementType());
2857}
2858
2859bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002860 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002861 return Visit(T->getElementType());
2862}
2863
2864bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002865 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002866 return Visit(T->getElementType());
2867}
2868
2869bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002870 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002871 return Visit(T->getElementType());
2872}
2873
2874bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002875 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002876 return Visit(T->getElementType());
2877}
2878
2879bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2880 return Visit(T->getElementType());
2881}
2882
2883bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2884 return Visit(T->getElementType());
2885}
2886
2887bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2888 const FunctionProtoType* T) {
2889 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002890 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002891 A != AEnd; ++A) {
2892 if (Visit(*A))
2893 return true;
2894 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002895
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002896 return Visit(T->getResultType());
2897}
2898
2899bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2900 const FunctionNoProtoType* T) {
2901 return Visit(T->getResultType());
2902}
2903
2904bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2905 const UnresolvedUsingType*) {
2906 return false;
2907}
2908
2909bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2910 return false;
2911}
2912
2913bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2914 return Visit(T->getUnderlyingType());
2915}
2916
2917bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2918 return false;
2919}
2920
Richard Smith34b41d92011-02-20 03:19:35 +00002921bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2922 return Visit(T->getDeducedType());
2923}
2924
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002925bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2926 return VisitTagDecl(T->getDecl());
2927}
2928
2929bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2930 return VisitTagDecl(T->getDecl());
2931}
2932
2933bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2934 const TemplateTypeParmType*) {
2935 return false;
2936}
2937
Douglas Gregorc3069d62011-01-14 02:55:32 +00002938bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2939 const SubstTemplateTypeParmPackType *) {
2940 return false;
2941}
2942
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002943bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2944 const TemplateSpecializationType*) {
2945 return false;
2946}
2947
2948bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2949 const InjectedClassNameType* T) {
2950 return VisitTagDecl(T->getDecl());
2951}
2952
2953bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2954 const DependentNameType* T) {
2955 return VisitNestedNameSpecifier(T->getQualifier());
2956}
2957
2958bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2959 const DependentTemplateSpecializationType* T) {
2960 return VisitNestedNameSpecifier(T->getQualifier());
2961}
2962
Douglas Gregor7536dd52010-12-20 02:24:11 +00002963bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2964 const PackExpansionType* T) {
2965 return Visit(T->getPattern());
2966}
2967
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002968bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2969 return false;
2970}
2971
2972bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2973 const ObjCInterfaceType *) {
2974 return false;
2975}
2976
2977bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2978 const ObjCObjectPointerType *) {
2979 return false;
2980}
2981
2982bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2983 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2984 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2985 << S.Context.getTypeDeclType(Tag) << SR;
2986 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002987 }
2988
Richard Smith162e1c12011-04-15 14:24:37 +00002989 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002990 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2991 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2992 return true;
2993 }
2994
2995 return false;
2996}
2997
2998bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2999 NestedNameSpecifier *NNS) {
3000 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3001 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003002
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003003 switch (NNS->getKind()) {
3004 case NestedNameSpecifier::Identifier:
3005 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003006 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003007 case NestedNameSpecifier::Global:
3008 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003009
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003010 case NestedNameSpecifier::TypeSpec:
3011 case NestedNameSpecifier::TypeSpecWithTemplate:
3012 return Visit(QualType(NNS->getAsType(), 0));
3013 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003014 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003015}
3016
3017
Douglas Gregorc15cb382009-02-09 23:23:08 +00003018/// \brief Check a template argument against its corresponding
3019/// template type parameter.
3020///
3021/// This routine implements the semantics of C++ [temp.arg.type]. It
3022/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003023bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003024 TypeSourceInfo *ArgInfo) {
3025 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003026 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003027 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003028
3029 if (Arg->isVariablyModifiedType()) {
3030 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003031 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003032 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003033 }
3034
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003035 // C++03 [temp.arg.type]p2:
3036 // A local type, a type with no linkage, an unnamed type or a type
3037 // compounded from any of these types shall not be used as a
3038 // template-argument for a template type-parameter.
3039 //
3040 // C++0x allows these, and even in C++03 we allow them as an extension with
3041 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003042 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003043 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3044 (void)Finder.Visit(Context.getCanonicalType(Arg));
3045 }
3046
Douglas Gregorc15cb382009-02-09 23:23:08 +00003047 return false;
3048}
3049
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003050/// \brief Checks whether the given template argument is the address
3051/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003052static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003053CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3054 NonTypeTemplateParmDecl *Param,
3055 QualType ParamType,
3056 Expr *ArgIn,
3057 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003058 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003059 Expr *Arg = ArgIn;
3060 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003061
3062 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003063 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003064 Arg = Cast->getSubExpr();
3065
3066 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003067 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003068 // A template-argument for a non-type, non-template
3069 // template-parameter shall be one of: [...]
3070 //
3071 // -- the address of an object or function with external
3072 // linkage, including function templates and function
3073 // template-ids but excluding non-static class members,
3074 // expressed as & id-expression where the & is optional if
3075 // the name refers to a function or array, or if the
3076 // corresponding template-parameter is a reference; or
3077 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003078
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003079 // In C++98/03 mode, give an extension warning on any extra parentheses.
3080 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3081 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003082 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003083 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003084 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003085 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003086 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003087 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003088 }
3089
3090 Arg = Parens->getSubExpr();
3091 }
3092
Douglas Gregorb7a09262010-04-01 18:32:35 +00003093 bool AddressTaken = false;
3094 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003095 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003096 if (UnOp->getOpcode() == UO_AddrOf) {
Francois Pichet11f98d02011-04-28 05:12:34 +00003097 // Support &__uuidof(class_with_uuid) as a non-type template argument.
3098 // Very common in Microsoft COM headers.
3099 if (S.getLangOptions().Microsoft &&
3100 isa<CXXUuidofExpr>(UnOp->getSubExpr())) {
3101 Converted = TemplateArgument(ArgIn);
3102 return false;
3103 }
3104
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003105 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003106 AddressTaken = true;
3107 AddrOpLoc = UnOp->getOperatorLoc();
3108 }
Francois Picheta343a412011-04-29 09:08:14 +00003109 } else {
3110 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3111 Converted = TemplateArgument(ArgIn);
3112 return false;
3113 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003114 DRE = dyn_cast<DeclRefExpr>(Arg);
Francois Picheta343a412011-04-29 09:08:14 +00003115 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003116 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003117 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3118 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003119 S.Diag(Param->getLocation(), diag::note_template_param_here);
3120 return true;
3121 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003122
3123 // Stop checking the precise nature of the argument if it is value dependent,
3124 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003125 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003126 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003127 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003128 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003129
Douglas Gregorb7a09262010-04-01 18:32:35 +00003130 if (!isa<ValueDecl>(DRE->getDecl())) {
3131 S.Diag(Arg->getSourceRange().getBegin(),
3132 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003133 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003134 S.Diag(Param->getLocation(), diag::note_template_param_here);
3135 return true;
3136 }
3137
3138 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003139
3140 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003141 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3142 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003143 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003144 S.Diag(Param->getLocation(), diag::note_template_param_here);
3145 return true;
3146 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003147
3148 // Cannot refer to non-static member functions
3149 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003150 if (!Method->isStatic()) {
3151 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003152 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003153 S.Diag(Param->getLocation(), diag::note_template_param_here);
3154 return true;
3155 }
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003157 // Functions must have external linkage.
3158 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003159 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003160 S.Diag(Arg->getSourceRange().getBegin(),
3161 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003162 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003163 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003164 << true;
3165 return true;
3166 }
3167
3168 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003169 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003170
Douglas Gregorb7a09262010-04-01 18:32:35 +00003171 // If the template parameter has pointer type, the function decays.
3172 if (ParamType->isPointerType() && !AddressTaken)
3173 ArgType = S.Context.getPointerType(Func->getType());
3174 else if (AddressTaken && ParamType->isReferenceType()) {
3175 // If we originally had an address-of operator, but the
3176 // parameter has reference type, complain and (if things look
3177 // like they will work) drop the address-of operator.
3178 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3179 ParamType.getNonReferenceType())) {
3180 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3181 << ParamType;
3182 S.Diag(Param->getLocation(), diag::note_template_param_here);
3183 return true;
3184 }
3185
3186 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3187 << ParamType
3188 << FixItHint::CreateRemoval(AddrOpLoc);
3189 S.Diag(Param->getLocation(), diag::note_template_param_here);
3190
3191 ArgType = Func->getType();
3192 }
3193 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003194 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003195 S.Diag(Arg->getSourceRange().getBegin(),
3196 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003197 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003198 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003199 << true;
3200 return true;
3201 }
3202
Douglas Gregorb7a09262010-04-01 18:32:35 +00003203 // A value of reference type is not an object.
3204 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003205 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003206 diag::err_template_arg_reference_var)
3207 << Var->getType() << Arg->getSourceRange();
3208 S.Diag(Param->getLocation(), diag::note_template_param_here);
3209 return true;
3210 }
3211
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003212 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003213 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003214
3215 // If the template parameter has pointer type, we must have taken
3216 // the address of this object.
3217 if (ParamType->isReferenceType()) {
3218 if (AddressTaken) {
3219 // If we originally had an address-of operator, but the
3220 // parameter has reference type, complain and (if things look
3221 // like they will work) drop the address-of operator.
3222 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3223 ParamType.getNonReferenceType())) {
3224 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3225 << ParamType;
3226 S.Diag(Param->getLocation(), diag::note_template_param_here);
3227 return true;
3228 }
3229
3230 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3231 << ParamType
3232 << FixItHint::CreateRemoval(AddrOpLoc);
3233 S.Diag(Param->getLocation(), diag::note_template_param_here);
3234
3235 ArgType = Var->getType();
3236 }
3237 } else if (!AddressTaken && ParamType->isPointerType()) {
3238 if (Var->getType()->isArrayType()) {
3239 // Array-to-pointer decay.
3240 ArgType = S.Context.getArrayDecayedType(Var->getType());
3241 } else {
3242 // If the template parameter has pointer type but the address of
3243 // this object was not taken, complain and (possibly) recover by
3244 // taking the address of the entity.
3245 ArgType = S.Context.getPointerType(Var->getType());
3246 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3247 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3248 << ParamType;
3249 S.Diag(Param->getLocation(), diag::note_template_param_here);
3250 return true;
3251 }
3252
3253 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3254 << ParamType
3255 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3256
3257 S.Diag(Param->getLocation(), diag::note_template_param_here);
3258 }
3259 }
3260 } else {
3261 // We found something else, but we don't know specifically what it is.
3262 S.Diag(Arg->getSourceRange().getBegin(),
3263 diag::err_template_arg_not_object_or_func)
3264 << Arg->getSourceRange();
3265 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3266 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003267 }
Mike Stump1eb44332009-09-09 15:08:12 +00003268
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003269 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003270 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003271 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003272 // For pointer-to-object types, qualification conversions are
3273 // permitted.
3274 } else {
3275 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3276 if (!ParamRef->getPointeeType()->isFunctionType()) {
3277 // C++ [temp.arg.nontype]p5b3:
3278 // For a non-type template-parameter of type reference to
3279 // object, no conversions apply. The type referred to by the
3280 // reference may be more cv-qualified than the (otherwise
3281 // identical) type of the template- argument. The
3282 // template-parameter is bound directly to the
3283 // template-argument, which shall be an lvalue.
3284
3285 // FIXME: Other qualifiers?
3286 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3287 unsigned ArgQuals = ArgType.getCVRQualifiers();
3288
3289 if ((ParamQuals | ArgQuals) != ParamQuals) {
3290 S.Diag(Arg->getSourceRange().getBegin(),
3291 diag::err_template_arg_ref_bind_ignores_quals)
3292 << ParamType << Arg->getType()
3293 << Arg->getSourceRange();
3294 S.Diag(Param->getLocation(), diag::note_template_param_here);
3295 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003296 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003297 }
3298 }
3299
3300 // At this point, the template argument refers to an object or
3301 // function with external linkage. We now need to check whether the
3302 // argument and parameter types are compatible.
3303 if (!S.Context.hasSameUnqualifiedType(ArgType,
3304 ParamType.getNonReferenceType())) {
3305 // We can't perform this conversion or binding.
3306 if (ParamType->isReferenceType())
3307 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3308 << ParamType << Arg->getType() << Arg->getSourceRange();
3309 else
3310 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3311 << Arg->getType() << ParamType << Arg->getSourceRange();
3312 S.Diag(Param->getLocation(), diag::note_template_param_here);
3313 return true;
3314 }
3315 }
3316
3317 // Create the template argument.
3318 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003319 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003320 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003321}
3322
3323/// \brief Checks whether the given template argument is a pointer to
3324/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003325bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003326 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003327 bool Invalid = false;
3328
3329 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003330 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003331 Arg = Cast->getSubExpr();
3332
3333 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003334 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003335 // A template-argument for a non-type, non-template
3336 // template-parameter shall be one of: [...]
3337 //
3338 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003339 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003340
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003341 // In C++98/03 mode, give an extension warning on any extra parentheses.
3342 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3343 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003344 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003345 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003346 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003347 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003348 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003349 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003350 }
3351
3352 Arg = Parens->getSubExpr();
3353 }
3354
Douglas Gregorcaddba02009-11-12 18:38:13 +00003355 // A pointer-to-member constant written &Class::member.
3356 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003357 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003358 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3359 if (DRE && !DRE->getQualifier())
3360 DRE = 0;
3361 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003362 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003363 // A constant of pointer-to-member type.
3364 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3365 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3366 if (VD->getType()->isMemberPointerType()) {
3367 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003368 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003369 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3370 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003371 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003372 else
3373 Converted = TemplateArgument(VD->getCanonicalDecl());
3374 return Invalid;
3375 }
3376 }
3377 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003378
Douglas Gregorcaddba02009-11-12 18:38:13 +00003379 DRE = 0;
3380 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003381
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003382 if (!DRE)
3383 return Diag(Arg->getSourceRange().getBegin(),
3384 diag::err_template_arg_not_pointer_to_member_form)
3385 << Arg->getSourceRange();
3386
3387 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3388 assert((isa<FieldDecl>(DRE->getDecl()) ||
3389 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3390 "Only non-static member pointers can make it here");
3391
3392 // Okay: this is the address of a non-static member, and therefore
3393 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003394 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003395 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003396 else
3397 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003398 return Invalid;
3399 }
3400
3401 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003402 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003403 diag::err_template_arg_not_pointer_to_member_form)
3404 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003405 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003406 diag::note_template_arg_refers_here);
3407 return true;
3408}
3409
Douglas Gregorc15cb382009-02-09 23:23:08 +00003410/// \brief Check a template argument against its corresponding
3411/// non-type template parameter.
3412///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003413/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003414/// If an error occurred, it returns ExprError(); otherwise, it
3415/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003416/// InstantiatedParamType is the type of the non-type template
3417/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003418ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3419 QualType InstantiatedParamType, Expr *Arg,
3420 TemplateArgument &Converted,
3421 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003422 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3423
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003424 // If either the parameter has a dependent type or the argument is
3425 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003426 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3427 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003428 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003429 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003430 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003431
3432 // C++ [temp.arg.nontype]p5:
3433 // The following conversions are performed on each expression used
3434 // as a non-type template-argument. If a non-type
3435 // template-argument cannot be converted to the type of the
3436 // corresponding template-parameter then the program is
3437 // ill-formed.
3438 //
3439 // -- for a non-type template-parameter of integral or
3440 // enumeration type, integral promotions (4.5) and integral
3441 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003442 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003443 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003444 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003445 // C++ [temp.arg.nontype]p1:
3446 // A template-argument for a non-type, non-template
3447 // template-parameter shall be one of:
3448 //
3449 // -- an integral constant-expression of integral or enumeration
3450 // type; or
3451 // -- the name of a non-type template-parameter; or
3452 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003453 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003454 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003455 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003456 diag::err_template_arg_not_integral_or_enumeral)
3457 << ArgType << Arg->getSourceRange();
3458 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003459 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003460 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003461 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003462 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3463 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003464 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003465 }
3466
Douglas Gregor02024a92010-03-28 02:42:43 +00003467 // From here on out, all we care about are the unqualified forms
3468 // of the parameter and argument types.
3469 ParamType = ParamType.getUnqualifiedType();
3470 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003471
3472 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003473 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003474 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003475 } else if (CTAK == CTAK_Deduced) {
3476 // C++ [temp.deduct.type]p17:
3477 // If, in the declaration of a function template with a non-type
3478 // template-parameter, the non-type template- parameter is used
3479 // in an expression in the function parameter-list and, if the
3480 // corresponding template-argument is deduced, the
3481 // template-argument type shall match the type of the
3482 // template-parameter exactly, except that a template-argument
3483 // deduced from an array bound may be of any integral type.
3484 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3485 << ArgType << ParamType;
3486 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003487 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003488 } else if (ParamType->isBooleanType()) {
3489 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003490 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003491 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3492 !ParamType->isEnumeralType()) {
3493 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003494 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003495 } else {
3496 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003497 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003498 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003499 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003500 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003501 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003502 }
3503
Douglas Gregorc7469372011-05-04 21:55:00 +00003504 // Add the value of this argument to the list of converted
3505 // arguments. We use the bitwidth and signedness of the template
3506 // parameter.
3507 if (Arg->isValueDependent()) {
3508 // The argument is value-dependent. Create a new
3509 // TemplateArgument with the converted expression.
3510 Converted = TemplateArgument(Arg);
3511 return Owned(Arg);
3512 }
3513
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003514 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003515 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003516 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003517
Douglas Gregorc7469372011-05-04 21:55:00 +00003518 if (ParamType->isBooleanType()) {
3519 // Value must be zero or one.
3520 Value = Value != 0;
3521 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3522 if (Value.getBitWidth() != AllowedBits)
3523 Value = Value.extOrTrunc(AllowedBits);
3524 Value.setIsSigned(IntegerType->isSignedIntegerType());
3525 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003526 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003527
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003528 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003529 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003530 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003531 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003532 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003533 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003534
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003535 // Complain if an unsigned parameter received a negative value.
3536 if (IntegerType->isUnsignedIntegerType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003537 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003538 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3539 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3540 << Arg->getSourceRange();
3541 Diag(Param->getLocation(), diag::note_template_param_here);
3542 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003543
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003544 // Complain if we overflowed the template parameter's type.
3545 unsigned RequiredBits;
3546 if (IntegerType->isUnsignedIntegerType())
3547 RequiredBits = OldValue.getActiveBits();
3548 else if (OldValue.isUnsigned())
3549 RequiredBits = OldValue.getActiveBits() + 1;
3550 else
3551 RequiredBits = OldValue.getMinSignedBits();
3552 if (RequiredBits > AllowedBits) {
3553 Diag(Arg->getSourceRange().getBegin(),
3554 diag::warn_template_arg_too_large)
3555 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3556 << Arg->getSourceRange();
3557 Diag(Param->getLocation(), diag::note_template_param_here);
3558 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003559 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003560
John McCall833ca992009-10-29 08:12:44 +00003561 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003562 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003563 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003564 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003565 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003566
John McCall6bb80172010-03-30 21:47:33 +00003567 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3568
Douglas Gregorb7a09262010-04-01 18:32:35 +00003569 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3570 // from a template argument of type std::nullptr_t to a non-type
3571 // template parameter of type pointer to object, pointer to
3572 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003573 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003574 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3575 Converted = TemplateArgument((NamedDecl *)0);
John Wiegley429bb272011-04-08 18:41:53 +00003576 return Owned(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003577 }
3578
Douglas Gregorb86b0572009-02-11 01:18:59 +00003579 // Handle pointer-to-function, reference-to-function, and
3580 // pointer-to-member-function all in (roughly) the same way.
3581 if (// -- For a non-type template-parameter of type pointer to
3582 // function, only the function-to-pointer conversion (4.3) is
3583 // applied. If the template-argument represents a set of
3584 // overloaded functions (or a pointer to such), the matching
3585 // function is selected from the set (13.4).
3586 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003587 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003588 // -- For a non-type template-parameter of type reference to
3589 // function, no conversions apply. If the template-argument
3590 // represents a set of overloaded functions, the matching
3591 // function is selected from the set (13.4).
3592 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003593 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003594 // -- For a non-type template-parameter of type pointer to
3595 // member function, no conversions apply. If the
3596 // template-argument represents a set of overloaded member
3597 // functions, the matching member function is selected from
3598 // the set (13.4).
3599 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003600 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003601 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003602
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003603 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003604 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003605 true,
3606 FoundResult)) {
3607 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003608 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003609
3610 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3611 ArgType = Arg->getType();
3612 } else
John Wiegley429bb272011-04-08 18:41:53 +00003613 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003614 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003615
John Wiegley429bb272011-04-08 18:41:53 +00003616 if (!ParamType->isMemberPointerType()) {
3617 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3618 ParamType,
3619 Arg, Converted))
3620 return ExprError();
3621 return Owned(Arg);
3622 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003623
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003624 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3625 false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003626 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003627 } else if (!Context.hasSameUnqualifiedType(ArgType,
3628 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003629 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003630 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003631 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003632 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003633 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003634 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003635 }
Mike Stump1eb44332009-09-09 15:08:12 +00003636
John Wiegley429bb272011-04-08 18:41:53 +00003637 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3638 return ExprError();
3639 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003640 }
3641
Chris Lattnerfe90de72009-02-20 21:37:53 +00003642 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003643 // -- for a non-type template-parameter of type pointer to
3644 // object, qualification conversions (4.4) and the
3645 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003646 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003647 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003648 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003649
John Wiegley429bb272011-04-08 18:41:53 +00003650 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3651 ParamType,
3652 Arg, Converted))
3653 return ExprError();
3654 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003655 }
Mike Stump1eb44332009-09-09 15:08:12 +00003656
Ted Kremenek6217b802009-07-29 21:53:49 +00003657 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003658 // -- For a non-type template-parameter of type reference to
3659 // object, no conversions apply. The type referred to by the
3660 // reference may be more cv-qualified than the (otherwise
3661 // identical) type of the template-argument. The
3662 // template-parameter is bound directly to the
3663 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003664 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003665 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003666
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003667 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003668 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3669 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003670 true,
3671 FoundResult)) {
3672 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003673 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003674
3675 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3676 ArgType = Arg->getType();
3677 } else
John Wiegley429bb272011-04-08 18:41:53 +00003678 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003679 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003680
John Wiegley429bb272011-04-08 18:41:53 +00003681 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3682 ParamType,
3683 Arg, Converted))
3684 return ExprError();
3685 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003686 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003687
3688 // -- For a non-type template-parameter of type pointer to data
3689 // member, qualification conversions (4.4) are applied.
3690 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3691
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003692 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003693 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003694 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003695 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003696 } else {
3697 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003698 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003699 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003700 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003701 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003702 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003703 }
3704
John Wiegley429bb272011-04-08 18:41:53 +00003705 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3706 return ExprError();
3707 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003708}
3709
3710/// \brief Check a template argument against its corresponding
3711/// template template parameter.
3712///
3713/// This routine implements the semantics of C++ [temp.arg.template].
3714/// It returns true if an error occurred, and false otherwise.
3715bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003716 const TemplateArgumentLoc &Arg) {
3717 TemplateName Name = Arg.getArgument().getAsTemplate();
3718 TemplateDecl *Template = Name.getAsTemplateDecl();
3719 if (!Template) {
3720 // Any dependent template name is fine.
3721 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3722 return false;
3723 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003724
3725 // C++ [temp.arg.template]p1:
3726 // A template-argument for a template template-parameter shall be
3727 // the name of a class template, expressed as id-expression. Only
3728 // primary class templates are considered when matching the
3729 // template template argument with the corresponding parameter;
3730 // partial specializations are not considered even if their
3731 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003732 //
3733 // Note that we also allow template template parameters here, which
3734 // will happen when we are dealing with, e.g., class template
3735 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003736 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003737 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003738 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003739 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003740 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003741 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003742 << Template;
3743 }
3744
3745 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3746 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003747 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003748 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003749 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003750}
3751
Douglas Gregor02024a92010-03-28 02:42:43 +00003752/// \brief Given a non-type template argument that refers to a
3753/// declaration and the type of its corresponding non-type template
3754/// parameter, produce an expression that properly refers to that
3755/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003756ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003757Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3758 QualType ParamType,
3759 SourceLocation Loc) {
3760 assert(Arg.getKind() == TemplateArgument::Declaration &&
3761 "Only declaration template arguments permitted here");
3762 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3763
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003764 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003765 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3766 // If the value is a class member, we might have a pointer-to-member.
3767 // Determine whether the non-type template template parameter is of
3768 // pointer-to-member type. If so, we need to build an appropriate
3769 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3770 // would refer to the member itself.
3771 if (ParamType->isMemberPointerType()) {
3772 QualType ClassType
3773 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3774 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003775 = NestedNameSpecifier::Create(Context, 0, false,
3776 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003777 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003778 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003779
3780 // The actual value-ness of this is unimportant, but for
3781 // internal consistency's sake, references to instance methods
3782 // are r-values.
3783 ExprValueKind VK = VK_LValue;
3784 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3785 VK = VK_RValue;
3786
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003787 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003788 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003789 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003790 Loc,
3791 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003792 if (RefExpr.isInvalid())
3793 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003794
John McCall2de56d12010-08-25 11:45:40 +00003795 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003796
Douglas Gregorc0c83002010-04-30 21:46:38 +00003797 // We might need to perform a trailing qualification conversion, since
3798 // the element type on the parameter could be more qualified than the
3799 // element type in the expression we constructed.
3800 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John Wiegley429bb272011-04-08 18:41:53 +00003801 ParamType.getUnqualifiedType(), false))
3802 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003803
Douglas Gregor02024a92010-03-28 02:42:43 +00003804 assert(!RefExpr.isInvalid() &&
3805 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003806 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003807 return move(RefExpr);
3808 }
3809 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003810
Douglas Gregor02024a92010-03-28 02:42:43 +00003811 QualType T = VD->getType().getNonReferenceType();
3812 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003813 // When the non-type template parameter is a pointer, take the
3814 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003815 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003816 if (RefExpr.isInvalid())
3817 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003818
3819 if (T->isFunctionType() || T->isArrayType()) {
3820 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00003821 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
3822 if (RefExpr.isInvalid())
3823 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003824
3825 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003826 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003827
Douglas Gregorb7a09262010-04-01 18:32:35 +00003828 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003829 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003830 }
3831
John McCallf89e55a2010-11-18 06:31:45 +00003832 ExprValueKind VK = VK_RValue;
3833
Douglas Gregor02024a92010-03-28 02:42:43 +00003834 // If the non-type template parameter has reference type, qualify the
3835 // resulting declaration reference with the extra qualifiers on the
3836 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003837 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3838 VK = VK_LValue;
3839 T = Context.getQualifiedType(T,
3840 TargetRef->getPointeeType().getQualifiers());
3841 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003842
John McCallf89e55a2010-11-18 06:31:45 +00003843 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003844}
3845
3846/// \brief Construct a new expression that refers to the given
3847/// integral template argument with the given source-location
3848/// information.
3849///
3850/// This routine takes care of the mapping from an integral template
3851/// argument (which may have any integral type) to the appropriate
3852/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003853ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003854Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3855 SourceLocation Loc) {
3856 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003857 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003858 QualType T = Arg.getIntegralType();
3859 if (T->isCharType() || T->isWideCharType())
3860 return Owned(new (Context) CharacterLiteral(
3861 Arg.getAsIntegral()->getZExtValue(),
Chris Lattner223de242011-04-25 20:37:58 +00003862 T->isWideCharType(), T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00003863 if (T->isBooleanType())
3864 return Owned(new (Context) CXXBoolLiteralExpr(
3865 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00003866 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00003867
Chris Lattner223de242011-04-25 20:37:58 +00003868 // If this is an enum type that we're instantiating, we need to use an integer
3869 // type the same size as the enumerator. We don't want to build an
3870 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003871 QualType BT;
3872 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00003873 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003874 else
3875 BT = T;
3876
3877 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003878 if (T->isEnumeralType()) {
3879 // FIXME: This is a hack. We need a better way to handle substituted
3880 // non-type template parameters.
Chris Lattner223de242011-04-25 20:37:58 +00003881 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
3882 Context.getTrivialTypeSourceInfo(T, Loc),
3883 Loc, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003884 }
3885
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003886 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003887}
3888
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003889/// \brief Match two template parameters within template parameter lists.
3890static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3891 bool Complain,
3892 Sema::TemplateParameterListEqualKind Kind,
3893 SourceLocation TemplateArgLoc) {
3894 // Check the actual kind (type, non-type, template).
3895 if (Old->getKind() != New->getKind()) {
3896 if (Complain) {
3897 unsigned NextDiag = diag::err_template_param_different_kind;
3898 if (TemplateArgLoc.isValid()) {
3899 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3900 NextDiag = diag::note_template_param_different_kind;
3901 }
3902 S.Diag(New->getLocation(), NextDiag)
3903 << (Kind != Sema::TPL_TemplateMatch);
3904 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3905 << (Kind != Sema::TPL_TemplateMatch);
3906 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003907
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003908 return false;
3909 }
3910
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003911 // Check that both are parameter packs are neither are parameter packs.
3912 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003913 // template template parameter, the template template parameter can have
3914 // a parameter pack where the template template argument does not.
3915 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3916 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3917 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003918 if (Complain) {
3919 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3920 if (TemplateArgLoc.isValid()) {
3921 S.Diag(TemplateArgLoc,
3922 diag::err_template_arg_template_params_mismatch);
3923 NextDiag = diag::note_template_parameter_pack_non_pack;
3924 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003925
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003926 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3927 : isa<NonTypeTemplateParmDecl>(New)? 1
3928 : 2;
3929 S.Diag(New->getLocation(), NextDiag)
3930 << ParamKind << New->isParameterPack();
3931 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3932 << ParamKind << Old->isParameterPack();
3933 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003934
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003935 return false;
3936 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003937
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003938 // For non-type template parameters, check the type of the parameter.
3939 if (NonTypeTemplateParmDecl *OldNTTP
3940 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3941 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003942
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003943 // If we are matching a template template argument to a template
3944 // template parameter and one of the non-type template parameter types
3945 // is dependent, then we must wait until template instantiation time
3946 // to actually compare the arguments.
3947 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3948 (OldNTTP->getType()->isDependentType() ||
3949 NewNTTP->getType()->isDependentType()))
3950 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003951
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003952 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3953 if (Complain) {
3954 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3955 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003956 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003957 diag::err_template_arg_template_params_mismatch);
3958 NextDiag = diag::note_template_nontype_parm_different_type;
3959 }
3960 S.Diag(NewNTTP->getLocation(), NextDiag)
3961 << NewNTTP->getType()
3962 << (Kind != Sema::TPL_TemplateMatch);
3963 S.Diag(OldNTTP->getLocation(),
3964 diag::note_template_nontype_parm_prev_declaration)
3965 << OldNTTP->getType();
3966 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003967
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003968 return false;
3969 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003970
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003971 return true;
3972 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003973
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003974 // For template template parameters, check the template parameter types.
3975 // The template parameter lists of template template
3976 // parameters must agree.
3977 if (TemplateTemplateParmDecl *OldTTP
3978 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003979 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003980 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3981 OldTTP->getTemplateParameters(),
3982 Complain,
3983 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003984 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003985 : Kind),
3986 TemplateArgLoc);
3987 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003988
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003989 return true;
3990}
Douglas Gregor02024a92010-03-28 02:42:43 +00003991
Douglas Gregora0347822011-01-13 00:08:50 +00003992/// \brief Diagnose a known arity mismatch when comparing template argument
3993/// lists.
3994static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003995void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003996 TemplateParameterList *New,
3997 TemplateParameterList *Old,
3998 Sema::TemplateParameterListEqualKind Kind,
3999 SourceLocation TemplateArgLoc) {
4000 unsigned NextDiag = diag::err_template_param_list_different_arity;
4001 if (TemplateArgLoc.isValid()) {
4002 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4003 NextDiag = diag::note_template_param_list_different_arity;
4004 }
4005 S.Diag(New->getTemplateLoc(), NextDiag)
4006 << (New->size() > Old->size())
4007 << (Kind != Sema::TPL_TemplateMatch)
4008 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4009 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4010 << (Kind != Sema::TPL_TemplateMatch)
4011 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4012}
4013
Douglas Gregorddc29e12009-02-06 22:42:48 +00004014/// \brief Determine whether the given template parameter lists are
4015/// equivalent.
4016///
Mike Stump1eb44332009-09-09 15:08:12 +00004017/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004018/// source code as part of a new template declaration.
4019///
4020/// \param Old The old template parameter list, typically found via
4021/// name lookup of the template declared with this template parameter
4022/// list.
4023///
4024/// \param Complain If true, this routine will produce a diagnostic if
4025/// the template parameter lists are not equivalent.
4026///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004027/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004028///
4029/// \param TemplateArgLoc If this source location is valid, then we
4030/// are actually checking the template parameter list of a template
4031/// argument (New) against the template parameter list of its
4032/// corresponding template template parameter (Old). We produce
4033/// slightly different diagnostics in this scenario.
4034///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004035/// \returns True if the template parameter lists are equal, false
4036/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004037bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004038Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4039 TemplateParameterList *Old,
4040 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004041 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004042 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004043 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4044 if (Complain)
4045 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4046 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004047
4048 return false;
4049 }
4050
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004051 // C++0x [temp.arg.template]p3:
4052 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004053 // when each of the template parameters in the template-parameter-list of
4054 // the template-argument's corresponding class template or template alias
4055 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004056 // template-parameter-list of P. [...]
4057 TemplateParameterList::iterator NewParm = New->begin();
4058 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004059 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004060 OldParmEnd = Old->end();
4061 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004062 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4063 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004064 if (NewParm == NewParmEnd) {
4065 if (Complain)
4066 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4067 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004068
Douglas Gregora0347822011-01-13 00:08:50 +00004069 return false;
4070 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004071
Douglas Gregora0347822011-01-13 00:08:50 +00004072 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4073 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004074 return false;
4075
Douglas Gregora0347822011-01-13 00:08:50 +00004076 ++NewParm;
4077 continue;
4078 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004079
Douglas Gregora0347822011-01-13 00:08:50 +00004080 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004081 // [...] When P's template- parameter-list contains a template parameter
4082 // pack (14.5.3), the template parameter pack will match zero or more
4083 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004084 // template-parameter-list of A with the same type and form as the
4085 // template parameter pack in P (ignoring whether those template
4086 // parameters are template parameter packs).
4087 for (; NewParm != NewParmEnd; ++NewParm) {
4088 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4089 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004090 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004091 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004092 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004093
Douglas Gregora0347822011-01-13 00:08:50 +00004094 // Make sure we exhausted all of the arguments.
4095 if (NewParm != NewParmEnd) {
4096 if (Complain)
4097 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4098 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004099
Douglas Gregora0347822011-01-13 00:08:50 +00004100 return false;
4101 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004102
Douglas Gregorddc29e12009-02-06 22:42:48 +00004103 return true;
4104}
4105
4106/// \brief Check whether a template can be declared within this scope.
4107///
4108/// If the template declaration is valid in this scope, returns
4109/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004110bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004111Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004112 // Find the nearest enclosing declaration scope.
4113 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4114 (S->getFlags() & Scope::TemplateParamScope) != 0)
4115 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004116
Douglas Gregorddc29e12009-02-06 22:42:48 +00004117 // C++ [temp]p2:
4118 // A template-declaration can appear only as a namespace scope or
4119 // class scope declaration.
4120 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004121 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4122 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004123 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004124 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004125
Eli Friedman1503f772009-07-31 01:43:05 +00004126 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004127 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004128
4129 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4130 return false;
4131
Mike Stump1eb44332009-09-09 15:08:12 +00004132 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004133 diag::err_template_outside_namespace_or_class_scope)
4134 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004135}
Douglas Gregorcc636682009-02-17 23:15:12 +00004136
Douglas Gregord5cb8762009-10-07 00:13:32 +00004137/// \brief Determine what kind of template specialization the given declaration
4138/// is.
4139static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4140 if (!D)
4141 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004142
Douglas Gregorf6b11852009-10-08 15:14:33 +00004143 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4144 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004145 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4146 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004147 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4148 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004149
Douglas Gregord5cb8762009-10-07 00:13:32 +00004150 return TSK_Undeclared;
4151}
4152
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004153/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004154/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004155///
Douglas Gregor9302da62009-10-14 23:50:59 +00004156/// This routine determines whether a template specialization can be declared
4157/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004158///
4159/// \param S the semantic analysis object for which this check is being
4160/// performed.
4161///
4162/// \param Specialized the entity being specialized or instantiated, which
4163/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004164/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004165/// member class).
4166///
4167/// \param PrevDecl the previous declaration of this entity, if any.
4168///
4169/// \param Loc the location of the explicit specialization or instantiation of
4170/// this entity.
4171///
4172/// \param IsPartialSpecialization whether this is a partial specialization of
4173/// a class template.
4174///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004175/// \returns true if there was an error that we cannot recover from, false
4176/// otherwise.
4177static bool CheckTemplateSpecializationScope(Sema &S,
4178 NamedDecl *Specialized,
4179 NamedDecl *PrevDecl,
4180 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004181 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004182 // Keep these "kind" numbers in sync with the %select statements in the
4183 // various diagnostics emitted by this routine.
4184 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004185 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004186 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004187 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004188 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004189 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004190 EntityKind = 3;
4191 else if (isa<VarDecl>(Specialized))
4192 EntityKind = 4;
4193 else if (isa<RecordDecl>(Specialized))
4194 EntityKind = 5;
4195 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004196 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4197 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004198 return true;
4199 }
4200
Douglas Gregor88b70942009-02-25 22:02:03 +00004201 // C++ [temp.expl.spec]p2:
4202 // An explicit specialization shall be declared in the namespace
4203 // of which the template is a member, or, for member templates, in
4204 // the namespace of which the enclosing class or enclosing class
4205 // template is a member. An explicit specialization of a member
4206 // function, member class or static data member of a class
4207 // template shall be declared in the namespace of which the class
4208 // template is a member. Such a declaration may also be a
4209 // definition. If the declaration is not a definition, the
4210 // specialization may be defined later in the name- space in which
4211 // the explicit specialization was declared, or in a namespace
4212 // that encloses the one in which the explicit specialization was
4213 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004214 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004215 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004216 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004217 return true;
4218 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004219
Douglas Gregor0a407472009-10-07 17:30:37 +00004220 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4221 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004222 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004223 return true;
4224 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004225
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004226 // C++ [temp.class.spec]p6:
4227 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004228 // in any namespace scope in which its definition may be defined (14.5.1
4229 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004230 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004231 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004232 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004233 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004234 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004235 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4236 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004237 // C++ [temp.exp.spec]p2:
4238 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004239 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004240 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004241 // An explicit specialization of a member function, member class or
4242 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004243 // namespace of which the class template is a member.
4244 //
4245 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004246 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004247 // the specialized template.
4248 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4249 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004250 bool IsCPlusPlus0xExtension
4251 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004252 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004253 S.Diag(Loc, IsCPlusPlus0xExtension
4254 ? diag::ext_template_spec_decl_out_of_scope_global
4255 : diag::err_template_spec_decl_out_of_scope_global)
4256 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004257 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004258 S.Diag(Loc, IsCPlusPlus0xExtension
4259 ? diag::ext_template_spec_decl_out_of_scope
4260 : diag::err_template_spec_decl_out_of_scope)
4261 << EntityKind << Specialized
4262 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004263
Douglas Gregor9302da62009-10-14 23:50:59 +00004264 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4265 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004266 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004267 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004268
4269 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004270 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004271 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004272 // specializations of function templates, static data members, and member
4273 // functions, so we skip the check here for those kinds of entities.
4274 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004275 // Should we refactor that check, so that it occurs later?
4276 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004277 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4278 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004279 if (isa<TranslationUnitDecl>(SpecializedContext))
4280 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4281 << EntityKind << Specialized;
4282 else if (isa<NamespaceDecl>(SpecializedContext))
4283 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4284 << EntityKind << Specialized
4285 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004286
Douglas Gregor9302da62009-10-14 23:50:59 +00004287 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004288 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004289
Douglas Gregord5cb8762009-10-07 00:13:32 +00004290 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004291
Douglas Gregor88b70942009-02-25 22:02:03 +00004292 return false;
4293}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004294
Douglas Gregorbacb9492011-01-03 21:13:47 +00004295/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4296/// that checks non-type template partial specialization arguments.
4297static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4298 NonTypeTemplateParmDecl *Param,
4299 const TemplateArgument *Args,
4300 unsigned NumArgs) {
4301 for (unsigned I = 0; I != NumArgs; ++I) {
4302 if (Args[I].getKind() == TemplateArgument::Pack) {
4303 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004304 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004305 Args[I].pack_size()))
4306 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004307
Douglas Gregore94866f2009-06-12 21:21:02 +00004308 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004309 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004310
Douglas Gregorbacb9492011-01-03 21:13:47 +00004311 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004312 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004313 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004314 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004315
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004316 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004317 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4318 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004319
4320 // Strip off any implicit casts we added as part of type checking.
4321 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4322 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004323
Douglas Gregore94866f2009-06-12 21:21:02 +00004324 // C++ [temp.class.spec]p8:
4325 // A non-type argument is non-specialized if it is the name of a
4326 // non-type parameter. All other non-type arguments are
4327 // specialized.
4328 //
4329 // Below, we check the two conditions that only apply to
4330 // specialized non-type arguments, so skip any non-specialized
4331 // arguments.
4332 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004333 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004334 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004335
Douglas Gregore94866f2009-06-12 21:21:02 +00004336 // C++ [temp.class.spec]p9:
4337 // Within the argument list of a class template partial
4338 // specialization, the following restrictions apply:
4339 // -- A partially specialized non-type argument expression
4340 // shall not involve a template parameter of the partial
4341 // specialization except when the argument expression is a
4342 // simple identifier.
4343 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004344 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004345 diag::err_dependent_non_type_arg_in_partial_spec)
4346 << ArgExpr->getSourceRange();
4347 return true;
4348 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004349
Douglas Gregore94866f2009-06-12 21:21:02 +00004350 // -- The type of a template parameter corresponding to a
4351 // specialized non-type argument shall not be dependent on a
4352 // parameter of the specialization.
4353 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004354 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004355 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4356 << Param->getType()
4357 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004358 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004359 return true;
4360 }
4361 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004362
Douglas Gregorbacb9492011-01-03 21:13:47 +00004363 return false;
4364}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004365
Douglas Gregorbacb9492011-01-03 21:13:47 +00004366/// \brief Check the non-type template arguments of a class template
4367/// partial specialization according to C++ [temp.class.spec]p9.
4368///
4369/// \param TemplateParams the template parameters of the primary class
4370/// template.
4371///
4372/// \param TemplateArg the template arguments of the class template
4373/// partial specialization.
4374///
4375/// \returns true if there was an error, false otherwise.
4376static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4377 TemplateParameterList *TemplateParams,
4378 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4379 const TemplateArgument *ArgList = TemplateArgs.data();
4380
4381 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4382 NonTypeTemplateParmDecl *Param
4383 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4384 if (!Param)
4385 continue;
4386
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004387 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004388 &ArgList[I], 1))
4389 return true;
4390 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004391
4392 return false;
4393}
4394
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004395/// \brief Retrieve the previous declaration of the given declaration.
4396static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4397 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4398 return VD->getPreviousDeclaration();
4399 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4400 return FD->getPreviousDeclaration();
4401 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4402 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004403 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004404 return TD->getPreviousDeclaration();
4405 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4406 return FTD->getPreviousDeclaration();
4407 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4408 return CTD->getPreviousDeclaration();
4409 return 0;
4410}
4411
John McCalld226f652010-08-21 09:40:31 +00004412DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004413Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4414 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004415 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004416 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004417 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004418 SourceLocation TemplateNameLoc,
4419 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004420 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004421 SourceLocation RAngleLoc,
4422 AttributeList *Attr,
4423 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004424 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004425
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004426 // NOTE: KWLoc is the location of the tag keyword. This will instead
4427 // store the location of the outermost template keyword in the declaration.
4428 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4429 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4430
Douglas Gregorcc636682009-02-17 23:15:12 +00004431 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004432 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004433 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004434 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4435
4436 if (!ClassTemplate) {
4437 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004438 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004439 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4440 return true;
4441 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004442
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004443 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004444 bool isPartialSpecialization = false;
4445
Douglas Gregor88b70942009-02-25 22:02:03 +00004446 // Check the validity of the template headers that introduce this
4447 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004448 // FIXME: We probably shouldn't complain about these headers for
4449 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004450 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004451 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004452 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4453 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004454 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004455 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004456 isExplicitSpecialization,
4457 Invalid);
4458 if (Invalid)
4459 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004460
Douglas Gregor05396e22009-08-25 17:23:04 +00004461 if (TemplateParams && TemplateParams->size() > 0) {
4462 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004463
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004464 if (TUK == TUK_Friend) {
4465 Diag(KWLoc, diag::err_partial_specialization_friend)
4466 << SourceRange(LAngleLoc, RAngleLoc);
4467 return true;
4468 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004469
Douglas Gregor05396e22009-08-25 17:23:04 +00004470 // C++ [temp.class.spec]p10:
4471 // The template parameter list of a specialization shall not
4472 // contain default template argument values.
4473 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4474 Decl *Param = TemplateParams->getParam(I);
4475 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4476 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004477 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004478 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004479 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004480 }
4481 } else if (NonTypeTemplateParmDecl *NTTP
4482 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4483 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004484 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004485 diag::err_default_arg_in_partial_spec)
4486 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004487 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004488 }
4489 } else {
4490 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004491 if (TTP->hasDefaultArgument()) {
4492 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004493 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004494 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004495 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004496 }
4497 }
4498 }
Douglas Gregora735b202009-10-13 14:39:41 +00004499 } else if (TemplateParams) {
4500 if (TUK == TUK_Friend)
4501 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004502 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004503 SourceRange(TemplateParams->getTemplateLoc(),
4504 TemplateParams->getRAngleLoc()))
4505 << SourceRange(LAngleLoc, RAngleLoc);
4506 else
4507 isExplicitSpecialization = true;
4508 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004509 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004510 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004511 isExplicitSpecialization = true;
4512 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004513
Douglas Gregorcc636682009-02-17 23:15:12 +00004514 // Check that the specialization uses the same tag kind as the
4515 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004516 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4517 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004518 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004519 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004520 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004521 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004522 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004523 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004524 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004525 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004526 diag::note_previous_use);
4527 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4528 }
4529
Douglas Gregor40808ce2009-03-09 23:48:35 +00004530 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004531 TemplateArgumentListInfo TemplateArgs;
4532 TemplateArgs.setLAngleLoc(LAngleLoc);
4533 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004534 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004535
Douglas Gregor925910d2011-01-03 20:35:03 +00004536 // Check for unexpanded parameter packs in any of the template arguments.
4537 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004538 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004539 UPPC_PartialSpecialization))
4540 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004541
Douglas Gregorcc636682009-02-17 23:15:12 +00004542 // Check that the template argument list is well-formed for this
4543 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004544 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004545 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4546 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004547 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004548
Douglas Gregor910f8002010-11-07 23:05:16 +00004549 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004550 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004551
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004552 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004553 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004554 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004555 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004556 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004557 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004558 return true;
4559
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004560 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004561 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004562 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004563 TemplateArgs.size())) {
4564 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4565 << ClassTemplate->getDeclName();
4566 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004567 }
4568 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004569
Douglas Gregorcc636682009-02-17 23:15:12 +00004570 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004571 ClassTemplateSpecializationDecl *PrevDecl = 0;
4572
4573 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004574 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004575 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004576 = ClassTemplate->findPartialSpecialization(Converted.data(),
4577 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004578 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004579 else
4580 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004581 = ClassTemplate->findSpecialization(Converted.data(),
4582 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004583
4584 ClassTemplateSpecializationDecl *Specialization = 0;
4585
Douglas Gregor88b70942009-02-25 22:02:03 +00004586 // Check whether we can declare a class template specialization in
4587 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004588 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004589 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4590 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004591 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004592 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004593
Douglas Gregorb88e8882009-07-30 17:40:51 +00004594 // The canonical type
4595 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004596 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004597 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004598 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004599 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004600 // arguments was referenced but not declared, or we're only
4601 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004602 // declaration node as our own, updating its source location and
4603 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004604 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004605 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004606 if (TemplateParameterLists.size() > 0) {
4607 Specialization->setTemplateParameterListsInfo(Context,
4608 TemplateParameterLists.size(),
4609 (TemplateParameterList**) TemplateParameterLists.release());
4610 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004611 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004612 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004613 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004614 // Build the canonical type that describes the converted template
4615 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004616 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4617 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004618 Converted.data(),
4619 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004620
4621 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004622 ClassTemplate->getInjectedClassNameSpecialization())) {
4623 // C++ [temp.class.spec]p9b3:
4624 //
4625 // -- The argument list of the specialization shall not be identical
4626 // to the implicit argument list of the primary template.
4627 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4628 << (TUK == TUK_Definition)
4629 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4630 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4631 ClassTemplate->getIdentifier(),
4632 TemplateNameLoc,
4633 Attr,
4634 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004635 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004636 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004637 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004638 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004639
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004640 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004641 ClassTemplatePartialSpecializationDecl *PrevPartial
4642 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004643 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004644 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004645 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004646 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004647 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004648 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004649 TemplateParams,
4650 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004651 Converted.data(),
4652 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004653 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004654 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004655 PrevPartial,
4656 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004657 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004658 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004659 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004660 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004661 (TemplateParameterList**) TemplateParameterLists.release());
4662 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004663
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004664 if (!PrevPartial)
4665 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004666 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004667
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004668 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004669 // template specialization, make a note of that.
4670 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4671 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004672
Douglas Gregor031a5882009-06-13 00:26:55 +00004673 // Check that all of the template parameters of the class template
4674 // partial specialization are deducible from the template
4675 // arguments. If not, this class template partial specialization
4676 // will never be used.
4677 llvm::SmallVector<bool, 8> DeducibleParams;
4678 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004679 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004680 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004681 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004682 unsigned NumNonDeducible = 0;
4683 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4684 if (!DeducibleParams[I])
4685 ++NumNonDeducible;
4686
4687 if (NumNonDeducible) {
4688 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4689 << (NumNonDeducible > 1)
4690 << SourceRange(TemplateNameLoc, RAngleLoc);
4691 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4692 if (!DeducibleParams[I]) {
4693 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4694 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004695 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004696 diag::note_partial_spec_unused_parameter)
4697 << Param->getDeclName();
4698 else
Mike Stump1eb44332009-09-09 15:08:12 +00004699 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004700 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004701 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004702 }
4703 }
4704 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004705 } else {
4706 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004707 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004708 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004709 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004710 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004711 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004712 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004713 Converted.data(),
4714 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004715 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004716 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004717 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004718 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004719 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00004720 (TemplateParameterList**) TemplateParameterLists.release());
4721 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004722
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004723 if (!PrevDecl)
4724 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004725
4726 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004727 }
4728
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004729 // C++ [temp.expl.spec]p6:
4730 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004731 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004732 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004733 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004734 // use occurs; no diagnostic is required.
4735 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004736 bool Okay = false;
4737 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4738 // Is there any previous explicit specialization declaration?
4739 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4740 Okay = true;
4741 break;
4742 }
4743 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004744
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004745 if (!Okay) {
4746 SourceRange Range(TemplateNameLoc, RAngleLoc);
4747 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4748 << Context.getTypeDeclType(Specialization) << Range;
4749
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004750 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004751 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004752 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004753 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004754 return true;
4755 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004756 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004757
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004758 // If this is not a friend, note that this is an explicit specialization.
4759 if (TUK != TUK_Friend)
4760 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004761
4762 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004763 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004764 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004765 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004766 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004767 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004768 Diag(Def->getLocation(), diag::note_previous_definition);
4769 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004770 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004771 }
4772 }
4773
John McCall7f1b9872010-12-18 03:30:47 +00004774 if (Attr)
4775 ProcessDeclAttributeList(S, Specialization, Attr);
4776
Douglas Gregorfc705b82009-02-26 22:19:44 +00004777 // Build the fully-sugared type for this class template
4778 // specialization as the user wrote in the specialization
4779 // itself. This means that we'll pretty-print the type retrieved
4780 // from the specialization's declaration the way that the user
4781 // actually wrote the specialization, rather than formatting the
4782 // name based on the "canonical" representation used to store the
4783 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004784 TypeSourceInfo *WrittenTy
4785 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4786 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004787 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004788 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004789 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004790 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004791 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004792
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004793 // C++ [temp.expl.spec]p9:
4794 // A template explicit specialization is in the scope of the
4795 // namespace in which the template was defined.
4796 //
4797 // We actually implement this paragraph where we set the semantic
4798 // context (in the creation of the ClassTemplateSpecializationDecl),
4799 // but we also maintain the lexical context where the actual
4800 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004801 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004802
Douglas Gregorcc636682009-02-17 23:15:12 +00004803 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004804 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004805 Specialization->startDefinition();
4806
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004807 if (TUK == TUK_Friend) {
4808 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4809 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004810 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004811 /*FIXME:*/KWLoc);
4812 Friend->setAccess(AS_public);
4813 CurContext->addDecl(Friend);
4814 } else {
4815 // Add the specialization into its lexical context, so that it can
4816 // be seen when iterating through the list of declarations in that
4817 // context. However, specializations are not found by name lookup.
4818 CurContext->addDecl(Specialization);
4819 }
John McCalld226f652010-08-21 09:40:31 +00004820 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004821}
Douglas Gregord57959a2009-03-27 23:10:48 +00004822
John McCalld226f652010-08-21 09:40:31 +00004823Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004824 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004825 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004826 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4827}
4828
John McCalld226f652010-08-21 09:40:31 +00004829Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004830 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004831 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004832 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004833 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004834
Douglas Gregor52591bf2009-06-24 00:54:41 +00004835 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004836 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004837 }
Mike Stump1eb44332009-09-09 15:08:12 +00004838
Douglas Gregor52591bf2009-06-24 00:54:41 +00004839 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004840
John McCalld226f652010-08-21 09:40:31 +00004841 Decl *DP = HandleDeclarator(ParentScope, D,
4842 move(TemplateParameterLists),
4843 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004844 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004845 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004846 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004847 FunctionTemplate->getTemplatedDecl());
4848 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4849 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4850 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004851}
4852
John McCall75042392010-02-11 01:33:53 +00004853/// \brief Strips various properties off an implicit instantiation
4854/// that has just been explicitly specialized.
4855static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004856 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004857
4858 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4859 FD->setInlineSpecified(false);
4860 }
4861}
4862
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004863/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004864/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004865/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004866/// new specialization/instantiation will have any effect.
4867///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004868/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004869/// instantiation.
4870///
4871/// \param NewTSK the kind of the new explicit specialization or instantiation.
4872///
4873/// \param PrevDecl the previous declaration of the entity.
4874///
4875/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4876///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004877/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004878/// declaration was instantiated (either implicitly or explicitly).
4879///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004880/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004881/// specialization or instantiation has no effect and should be ignored.
4882///
4883/// \returns true if there was an error that should prevent the introduction of
4884/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004885bool
4886Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4887 TemplateSpecializationKind NewTSK,
4888 NamedDecl *PrevDecl,
4889 TemplateSpecializationKind PrevTSK,
4890 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004891 bool &HasNoEffect) {
4892 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004893
Douglas Gregor454885e2009-10-15 15:54:05 +00004894 switch (NewTSK) {
4895 case TSK_Undeclared:
4896 case TSK_ImplicitInstantiation:
4897 assert(false && "Don't check implicit instantiations here");
4898 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004899
Douglas Gregor454885e2009-10-15 15:54:05 +00004900 case TSK_ExplicitSpecialization:
4901 switch (PrevTSK) {
4902 case TSK_Undeclared:
4903 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004904 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004905 // explicitly specialized or has merely been mentioned without any
4906 // instantiation.
4907 return false;
4908
4909 case TSK_ImplicitInstantiation:
4910 if (PrevPointOfInstantiation.isInvalid()) {
4911 // The declaration itself has not actually been instantiated, so it is
4912 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004913 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004914 return false;
4915 }
4916 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004917
Douglas Gregor454885e2009-10-15 15:54:05 +00004918 case TSK_ExplicitInstantiationDeclaration:
4919 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004920 assert((PrevTSK == TSK_ImplicitInstantiation ||
4921 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004922 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004923
Douglas Gregor454885e2009-10-15 15:54:05 +00004924 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004925 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004926 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004927 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004928 // implicit instantiation to take place, in every translation unit in
4929 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004930 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4931 // Is there any previous explicit specialization declaration?
4932 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4933 return false;
4934 }
4935
Douglas Gregor0d035142009-10-27 18:42:08 +00004936 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004937 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004938 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004939 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004940
Douglas Gregor454885e2009-10-15 15:54:05 +00004941 return true;
4942 }
4943 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004944
Douglas Gregor454885e2009-10-15 15:54:05 +00004945 case TSK_ExplicitInstantiationDeclaration:
4946 switch (PrevTSK) {
4947 case TSK_ExplicitInstantiationDeclaration:
4948 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004949 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004950 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004951
Douglas Gregor454885e2009-10-15 15:54:05 +00004952 case TSK_Undeclared:
4953 case TSK_ImplicitInstantiation:
4954 // We're explicitly instantiating something that may have already been
4955 // implicitly instantiated; that's fine.
4956 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004957
Douglas Gregor454885e2009-10-15 15:54:05 +00004958 case TSK_ExplicitSpecialization:
4959 // C++0x [temp.explicit]p4:
4960 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004961 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004962 // specialization for that template, the explicit instantiation has no
4963 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004964 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004965 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004966
Douglas Gregor454885e2009-10-15 15:54:05 +00004967 case TSK_ExplicitInstantiationDefinition:
4968 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004969 // If an entity is the subject of both an explicit instantiation
4970 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004971 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004972 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004973 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004974 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004975 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004976 assert(PrevPointOfInstantiation.isValid() &&
4977 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004978 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004979 return false;
4980 }
4981 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004982
Douglas Gregor454885e2009-10-15 15:54:05 +00004983 case TSK_ExplicitInstantiationDefinition:
4984 switch (PrevTSK) {
4985 case TSK_Undeclared:
4986 case TSK_ImplicitInstantiation:
4987 // We're explicitly instantiating something that may have already been
4988 // implicitly instantiated; that's fine.
4989 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004990
Douglas Gregor454885e2009-10-15 15:54:05 +00004991 case TSK_ExplicitSpecialization:
4992 // C++ DR 259, C++0x [temp.explicit]p4:
4993 // For a given set of template parameters, if an explicit
4994 // instantiation of a template appears after a declaration of
4995 // an explicit specialization for that template, the explicit
4996 // instantiation has no effect.
4997 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004998 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004999 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005000 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005001 if (!getLangOptions().CPlusPlus0x) {
5002 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005003 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005004 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005005 diag::note_previous_template_specialization);
5006 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005007 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005008 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005009
Douglas Gregor454885e2009-10-15 15:54:05 +00005010 case TSK_ExplicitInstantiationDeclaration:
5011 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005012 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005013 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005014
Douglas Gregor454885e2009-10-15 15:54:05 +00005015 case TSK_ExplicitInstantiationDefinition:
5016 // C++0x [temp.spec]p5:
5017 // For a given template and a given set of template-arguments,
5018 // - an explicit instantiation definition shall appear at most once
5019 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005020 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005021 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005022 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005023 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005024 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005025 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005026 }
5027 break;
5028 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005029
Douglas Gregor454885e2009-10-15 15:54:05 +00005030 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005031
Douglas Gregor454885e2009-10-15 15:54:05 +00005032 return false;
5033}
5034
John McCallaf2094e2010-04-08 09:05:18 +00005035/// \brief Perform semantic analysis for the given dependent function
5036/// template specialization. The only possible way to get a dependent
5037/// function template specialization is with a friend declaration,
5038/// like so:
5039///
5040/// template <class T> void foo(T);
5041/// template <class T> class A {
5042/// friend void foo<>(T);
5043/// };
5044///
5045/// There really isn't any useful analysis we can do here, so we
5046/// just store the information.
5047bool
5048Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5049 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5050 LookupResult &Previous) {
5051 // Remove anything from Previous that isn't a function template in
5052 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005053 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005054 LookupResult::Filter F = Previous.makeFilter();
5055 while (F.hasNext()) {
5056 NamedDecl *D = F.next()->getUnderlyingDecl();
5057 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005058 !FDLookupContext->InEnclosingNamespaceSetOf(
5059 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005060 F.erase();
5061 }
5062 F.done();
5063
5064 // Should this be diagnosed here?
5065 if (Previous.empty()) return true;
5066
5067 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5068 ExplicitTemplateArgs);
5069 return false;
5070}
5071
Abramo Bagnarae03db982010-05-20 15:32:11 +00005072/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005073/// specialization.
5074///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005075/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005076/// explicit function template specialization. On successful completion,
5077/// the function declaration \p FD will become a function template
5078/// specialization.
5079///
5080/// \param FD the function declaration, which will be updated to become a
5081/// function template specialization.
5082///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005083/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5084/// if any. Note that this may be valid info even when 0 arguments are
5085/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5086/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005087///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005088/// \param PrevDecl the set of declarations that may be specialized by
5089/// this function specialization.
5090bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005091Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005092 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005093 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005094 // The set of function template specializations that could match this
5095 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005096 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005097
Sebastian Redl7a126a42010-08-31 00:36:30 +00005098 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005099 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5100 I != E; ++I) {
5101 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5102 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005103 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005104 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005105 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5106 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005107 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005108
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005109 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005110 // A trailing template-argument can be left unspecified in the
5111 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005112 // provided it can be deduced from the function argument type.
5113 // Perform template argument deduction to determine whether we may be
5114 // specializing this template.
5115 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005116 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005117 FunctionDecl *Specialization = 0;
5118 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005119 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005120 FD->getType(),
5121 Specialization,
5122 Info)) {
5123 // FIXME: Template argument deduction failed; record why it failed, so
5124 // that we can provide nifty diagnostics.
5125 (void)TDK;
5126 continue;
5127 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005128
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005129 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005130 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005131 }
5132 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005133
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005134 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005135 UnresolvedSetIterator Result
5136 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005137 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005138 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005139 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005140 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005141 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005142 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005143 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005144 return true;
John McCallc373d482010-01-27 01:50:18 +00005145
5146 // Ignore access information; it doesn't figure into redeclaration checking.
5147 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005148
5149 FunctionTemplateSpecializationInfo *SpecInfo
5150 = Specialization->getTemplateSpecializationInfo();
5151 assert(SpecInfo && "Function template specialization info missing?");
5152 {
5153 // Note: do not overwrite location info if previous template
5154 // specialization kind was explicit.
5155 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5156 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5157 Specialization->setLocation(FD->getLocation());
5158 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005159
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005160 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005161 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005162
5163 // If this is a friend declaration, then we're not really declaring
5164 // an explicit specialization.
5165 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005166
Douglas Gregord5cb8762009-10-07 00:13:32 +00005167 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005168 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005169 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005170 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005171 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005172 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005173 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005174
5175 // C++ [temp.expl.spec]p6:
5176 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005177 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005178 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005179 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005180 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005181 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005182 if (!isFriend &&
5183 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005184 TSK_ExplicitSpecialization,
5185 Specialization,
5186 SpecInfo->getTemplateSpecializationKind(),
5187 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005188 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005189 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005190
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005191 // Mark the prior declaration as an explicit specialization, so that later
5192 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005193 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005194 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005195 MarkUnusedFileScopedDecl(Specialization);
5196 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005197
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005198 // Turn the given function declaration into a function template
5199 // specialization, with the template arguments from the previous
5200 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005201 // Take copies of (semantic and syntactic) template argument lists.
5202 const TemplateArgumentList* TemplArgs = new (Context)
5203 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5204 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5205 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005206 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005207 TemplArgs, /*InsertPos=*/0,
5208 SpecInfo->getTemplateSpecializationKind(),
5209 TemplArgsAsWritten);
5210
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005211 // The "previous declaration" for this function template specialization is
5212 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005213 Previous.clear();
5214 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005215 return false;
5216}
5217
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005218/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005219/// specialization.
5220///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005221/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005222/// explicit member function specialization. On successful completion,
5223/// the function declaration \p FD will become a member function
5224/// specialization.
5225///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005226/// \param Member the member declaration, which will be updated to become a
5227/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005228///
John McCall68263142009-11-18 22:49:29 +00005229/// \param Previous the set of declarations, one of which may be specialized
5230/// by this function specialization; the set will be modified to contain the
5231/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005232bool
John McCall68263142009-11-18 22:49:29 +00005233Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005234 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005235
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005236 // Try to find the member we are instantiating.
5237 NamedDecl *Instantiation = 0;
5238 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005239 MemberSpecializationInfo *MSInfo = 0;
5240
John McCall68263142009-11-18 22:49:29 +00005241 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005242 // Nowhere to look anyway.
5243 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005244 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5245 I != E; ++I) {
5246 NamedDecl *D = (*I)->getUnderlyingDecl();
5247 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005248 if (Context.hasSameType(Function->getType(), Method->getType())) {
5249 Instantiation = Method;
5250 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005251 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005252 break;
5253 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005254 }
5255 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005256 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005257 VarDecl *PrevVar;
5258 if (Previous.isSingleResult() &&
5259 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005260 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005261 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005262 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005263 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005264 }
5265 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005266 CXXRecordDecl *PrevRecord;
5267 if (Previous.isSingleResult() &&
5268 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5269 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005270 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005271 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005272 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005273 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005274
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005275 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005276 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005277 // specializations are always out-of-line, the caller will complain about
5278 // this mismatch later.
5279 return false;
5280 }
John McCall77e8b112010-04-13 20:37:33 +00005281
5282 // If this is a friend, just bail out here before we start turning
5283 // things into explicit specializations.
5284 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5285 // Preserve instantiation information.
5286 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5287 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5288 cast<CXXMethodDecl>(InstantiatedFrom),
5289 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5290 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5291 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5292 cast<CXXRecordDecl>(InstantiatedFrom),
5293 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5294 }
5295
5296 Previous.clear();
5297 Previous.addDecl(Instantiation);
5298 return false;
5299 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005300
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005301 // Make sure that this is a specialization of a member.
5302 if (!InstantiatedFrom) {
5303 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5304 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005305 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5306 return true;
5307 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005308
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005309 // C++ [temp.expl.spec]p6:
5310 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005311 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005312 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005313 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005314 // use occurs; no diagnostic is required.
5315 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005316
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005317 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005318 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5319 TSK_ExplicitSpecialization,
5320 Instantiation,
5321 MSInfo->getTemplateSpecializationKind(),
5322 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005323 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005324 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005325
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005326 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005327 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005328 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005329 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005330 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005331 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005332
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005333 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005334 // the original declaration to note that it is an explicit specialization
5335 // (if it was previously an implicit instantiation). This latter step
5336 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005337 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005338 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5339 if (InstantiationFunction->getTemplateSpecializationKind() ==
5340 TSK_ImplicitInstantiation) {
5341 InstantiationFunction->setTemplateSpecializationKind(
5342 TSK_ExplicitSpecialization);
5343 InstantiationFunction->setLocation(Member->getLocation());
5344 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005345
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005346 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5347 cast<CXXMethodDecl>(InstantiatedFrom),
5348 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005349 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005350 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005351 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5352 if (InstantiationVar->getTemplateSpecializationKind() ==
5353 TSK_ImplicitInstantiation) {
5354 InstantiationVar->setTemplateSpecializationKind(
5355 TSK_ExplicitSpecialization);
5356 InstantiationVar->setLocation(Member->getLocation());
5357 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005358
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005359 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5360 cast<VarDecl>(InstantiatedFrom),
5361 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005362 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005363 } else {
5364 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005365 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5366 if (InstantiationClass->getTemplateSpecializationKind() ==
5367 TSK_ImplicitInstantiation) {
5368 InstantiationClass->setTemplateSpecializationKind(
5369 TSK_ExplicitSpecialization);
5370 InstantiationClass->setLocation(Member->getLocation());
5371 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005372
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005373 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005374 cast<CXXRecordDecl>(InstantiatedFrom),
5375 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005376 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005377
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005378 // Save the caller the trouble of having to figure out which declaration
5379 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005380 Previous.clear();
5381 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005382 return false;
5383}
5384
Douglas Gregor558c0322009-10-14 23:41:34 +00005385/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005386///
5387/// \returns true if a serious error occurs, false otherwise.
5388static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005389 SourceLocation InstLoc,
5390 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005391 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5392 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005393
Douglas Gregor669eed82010-07-13 00:10:04 +00005394 if (CurContext->isRecord()) {
5395 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5396 << D;
5397 return true;
5398 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005399
Douglas Gregor558c0322009-10-14 23:41:34 +00005400 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005401 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005402 // template.
5403 //
5404 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005405 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005406 !CurContext->Encloses(OrigContext)) {
5407 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005408 S.Diag(InstLoc,
5409 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005410 diag::err_explicit_instantiation_out_of_scope
5411 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005412 << D << NS;
5413 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005414 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005415 S.getLangOptions().CPlusPlus0x?
5416 diag::err_explicit_instantiation_must_be_global
5417 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005418 << D;
5419 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005420 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005421 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005422
Douglas Gregor558c0322009-10-14 23:41:34 +00005423 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005424 // If the name declared in the explicit instantiation is an unqualified
5425 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005426 // its template is declared or, if that namespace is inline (7.3.1), any
5427 // namespace from its enclosing namespace set.
5428 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005429 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005430
5431 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005432 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005433
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005434 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005435 S.getLangOptions().CPlusPlus0x?
5436 diag::err_explicit_instantiation_unqualified_wrong_namespace
5437 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005438 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005439 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005440 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005441}
5442
5443/// \brief Determine whether the given scope specifier has a template-id in it.
5444static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5445 if (!SS.isSet())
5446 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005447
Douglas Gregor558c0322009-10-14 23:41:34 +00005448 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005449 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005450 // or a static data member of a class template specialization, the name of
5451 // the class template specialization in the qualified-id for the member
5452 // name shall be a simple-template-id.
5453 //
5454 // C++98 has the same restriction, just worded differently.
5455 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5456 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005457 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005458 if (isa<TemplateSpecializationType>(T))
5459 return true;
5460
5461 return false;
5462}
5463
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005464// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005465DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005466Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005467 SourceLocation ExternLoc,
5468 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005469 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005470 SourceLocation KWLoc,
5471 const CXXScopeSpec &SS,
5472 TemplateTy TemplateD,
5473 SourceLocation TemplateNameLoc,
5474 SourceLocation LAngleLoc,
5475 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005476 SourceLocation RAngleLoc,
5477 AttributeList *Attr) {
5478 // Find the class template we're specializing
5479 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005480 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005481 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5482
5483 // Check that the specialization uses the same tag kind as the
5484 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005485 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5486 assert(Kind != TTK_Enum &&
5487 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005488 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005489 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005490 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005491 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005492 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005493 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005494 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005495 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005496 diag::note_previous_use);
5497 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5498 }
5499
Douglas Gregor558c0322009-10-14 23:41:34 +00005500 // C++0x [temp.explicit]p2:
5501 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005502 // definition and an explicit instantiation declaration. An explicit
5503 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005504 TemplateSpecializationKind TSK
5505 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5506 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005507
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005508 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005509 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005510 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005511
5512 // Check that the template argument list is well-formed for this
5513 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005514 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005515 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5516 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005517 return true;
5518
Douglas Gregor910f8002010-11-07 23:05:16 +00005519 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005520 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005521
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005522 // Find the class template specialization declaration that
5523 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005524 void *InsertPos = 0;
5525 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005526 = ClassTemplate->findSpecialization(Converted.data(),
5527 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005528
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005529 TemplateSpecializationKind PrevDecl_TSK
5530 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5531
Douglas Gregord5cb8762009-10-07 00:13:32 +00005532 // C++0x [temp.explicit]p2:
5533 // [...] An explicit instantiation shall appear in an enclosing
5534 // namespace of its template. [...]
5535 //
5536 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005537 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5538 SS.isSet()))
5539 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005540
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005541 ClassTemplateSpecializationDecl *Specialization = 0;
5542
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005543 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005544 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005545 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005546 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005547 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005548 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005549 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005550
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005551 // Even though HasNoEffect == true means that this explicit instantiation
5552 // has no effect on semantics, we go on to put its syntax in the AST.
5553
5554 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5555 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005556 // Since the only prior class template specialization with these
5557 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005558 // declaration node as our own, updating the source location
5559 // for the template name to reflect our new declaration.
5560 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005561 Specialization = PrevDecl;
5562 Specialization->setLocation(TemplateNameLoc);
5563 PrevDecl = 0;
5564 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005565 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005566
Douglas Gregor52604ab2009-09-11 21:19:12 +00005567 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005568 // Create a new class template specialization declaration node for
5569 // this explicit specialization.
5570 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005571 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005572 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005573 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005574 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005575 Converted.data(),
5576 Converted.size(),
5577 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005578 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005579
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005580 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005581 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005582 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005583 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005584 }
5585
5586 // Build the fully-sugared type for this explicit instantiation as
5587 // the user wrote in the explicit instantiation itself. This means
5588 // that we'll pretty-print the type retrieved from the
5589 // specialization's declaration the way that the user actually wrote
5590 // the explicit instantiation, rather than formatting the name based
5591 // on the "canonical" representation used to store the template
5592 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005593 TypeSourceInfo *WrittenTy
5594 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5595 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005596 Context.getTypeDeclType(Specialization));
5597 Specialization->setTypeAsWritten(WrittenTy);
5598 TemplateArgsIn.release();
5599
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005600 // Set source locations for keywords.
5601 Specialization->setExternLoc(ExternLoc);
5602 Specialization->setTemplateKeywordLoc(TemplateLoc);
5603
5604 // Add the explicit instantiation into its lexical context. However,
5605 // since explicit instantiations are never found by name lookup, we
5606 // just put it into the declaration context directly.
5607 Specialization->setLexicalDeclContext(CurContext);
5608 CurContext->addDecl(Specialization);
5609
5610 // Syntax is now OK, so return if it has no other effect on semantics.
5611 if (HasNoEffect) {
5612 // Set the template specialization kind.
5613 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005614 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005615 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005616
5617 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005618 // A definition of a class template or class member template
5619 // shall be in scope at the point of the explicit instantiation of
5620 // the class template or class member template.
5621 //
5622 // This check comes when we actually try to perform the
5623 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005624 ClassTemplateSpecializationDecl *Def
5625 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005626 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005627 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005628 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005629 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005630 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005631 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5632 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005633
Douglas Gregor0d035142009-10-27 18:42:08 +00005634 // Instantiate the members of this class template specialization.
5635 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005636 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005637 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005638 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5639
5640 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5641 // TSK_ExplicitInstantiationDefinition
5642 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5643 TSK == TSK_ExplicitInstantiationDefinition)
5644 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005645
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005646 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005647 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005648
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005649 // Set the template specialization kind.
5650 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005651 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005652}
5653
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005654// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005655DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005656Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005657 SourceLocation ExternLoc,
5658 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005659 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005660 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005661 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005662 IdentifierInfo *Name,
5663 SourceLocation NameLoc,
5664 AttributeList *Attr) {
5665
Douglas Gregor402abb52009-05-28 23:31:59 +00005666 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005667 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005668 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005669 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5670 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005671 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005672 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005673 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5674
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005675 if (!TagD)
5676 return true;
5677
John McCalld226f652010-08-21 09:40:31 +00005678 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005679 if (Tag->isEnum()) {
5680 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5681 << Context.getTypeDeclType(Tag);
5682 return true;
5683 }
5684
Douglas Gregord0c87372009-05-27 17:30:49 +00005685 if (Tag->isInvalidDecl())
5686 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005687
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005688 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5689 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5690 if (!Pattern) {
5691 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5692 << Context.getTypeDeclType(Record);
5693 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5694 return true;
5695 }
5696
Douglas Gregor558c0322009-10-14 23:41:34 +00005697 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005698 // If the explicit instantiation is for a class or member class, the
5699 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005700 // simple-template-id.
5701 //
5702 // C++98 has the same restriction, just worded differently.
5703 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005704 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005705 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005706
Douglas Gregor558c0322009-10-14 23:41:34 +00005707 // C++0x [temp.explicit]p2:
5708 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005709 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005710 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005711 TemplateSpecializationKind TSK
5712 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5713 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005714
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005715 // C++0x [temp.explicit]p2:
5716 // [...] An explicit instantiation shall appear in an enclosing
5717 // namespace of its template. [...]
5718 //
5719 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005720 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005721
Douglas Gregor454885e2009-10-15 15:54:05 +00005722 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005723 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005724 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005725 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005726 PrevDecl = Record;
5727 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005728 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005729 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005730 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005731 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005732 PrevDecl,
5733 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005734 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005735 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005736 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005737 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005738 return TagD;
5739 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005740
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005741 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005742 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005743 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005744 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005745 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005746 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005747 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005748 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005749 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005750 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5751 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005752 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5753 << Pattern;
5754 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005755 } else {
5756 if (InstantiateClass(NameLoc, Record, Def,
5757 getTemplateInstantiationArgs(Record),
5758 TSK))
5759 return true;
5760
Douglas Gregor952b0172010-02-11 01:04:33 +00005761 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005762 if (!RecordDef)
5763 return true;
5764 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005765 }
5766
Douglas Gregor0d035142009-10-27 18:42:08 +00005767 // Instantiate all of the members of the class.
5768 InstantiateClassMembers(NameLoc, RecordDef,
5769 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005770
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005771 if (TSK == TSK_ExplicitInstantiationDefinition)
5772 MarkVTableUsed(NameLoc, RecordDef, true);
5773
Mike Stump390b4cc2009-05-16 07:39:55 +00005774 // FIXME: We don't have any representation for explicit instantiations of
5775 // member classes. Such a representation is not needed for compilation, but it
5776 // should be available for clients that want to see all of the declarations in
5777 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005778 return TagD;
5779}
5780
John McCallf312b1e2010-08-26 23:41:50 +00005781DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5782 SourceLocation ExternLoc,
5783 SourceLocation TemplateLoc,
5784 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005785 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005786 // TODO: check if/when DNInfo should replace Name.
5787 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5788 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005789 if (!Name) {
5790 if (!D.isInvalidType())
5791 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5792 diag::err_explicit_instantiation_requires_name)
5793 << D.getDeclSpec().getSourceRange()
5794 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005795
Douglas Gregord5a423b2009-09-25 18:43:00 +00005796 return true;
5797 }
5798
5799 // The scope passed in may not be a decl scope. Zip up the scope tree until
5800 // we find one that is.
5801 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5802 (S->getFlags() & Scope::TemplateParamScope) != 0)
5803 S = S->getParent();
5804
5805 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005806 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5807 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005808 if (R.isNull())
5809 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005810
Douglas Gregord5a423b2009-09-25 18:43:00 +00005811 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5812 // Cannot explicitly instantiate a typedef.
5813 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5814 << Name;
5815 return true;
5816 }
5817
Douglas Gregor663b5a02009-10-14 20:14:33 +00005818 // C++0x [temp.explicit]p1:
5819 // [...] An explicit instantiation of a function template shall not use the
5820 // inline or constexpr specifiers.
5821 // Presumably, this also applies to member functions of class templates as
5822 // well.
5823 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005824 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005825 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005826 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005827
Douglas Gregor663b5a02009-10-14 20:14:33 +00005828 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005829
Douglas Gregor558c0322009-10-14 23:41:34 +00005830 // C++0x [temp.explicit]p2:
5831 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005832 // definition and an explicit instantiation declaration. An explicit
5833 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005834 TemplateSpecializationKind TSK
5835 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5836 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005837
Abramo Bagnara25777432010-08-11 22:01:17 +00005838 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005839 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005840
5841 if (!R->isFunctionType()) {
5842 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005843 // A [...] static data member of a class template can be explicitly
5844 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005845 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005846 if (Previous.isAmbiguous())
5847 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005848
John McCall1bcee0a2009-12-02 08:25:40 +00005849 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005850 if (!Prev || !Prev->isStaticDataMember()) {
5851 // We expect to see a data data member here.
5852 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5853 << Name;
5854 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5855 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005856 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005857 return true;
5858 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005859
Douglas Gregord5a423b2009-09-25 18:43:00 +00005860 if (!Prev->getInstantiatedFromStaticDataMember()) {
5861 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005862 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005863 diag::err_explicit_instantiation_data_member_not_instantiated)
5864 << Prev;
5865 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5866 // FIXME: Can we provide a note showing where this was declared?
5867 return true;
5868 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005869
Douglas Gregor558c0322009-10-14 23:41:34 +00005870 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005871 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005872 // or a static data member of a class template specialization, the name of
5873 // the class template specialization in the qualified-id for the member
5874 // name shall be a simple-template-id.
5875 //
5876 // C++98 has the same restriction, just worded differently.
5877 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005878 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005879 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005880 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005881
Douglas Gregor558c0322009-10-14 23:41:34 +00005882 // Check the scope of this explicit instantiation.
5883 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005884
Douglas Gregor454885e2009-10-15 15:54:05 +00005885 // Verify that it is okay to explicitly instantiate here.
5886 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5887 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005888 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005889 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005890 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005891 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005892 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005893 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005894 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005895 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005896
Douglas Gregord5a423b2009-09-25 18:43:00 +00005897 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005898 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005899 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005900 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005901
Douglas Gregord5a423b2009-09-25 18:43:00 +00005902 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005903 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005904 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005905
5906 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005907 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005908 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005909 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005910 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5911 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005912 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5913 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005914 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5915 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005916 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005917 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005918 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005919 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005920 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005921
Douglas Gregord5a423b2009-09-25 18:43:00 +00005922 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005923 // A [...] function [...] can be explicitly instantiated from its template.
5924 // A member function [...] of a class template can be explicitly
5925 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005926 // template.
John McCallc373d482010-01-27 01:50:18 +00005927 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005928 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5929 P != PEnd; ++P) {
5930 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005931 if (!HasExplicitTemplateArgs) {
5932 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5933 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5934 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005935
John McCallc373d482010-01-27 01:50:18 +00005936 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005937 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5938 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005939 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005940 }
5941 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005942
Douglas Gregord5a423b2009-09-25 18:43:00 +00005943 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5944 if (!FunTmpl)
5945 continue;
5946
John McCall5769d612010-02-08 23:07:23 +00005947 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005948 FunctionDecl *Specialization = 0;
5949 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005950 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005951 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005952 R, Specialization, Info)) {
5953 // FIXME: Keep track of almost-matches?
5954 (void)TDK;
5955 continue;
5956 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005957
John McCallc373d482010-01-27 01:50:18 +00005958 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005959 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005960
Douglas Gregord5a423b2009-09-25 18:43:00 +00005961 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005962 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005963 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005964 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005965 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5966 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5967 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005968
John McCallc373d482010-01-27 01:50:18 +00005969 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005970 return true;
John McCallc373d482010-01-27 01:50:18 +00005971
5972 // Ignore access control bits, we don't need them for redeclaration checking.
5973 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005974
Douglas Gregor0a897e32009-10-15 17:21:20 +00005975 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005976 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005977 diag::err_explicit_instantiation_member_function_not_instantiated)
5978 << Specialization
5979 << (Specialization->getTemplateSpecializationKind() ==
5980 TSK_ExplicitSpecialization);
5981 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5982 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005983 }
5984
Douglas Gregor0a897e32009-10-15 17:21:20 +00005985 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005986 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5987 PrevDecl = Specialization;
5988
Douglas Gregor0a897e32009-10-15 17:21:20 +00005989 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005990 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005991 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005992 PrevDecl,
5993 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005994 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005995 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005996 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005997
Douglas Gregor0a897e32009-10-15 17:21:20 +00005998 // FIXME: We may still want to build some representation of this
5999 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006000 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006001 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006002 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006003
6004 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006005
Douglas Gregor0a897e32009-10-15 17:21:20 +00006006 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006007 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006008
Douglas Gregor558c0322009-10-14 23:41:34 +00006009 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006010 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006011 // or a static data member of a class template specialization, the name of
6012 // the class template specialization in the qualified-id for the member
6013 // name shall be a simple-template-id.
6014 //
6015 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006016 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006017 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006018 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006019 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006020 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006021 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006022 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006023
Douglas Gregor558c0322009-10-14 23:41:34 +00006024 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006025 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006026 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006027 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006028 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006029
Douglas Gregord5a423b2009-09-25 18:43:00 +00006030 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006031 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006032}
6033
John McCallf312b1e2010-08-26 23:41:50 +00006034TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006035Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6036 const CXXScopeSpec &SS, IdentifierInfo *Name,
6037 SourceLocation TagLoc, SourceLocation NameLoc) {
6038 // This has to hold, because SS is expected to be defined.
6039 assert(Name && "Expected a name in a dependent tag");
6040
6041 NestedNameSpecifier *NNS
6042 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6043 if (!NNS)
6044 return true;
6045
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006046 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006047
Douglas Gregor48c89f42010-04-24 16:38:41 +00006048 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6049 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006050 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006051 return true;
6052 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006053
Douglas Gregor059101f2011-03-02 00:47:37 +00006054 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006055 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006056 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6057
6058 // Create type-source location information for this type.
6059 TypeLocBuilder TLB;
6060 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6061 TL.setKeywordLoc(TagLoc);
6062 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6063 TL.setNameLoc(NameLoc);
6064 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006065}
6066
John McCallf312b1e2010-08-26 23:41:50 +00006067TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006068Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6069 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006070 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006071 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006072 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006073
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006074 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6075 !getLangOptions().CPlusPlus0x)
6076 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006077 << FixItHint::CreateRemoval(TypenameLoc);
6078
Douglas Gregor2494dd02011-03-01 01:34:45 +00006079 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006080 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6081 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006082 if (T.isNull())
6083 return true;
John McCall63b43852010-04-29 23:50:39 +00006084
6085 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6086 if (isa<DependentNameType>(T)) {
6087 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006088 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006089 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006090 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006091 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006092 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006093 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006094 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006095 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006096 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006097
John McCallb3d87482010-08-24 05:47:05 +00006098 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006099}
6100
John McCallf312b1e2010-08-26 23:41:50 +00006101TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006102Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6103 const CXXScopeSpec &SS,
6104 SourceLocation TemplateLoc,
6105 TemplateTy TemplateIn,
6106 SourceLocation TemplateNameLoc,
6107 SourceLocation LAngleLoc,
6108 ASTTemplateArgsPtr TemplateArgsIn,
6109 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006110 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6111 !getLangOptions().CPlusPlus0x)
6112 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006113 << FixItHint::CreateRemoval(TypenameLoc);
6114
6115 // Translate the parser's template argument list in our AST format.
6116 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6117 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6118
6119 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006120 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6121 // Construct a dependent template specialization type.
6122 assert(DTN && "dependent template has non-dependent name?");
6123 assert(DTN->getQualifier()
6124 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6125 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6126 DTN->getQualifier(),
6127 DTN->getIdentifier(),
6128 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006129
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006130 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006131 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006132 DependentTemplateSpecializationTypeLoc SpecTL
6133 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006134 SpecTL.setLAngleLoc(LAngleLoc);
6135 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006136 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006137 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006138 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006139 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6140 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006141 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006142 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006143
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006144 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6145 if (T.isNull())
6146 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006147
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006148 // Provide source-location information for the template specialization
6149 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006150 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006151 TemplateSpecializationTypeLoc SpecTL
6152 = Builder.push<TemplateSpecializationTypeLoc>(T);
6153
6154 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006155 SpecTL.setLAngleLoc(LAngleLoc);
6156 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006157 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006158 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6159 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6160
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006161 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6162 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6163 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006164 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6165
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006166 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6167 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006168}
6169
Douglas Gregora02411e2011-02-27 22:46:49 +00006170
Douglas Gregord57959a2009-03-27 23:10:48 +00006171/// \brief Build the type that describes a C++ typename specifier,
6172/// e.g., "typename T::type".
6173QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006174Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6175 SourceLocation KeywordLoc,
6176 NestedNameSpecifierLoc QualifierLoc,
6177 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006178 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006179 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006180 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006181
John McCall77bb1aa2010-05-01 00:40:08 +00006182 DeclContext *Ctx = computeDeclContext(SS);
6183 if (!Ctx) {
6184 // If the nested-name-specifier is dependent and couldn't be
6185 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006186 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6187 return Context.getDependentNameType(Keyword,
6188 QualifierLoc.getNestedNameSpecifier(),
6189 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006190 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006191
John McCall77bb1aa2010-05-01 00:40:08 +00006192 // If the nested-name-specifier refers to the current instantiation,
6193 // the "typename" keyword itself is superfluous. In C++03, the
6194 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6195 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006196 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006197
John McCall77bb1aa2010-05-01 00:40:08 +00006198 if (RequireCompleteDeclContext(SS, Ctx))
6199 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006200
6201 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006202 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006203 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006204 unsigned DiagID = 0;
6205 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006206 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006207 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006208 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006209 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006210
6211 case LookupResult::FoundUnresolvedValue: {
6212 // We found a using declaration that is a value. Most likely, the using
6213 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006214 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006215 IILoc);
6216 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6217 << Name << Ctx << FullRange;
6218 if (UnresolvedUsingValueDecl *Using
6219 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006220 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006221 Diag(Loc, diag::note_using_value_decl_missing_typename)
6222 << FixItHint::CreateInsertion(Loc, "typename ");
6223 }
6224 }
6225 // Fall through to create a dependent typename type, from which we can recover
6226 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006227
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006228 case LookupResult::NotFoundInCurrentInstantiation:
6229 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006230 return Context.getDependentNameType(Keyword,
6231 QualifierLoc.getNestedNameSpecifier(),
6232 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006233
6234 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006235 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006236 // We found a type. Build an ElaboratedType, since the
6237 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006238 return Context.getElaboratedType(ETK_Typename,
6239 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006240 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006241 }
6242
6243 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006244 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006245 break;
6246
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006247
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006248 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006249 return QualType();
6250
Douglas Gregord57959a2009-03-27 23:10:48 +00006251 case LookupResult::FoundOverloaded:
6252 DiagID = diag::err_typename_nested_not_type;
6253 Referenced = *Result.begin();
6254 break;
6255
John McCall6e247262009-10-10 05:48:19 +00006256 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006257 return QualType();
6258 }
6259
6260 // If we get here, it's because name lookup did not find a
6261 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006262 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006263 IILoc);
6264 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006265 if (Referenced)
6266 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6267 << Name;
6268 return QualType();
6269}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006270
6271namespace {
6272 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006273 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006274 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006275 SourceLocation Loc;
6276 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006277
Douglas Gregor4a959d82009-08-06 16:20:37 +00006278 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006279 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006280
Mike Stump1eb44332009-09-09 15:08:12 +00006281 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006282 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006283 DeclarationName Entity)
6284 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006285 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006286
6287 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006288 /// transformed.
6289 ///
6290 /// For the purposes of type reconstruction, a type has already been
6291 /// transformed if it is NULL or if it is not dependent.
6292 bool AlreadyTransformed(QualType T) {
6293 return T.isNull() || !T->isDependentType();
6294 }
Mike Stump1eb44332009-09-09 15:08:12 +00006295
6296 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006297 /// rebuilt.
6298 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006299
Douglas Gregor4a959d82009-08-06 16:20:37 +00006300 /// \brief Returns the name of the entity whose type is being rebuilt.
6301 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006302
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006303 /// \brief Sets the "base" location and entity when that
6304 /// information is known based on another transformation.
6305 void setBase(SourceLocation Loc, DeclarationName Entity) {
6306 this->Loc = Loc;
6307 this->Entity = Entity;
6308 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006309 };
6310}
6311
Douglas Gregor4a959d82009-08-06 16:20:37 +00006312/// \brief Rebuilds a type within the context of the current instantiation.
6313///
Mike Stump1eb44332009-09-09 15:08:12 +00006314/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006315/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006316/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006317/// partial specialization thereof). This routine will rebuild that type now
6318/// that we have entered the declarator's scope, which may produce different
6319/// canonical types, e.g.,
6320///
6321/// \code
6322/// template<typename T>
6323/// struct X {
6324/// typedef T* pointer;
6325/// pointer data();
6326/// };
6327///
6328/// template<typename T>
6329/// typename X<T>::pointer X<T>::data() { ... }
6330/// \endcode
6331///
Douglas Gregor4714c122010-03-31 17:34:00 +00006332/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006333/// since we do not know that we can look into X<T> when we parsed the type.
6334/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006335/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006336/// as the canonical type of T*, allowing the return types of the out-of-line
6337/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006338TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6339 SourceLocation Loc,
6340 DeclarationName Name) {
6341 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006342 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006343
Douglas Gregor4a959d82009-08-06 16:20:37 +00006344 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6345 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006346}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006347
John McCall60d7b3a2010-08-24 06:29:42 +00006348ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006349 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6350 DeclarationName());
6351 return Rebuilder.TransformExpr(E);
6352}
6353
John McCall63b43852010-04-29 23:50:39 +00006354bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006355 if (SS.isInvalid())
6356 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006357
Douglas Gregor7e384942011-02-25 16:07:42 +00006358 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006359 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6360 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006361 NestedNameSpecifierLoc Rebuilt
6362 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6363 if (!Rebuilt)
6364 return true;
John McCall63b43852010-04-29 23:50:39 +00006365
Douglas Gregor7e384942011-02-25 16:07:42 +00006366 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006367 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006368}
6369
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006370/// \brief Produces a formatted string that describes the binding of
6371/// template parameters to template arguments.
6372std::string
6373Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6374 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006375 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006376}
6377
6378std::string
6379Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6380 const TemplateArgument *Args,
6381 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006382 llvm::SmallString<128> Str;
6383 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006384
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006385 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006386 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006387
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006388 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006389 if (I >= NumArgs)
6390 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006391
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006392 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006393 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006394 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006395 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006396
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006397 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006398 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006399 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006400 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006401 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006402
Douglas Gregor87dd6972010-12-20 16:52:59 +00006403 Out << " = ";
6404 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006405 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006406
6407 Out << ']';
6408 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006409}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006410
6411void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6412 if (!FD)
6413 return;
6414 FD->setLateTemplateParsed(Flag);
6415}
6416
6417bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6418 DeclContext *DC = CurContext;
6419
6420 while (DC) {
6421 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6422 const FunctionDecl *FD = RD->isLocalClass();
6423 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6424 } else if (DC->isTranslationUnit() || DC->isNamespace())
6425 return false;
6426
6427 DC = DC->getParent();
6428 }
6429 return false;
6430}