blob: 3123f5fc675289aa0643fb4da06b7895bba80b0b [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
122 return true;
123}
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
Douglas Gregorefed5c82010-06-16 15:23:05 +0000506/// ParamName 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) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003097 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003098 AddressTaken = true;
3099 AddrOpLoc = UnOp->getOperatorLoc();
3100 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003101 } else
3102 DRE = dyn_cast<DeclRefExpr>(Arg);
3103
Douglas Gregorb7a09262010-04-01 18:32:35 +00003104 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003105 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3106 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003107 S.Diag(Param->getLocation(), diag::note_template_param_here);
3108 return true;
3109 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003110
3111 // Stop checking the precise nature of the argument if it is value dependent,
3112 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003113 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003114 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003115 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003116 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003117
Douglas Gregorb7a09262010-04-01 18:32:35 +00003118 if (!isa<ValueDecl>(DRE->getDecl())) {
3119 S.Diag(Arg->getSourceRange().getBegin(),
3120 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003121 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003122 S.Diag(Param->getLocation(), diag::note_template_param_here);
3123 return true;
3124 }
3125
3126 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003127
3128 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003129 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3130 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003131 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003132 S.Diag(Param->getLocation(), diag::note_template_param_here);
3133 return true;
3134 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003135
3136 // Cannot refer to non-static member functions
3137 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003138 if (!Method->isStatic()) {
3139 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003140 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003141 S.Diag(Param->getLocation(), diag::note_template_param_here);
3142 return true;
3143 }
Mike Stump1eb44332009-09-09 15:08:12 +00003144
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003145 // Functions must have external linkage.
3146 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003147 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003148 S.Diag(Arg->getSourceRange().getBegin(),
3149 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003150 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003151 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003152 << true;
3153 return true;
3154 }
3155
3156 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003157 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003158
Douglas Gregorb7a09262010-04-01 18:32:35 +00003159 // If the template parameter has pointer type, the function decays.
3160 if (ParamType->isPointerType() && !AddressTaken)
3161 ArgType = S.Context.getPointerType(Func->getType());
3162 else if (AddressTaken && ParamType->isReferenceType()) {
3163 // If we originally had an address-of operator, but the
3164 // parameter has reference type, complain and (if things look
3165 // like they will work) drop the address-of operator.
3166 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3167 ParamType.getNonReferenceType())) {
3168 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3169 << ParamType;
3170 S.Diag(Param->getLocation(), diag::note_template_param_here);
3171 return true;
3172 }
3173
3174 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3175 << ParamType
3176 << FixItHint::CreateRemoval(AddrOpLoc);
3177 S.Diag(Param->getLocation(), diag::note_template_param_here);
3178
3179 ArgType = Func->getType();
3180 }
3181 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003182 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003183 S.Diag(Arg->getSourceRange().getBegin(),
3184 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003185 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003186 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003187 << true;
3188 return true;
3189 }
3190
Douglas Gregorb7a09262010-04-01 18:32:35 +00003191 // A value of reference type is not an object.
3192 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003193 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003194 diag::err_template_arg_reference_var)
3195 << Var->getType() << Arg->getSourceRange();
3196 S.Diag(Param->getLocation(), diag::note_template_param_here);
3197 return true;
3198 }
3199
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003200 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003201 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003202
3203 // If the template parameter has pointer type, we must have taken
3204 // the address of this object.
3205 if (ParamType->isReferenceType()) {
3206 if (AddressTaken) {
3207 // If we originally had an address-of operator, but the
3208 // parameter has reference type, complain and (if things look
3209 // like they will work) drop the address-of operator.
3210 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3211 ParamType.getNonReferenceType())) {
3212 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3213 << ParamType;
3214 S.Diag(Param->getLocation(), diag::note_template_param_here);
3215 return true;
3216 }
3217
3218 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3219 << ParamType
3220 << FixItHint::CreateRemoval(AddrOpLoc);
3221 S.Diag(Param->getLocation(), diag::note_template_param_here);
3222
3223 ArgType = Var->getType();
3224 }
3225 } else if (!AddressTaken && ParamType->isPointerType()) {
3226 if (Var->getType()->isArrayType()) {
3227 // Array-to-pointer decay.
3228 ArgType = S.Context.getArrayDecayedType(Var->getType());
3229 } else {
3230 // If the template parameter has pointer type but the address of
3231 // this object was not taken, complain and (possibly) recover by
3232 // taking the address of the entity.
3233 ArgType = S.Context.getPointerType(Var->getType());
3234 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3235 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3236 << ParamType;
3237 S.Diag(Param->getLocation(), diag::note_template_param_here);
3238 return true;
3239 }
3240
3241 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3242 << ParamType
3243 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3244
3245 S.Diag(Param->getLocation(), diag::note_template_param_here);
3246 }
3247 }
3248 } else {
3249 // We found something else, but we don't know specifically what it is.
3250 S.Diag(Arg->getSourceRange().getBegin(),
3251 diag::err_template_arg_not_object_or_func)
3252 << Arg->getSourceRange();
3253 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3254 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003255 }
Mike Stump1eb44332009-09-09 15:08:12 +00003256
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003257 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003258 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003259 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003260 // For pointer-to-object types, qualification conversions are
3261 // permitted.
3262 } else {
3263 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3264 if (!ParamRef->getPointeeType()->isFunctionType()) {
3265 // C++ [temp.arg.nontype]p5b3:
3266 // For a non-type template-parameter of type reference to
3267 // object, no conversions apply. The type referred to by the
3268 // reference may be more cv-qualified than the (otherwise
3269 // identical) type of the template- argument. The
3270 // template-parameter is bound directly to the
3271 // template-argument, which shall be an lvalue.
3272
3273 // FIXME: Other qualifiers?
3274 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3275 unsigned ArgQuals = ArgType.getCVRQualifiers();
3276
3277 if ((ParamQuals | ArgQuals) != ParamQuals) {
3278 S.Diag(Arg->getSourceRange().getBegin(),
3279 diag::err_template_arg_ref_bind_ignores_quals)
3280 << ParamType << Arg->getType()
3281 << Arg->getSourceRange();
3282 S.Diag(Param->getLocation(), diag::note_template_param_here);
3283 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003284 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003285 }
3286 }
3287
3288 // At this point, the template argument refers to an object or
3289 // function with external linkage. We now need to check whether the
3290 // argument and parameter types are compatible.
3291 if (!S.Context.hasSameUnqualifiedType(ArgType,
3292 ParamType.getNonReferenceType())) {
3293 // We can't perform this conversion or binding.
3294 if (ParamType->isReferenceType())
3295 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3296 << ParamType << Arg->getType() << Arg->getSourceRange();
3297 else
3298 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3299 << Arg->getType() << ParamType << Arg->getSourceRange();
3300 S.Diag(Param->getLocation(), diag::note_template_param_here);
3301 return true;
3302 }
3303 }
3304
3305 // Create the template argument.
3306 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003307 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003308 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003309}
3310
3311/// \brief Checks whether the given template argument is a pointer to
3312/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003313bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003314 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003315 bool Invalid = false;
3316
3317 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003318 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003319 Arg = Cast->getSubExpr();
3320
3321 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003322 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003323 // A template-argument for a non-type, non-template
3324 // template-parameter shall be one of: [...]
3325 //
3326 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003327 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003328
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003329 // In C++98/03 mode, give an extension warning on any extra parentheses.
3330 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3331 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003332 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003333 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003334 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003335 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003336 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003337 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003338 }
3339
3340 Arg = Parens->getSubExpr();
3341 }
3342
Douglas Gregorcaddba02009-11-12 18:38:13 +00003343 // A pointer-to-member constant written &Class::member.
3344 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003345 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003346 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3347 if (DRE && !DRE->getQualifier())
3348 DRE = 0;
3349 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003350 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003351 // A constant of pointer-to-member type.
3352 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3353 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3354 if (VD->getType()->isMemberPointerType()) {
3355 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003356 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003357 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3358 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003359 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003360 else
3361 Converted = TemplateArgument(VD->getCanonicalDecl());
3362 return Invalid;
3363 }
3364 }
3365 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003366
Douglas Gregorcaddba02009-11-12 18:38:13 +00003367 DRE = 0;
3368 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003369
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003370 if (!DRE)
3371 return Diag(Arg->getSourceRange().getBegin(),
3372 diag::err_template_arg_not_pointer_to_member_form)
3373 << Arg->getSourceRange();
3374
3375 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3376 assert((isa<FieldDecl>(DRE->getDecl()) ||
3377 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3378 "Only non-static member pointers can make it here");
3379
3380 // Okay: this is the address of a non-static member, and therefore
3381 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003382 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003383 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003384 else
3385 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003386 return Invalid;
3387 }
3388
3389 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003390 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003391 diag::err_template_arg_not_pointer_to_member_form)
3392 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003393 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003394 diag::note_template_arg_refers_here);
3395 return true;
3396}
3397
Douglas Gregorc15cb382009-02-09 23:23:08 +00003398/// \brief Check a template argument against its corresponding
3399/// non-type template parameter.
3400///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003401/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003402/// If an error occurred, it returns ExprError(); otherwise, it
3403/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003404/// InstantiatedParamType is the type of the non-type template
3405/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003406ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3407 QualType InstantiatedParamType, Expr *Arg,
3408 TemplateArgument &Converted,
3409 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003410 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3411
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003412 // If either the parameter has a dependent type or the argument is
3413 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003414 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3415 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003416 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003417 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003418 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003419
3420 // C++ [temp.arg.nontype]p5:
3421 // The following conversions are performed on each expression used
3422 // as a non-type template-argument. If a non-type
3423 // template-argument cannot be converted to the type of the
3424 // corresponding template-parameter then the program is
3425 // ill-formed.
3426 //
3427 // -- for a non-type template-parameter of integral or
3428 // enumeration type, integral promotions (4.5) and integral
3429 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003430 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003431 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003432 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003433 // C++ [temp.arg.nontype]p1:
3434 // A template-argument for a non-type, non-template
3435 // template-parameter shall be one of:
3436 //
3437 // -- an integral constant-expression of integral or enumeration
3438 // type; or
3439 // -- the name of a non-type template-parameter; or
3440 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003441 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003442 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003443 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003444 diag::err_template_arg_not_integral_or_enumeral)
3445 << ArgType << Arg->getSourceRange();
3446 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003447 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003448 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003449 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003450 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3451 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003452 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003453 }
3454
Douglas Gregor02024a92010-03-28 02:42:43 +00003455 // From here on out, all we care about are the unqualified forms
3456 // of the parameter and argument types.
3457 ParamType = ParamType.getUnqualifiedType();
3458 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003459
3460 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003461 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003462 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003463 } else if (CTAK == CTAK_Deduced) {
3464 // C++ [temp.deduct.type]p17:
3465 // If, in the declaration of a function template with a non-type
3466 // template-parameter, the non-type template- parameter is used
3467 // in an expression in the function parameter-list and, if the
3468 // corresponding template-argument is deduced, the
3469 // template-argument type shall match the type of the
3470 // template-parameter exactly, except that a template-argument
3471 // deduced from an array bound may be of any integral type.
3472 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3473 << ArgType << ParamType;
3474 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003475 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003476 } else if (ParamType->isBooleanType()) {
3477 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003478 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003479 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3480 !ParamType->isEnumeralType()) {
3481 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003482 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003483 } else {
3484 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003485 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003486 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003487 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003488 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003489 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003490 }
3491
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003492 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003493 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003494 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003495
3496 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003497 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003498
3499 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003500 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003501 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003502 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003503 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003504 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003505
3506 // Complain if an unsigned parameter received a negative value.
3507 if (IntegerType->isUnsignedIntegerType()
3508 && (OldValue.isSigned() && OldValue.isNegative())) {
3509 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3510 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3511 << Arg->getSourceRange();
3512 Diag(Param->getLocation(), diag::note_template_param_here);
3513 }
3514
3515 // Complain if we overflowed the template parameter's type.
3516 unsigned RequiredBits;
3517 if (IntegerType->isUnsignedIntegerType())
3518 RequiredBits = OldValue.getActiveBits();
3519 else if (OldValue.isUnsigned())
3520 RequiredBits = OldValue.getActiveBits() + 1;
3521 else
3522 RequiredBits = OldValue.getMinSignedBits();
3523 if (RequiredBits > AllowedBits) {
3524 Diag(Arg->getSourceRange().getBegin(),
3525 diag::warn_template_arg_too_large)
3526 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3527 << Arg->getSourceRange();
3528 Diag(Param->getLocation(), diag::note_template_param_here);
3529 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003530 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003531
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003532 // Add the value of this argument to the list of converted
3533 // arguments. We use the bitwidth and signedness of the template
3534 // parameter.
3535 if (Arg->isValueDependent()) {
3536 // The argument is value-dependent. Create a new
3537 // TemplateArgument with the converted expression.
3538 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003539 return Owned(Arg);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003540 }
3541
John McCall833ca992009-10-29 08:12:44 +00003542 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003543 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003544 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003545 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003546 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003547
John McCall6bb80172010-03-30 21:47:33 +00003548 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3549
Douglas Gregorb7a09262010-04-01 18:32:35 +00003550 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3551 // from a template argument of type std::nullptr_t to a non-type
3552 // template parameter of type pointer to object, pointer to
3553 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003554 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003555 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3556 Converted = TemplateArgument((NamedDecl *)0);
John Wiegley429bb272011-04-08 18:41:53 +00003557 return Owned(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003558 }
3559
Douglas Gregorb86b0572009-02-11 01:18:59 +00003560 // Handle pointer-to-function, reference-to-function, and
3561 // pointer-to-member-function all in (roughly) the same way.
3562 if (// -- For a non-type template-parameter of type pointer to
3563 // function, only the function-to-pointer conversion (4.3) is
3564 // applied. If the template-argument represents a set of
3565 // overloaded functions (or a pointer to such), the matching
3566 // function is selected from the set (13.4).
3567 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003568 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003569 // -- For a non-type template-parameter of type reference to
3570 // function, no conversions apply. If the template-argument
3571 // represents a set of overloaded functions, the matching
3572 // function is selected from the set (13.4).
3573 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003574 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003575 // -- For a non-type template-parameter of type pointer to
3576 // member function, no conversions apply. If the
3577 // template-argument represents a set of overloaded member
3578 // functions, the matching member function is selected from
3579 // the set (13.4).
3580 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003581 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003582 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003583
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003584 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003585 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003586 true,
3587 FoundResult)) {
3588 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003589 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003590
3591 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3592 ArgType = Arg->getType();
3593 } else
John Wiegley429bb272011-04-08 18:41:53 +00003594 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003595 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003596
John Wiegley429bb272011-04-08 18:41:53 +00003597 if (!ParamType->isMemberPointerType()) {
3598 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3599 ParamType,
3600 Arg, Converted))
3601 return ExprError();
3602 return Owned(Arg);
3603 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003604
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003605 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3606 false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003607 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003608 } else if (!Context.hasSameUnqualifiedType(ArgType,
3609 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003610 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003611 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003612 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003613 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003614 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003615 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003616 }
Mike Stump1eb44332009-09-09 15:08:12 +00003617
John Wiegley429bb272011-04-08 18:41:53 +00003618 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3619 return ExprError();
3620 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003621 }
3622
Chris Lattnerfe90de72009-02-20 21:37:53 +00003623 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003624 // -- for a non-type template-parameter of type pointer to
3625 // object, qualification conversions (4.4) and the
3626 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003627 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003628 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003629 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003630
John Wiegley429bb272011-04-08 18:41:53 +00003631 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3632 ParamType,
3633 Arg, Converted))
3634 return ExprError();
3635 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003636 }
Mike Stump1eb44332009-09-09 15:08:12 +00003637
Ted Kremenek6217b802009-07-29 21:53:49 +00003638 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003639 // -- For a non-type template-parameter of type reference to
3640 // object, no conversions apply. The type referred to by the
3641 // reference may be more cv-qualified than the (otherwise
3642 // identical) type of the template-argument. The
3643 // template-parameter is bound directly to the
3644 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003645 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003646 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003647
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003648 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003649 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3650 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003651 true,
3652 FoundResult)) {
3653 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003654 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003655
3656 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3657 ArgType = Arg->getType();
3658 } else
John Wiegley429bb272011-04-08 18:41:53 +00003659 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003660 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003661
John Wiegley429bb272011-04-08 18:41:53 +00003662 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3663 ParamType,
3664 Arg, Converted))
3665 return ExprError();
3666 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003667 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003668
3669 // -- For a non-type template-parameter of type pointer to data
3670 // member, qualification conversions (4.4) are applied.
3671 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3672
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003673 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003674 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003675 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003676 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003677 } else {
3678 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003679 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003680 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003681 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003682 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003683 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003684 }
3685
John Wiegley429bb272011-04-08 18:41:53 +00003686 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3687 return ExprError();
3688 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003689}
3690
3691/// \brief Check a template argument against its corresponding
3692/// template template parameter.
3693///
3694/// This routine implements the semantics of C++ [temp.arg.template].
3695/// It returns true if an error occurred, and false otherwise.
3696bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003697 const TemplateArgumentLoc &Arg) {
3698 TemplateName Name = Arg.getArgument().getAsTemplate();
3699 TemplateDecl *Template = Name.getAsTemplateDecl();
3700 if (!Template) {
3701 // Any dependent template name is fine.
3702 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3703 return false;
3704 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003705
3706 // C++ [temp.arg.template]p1:
3707 // A template-argument for a template template-parameter shall be
3708 // the name of a class template, expressed as id-expression. Only
3709 // primary class templates are considered when matching the
3710 // template template argument with the corresponding parameter;
3711 // partial specializations are not considered even if their
3712 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003713 //
3714 // Note that we also allow template template parameters here, which
3715 // will happen when we are dealing with, e.g., class template
3716 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003717 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003718 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003719 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003720 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003721 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003722 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003723 << Template;
3724 }
3725
3726 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3727 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003728 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003729 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003730 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003731}
3732
Douglas Gregor02024a92010-03-28 02:42:43 +00003733/// \brief Given a non-type template argument that refers to a
3734/// declaration and the type of its corresponding non-type template
3735/// parameter, produce an expression that properly refers to that
3736/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003737ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003738Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3739 QualType ParamType,
3740 SourceLocation Loc) {
3741 assert(Arg.getKind() == TemplateArgument::Declaration &&
3742 "Only declaration template arguments permitted here");
3743 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3744
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003745 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003746 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3747 // If the value is a class member, we might have a pointer-to-member.
3748 // Determine whether the non-type template template parameter is of
3749 // pointer-to-member type. If so, we need to build an appropriate
3750 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3751 // would refer to the member itself.
3752 if (ParamType->isMemberPointerType()) {
3753 QualType ClassType
3754 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3755 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003756 = NestedNameSpecifier::Create(Context, 0, false,
3757 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003758 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003759 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003760
3761 // The actual value-ness of this is unimportant, but for
3762 // internal consistency's sake, references to instance methods
3763 // are r-values.
3764 ExprValueKind VK = VK_LValue;
3765 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3766 VK = VK_RValue;
3767
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003768 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003769 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003770 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003771 Loc,
3772 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003773 if (RefExpr.isInvalid())
3774 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003775
John McCall2de56d12010-08-25 11:45:40 +00003776 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003777
Douglas Gregorc0c83002010-04-30 21:46:38 +00003778 // We might need to perform a trailing qualification conversion, since
3779 // the element type on the parameter could be more qualified than the
3780 // element type in the expression we constructed.
3781 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John Wiegley429bb272011-04-08 18:41:53 +00003782 ParamType.getUnqualifiedType(), false))
3783 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003784
Douglas Gregor02024a92010-03-28 02:42:43 +00003785 assert(!RefExpr.isInvalid() &&
3786 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003787 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003788 return move(RefExpr);
3789 }
3790 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003791
Douglas Gregor02024a92010-03-28 02:42:43 +00003792 QualType T = VD->getType().getNonReferenceType();
3793 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003794 // When the non-type template parameter is a pointer, take the
3795 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003796 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003797 if (RefExpr.isInvalid())
3798 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003799
3800 if (T->isFunctionType() || T->isArrayType()) {
3801 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00003802 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
3803 if (RefExpr.isInvalid())
3804 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003805
3806 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003807 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003808
Douglas Gregorb7a09262010-04-01 18:32:35 +00003809 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003810 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003811 }
3812
John McCallf89e55a2010-11-18 06:31:45 +00003813 ExprValueKind VK = VK_RValue;
3814
Douglas Gregor02024a92010-03-28 02:42:43 +00003815 // If the non-type template parameter has reference type, qualify the
3816 // resulting declaration reference with the extra qualifiers on the
3817 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003818 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3819 VK = VK_LValue;
3820 T = Context.getQualifiedType(T,
3821 TargetRef->getPointeeType().getQualifiers());
3822 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003823
John McCallf89e55a2010-11-18 06:31:45 +00003824 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003825}
3826
3827/// \brief Construct a new expression that refers to the given
3828/// integral template argument with the given source-location
3829/// information.
3830///
3831/// This routine takes care of the mapping from an integral template
3832/// argument (which may have any integral type) to the appropriate
3833/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003834ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003835Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3836 SourceLocation Loc) {
3837 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003838 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003839 QualType T = Arg.getIntegralType();
3840 if (T->isCharType() || T->isWideCharType())
3841 return Owned(new (Context) CharacterLiteral(
3842 Arg.getAsIntegral()->getZExtValue(),
Chris Lattner223de242011-04-25 20:37:58 +00003843 T->isWideCharType(), T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00003844 if (T->isBooleanType())
3845 return Owned(new (Context) CXXBoolLiteralExpr(
3846 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00003847 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00003848
Chris Lattner223de242011-04-25 20:37:58 +00003849 // If this is an enum type that we're instantiating, we need to use an integer
3850 // type the same size as the enumerator. We don't want to build an
3851 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003852 QualType BT;
3853 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00003854 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003855 else
3856 BT = T;
3857
3858 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003859 if (T->isEnumeralType()) {
3860 // FIXME: This is a hack. We need a better way to handle substituted
3861 // non-type template parameters.
Chris Lattner223de242011-04-25 20:37:58 +00003862 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
3863 Context.getTrivialTypeSourceInfo(T, Loc),
3864 Loc, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003865 }
3866
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003867 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003868}
3869
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003870/// \brief Match two template parameters within template parameter lists.
3871static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3872 bool Complain,
3873 Sema::TemplateParameterListEqualKind Kind,
3874 SourceLocation TemplateArgLoc) {
3875 // Check the actual kind (type, non-type, template).
3876 if (Old->getKind() != New->getKind()) {
3877 if (Complain) {
3878 unsigned NextDiag = diag::err_template_param_different_kind;
3879 if (TemplateArgLoc.isValid()) {
3880 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3881 NextDiag = diag::note_template_param_different_kind;
3882 }
3883 S.Diag(New->getLocation(), NextDiag)
3884 << (Kind != Sema::TPL_TemplateMatch);
3885 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3886 << (Kind != Sema::TPL_TemplateMatch);
3887 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003888
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003889 return false;
3890 }
3891
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003892 // Check that both are parameter packs are neither are parameter packs.
3893 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003894 // template template parameter, the template template parameter can have
3895 // a parameter pack where the template template argument does not.
3896 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3897 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3898 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003899 if (Complain) {
3900 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3901 if (TemplateArgLoc.isValid()) {
3902 S.Diag(TemplateArgLoc,
3903 diag::err_template_arg_template_params_mismatch);
3904 NextDiag = diag::note_template_parameter_pack_non_pack;
3905 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003906
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003907 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3908 : isa<NonTypeTemplateParmDecl>(New)? 1
3909 : 2;
3910 S.Diag(New->getLocation(), NextDiag)
3911 << ParamKind << New->isParameterPack();
3912 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3913 << ParamKind << Old->isParameterPack();
3914 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003915
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003916 return false;
3917 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003918
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003919 // For non-type template parameters, check the type of the parameter.
3920 if (NonTypeTemplateParmDecl *OldNTTP
3921 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3922 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003923
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003924 // If we are matching a template template argument to a template
3925 // template parameter and one of the non-type template parameter types
3926 // is dependent, then we must wait until template instantiation time
3927 // to actually compare the arguments.
3928 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3929 (OldNTTP->getType()->isDependentType() ||
3930 NewNTTP->getType()->isDependentType()))
3931 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003932
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003933 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3934 if (Complain) {
3935 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3936 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003937 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003938 diag::err_template_arg_template_params_mismatch);
3939 NextDiag = diag::note_template_nontype_parm_different_type;
3940 }
3941 S.Diag(NewNTTP->getLocation(), NextDiag)
3942 << NewNTTP->getType()
3943 << (Kind != Sema::TPL_TemplateMatch);
3944 S.Diag(OldNTTP->getLocation(),
3945 diag::note_template_nontype_parm_prev_declaration)
3946 << OldNTTP->getType();
3947 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003948
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003949 return false;
3950 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003951
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003952 return true;
3953 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003954
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003955 // For template template parameters, check the template parameter types.
3956 // The template parameter lists of template template
3957 // parameters must agree.
3958 if (TemplateTemplateParmDecl *OldTTP
3959 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003960 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003961 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3962 OldTTP->getTemplateParameters(),
3963 Complain,
3964 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003965 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003966 : Kind),
3967 TemplateArgLoc);
3968 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003969
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003970 return true;
3971}
Douglas Gregor02024a92010-03-28 02:42:43 +00003972
Douglas Gregora0347822011-01-13 00:08:50 +00003973/// \brief Diagnose a known arity mismatch when comparing template argument
3974/// lists.
3975static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003976void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003977 TemplateParameterList *New,
3978 TemplateParameterList *Old,
3979 Sema::TemplateParameterListEqualKind Kind,
3980 SourceLocation TemplateArgLoc) {
3981 unsigned NextDiag = diag::err_template_param_list_different_arity;
3982 if (TemplateArgLoc.isValid()) {
3983 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3984 NextDiag = diag::note_template_param_list_different_arity;
3985 }
3986 S.Diag(New->getTemplateLoc(), NextDiag)
3987 << (New->size() > Old->size())
3988 << (Kind != Sema::TPL_TemplateMatch)
3989 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3990 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3991 << (Kind != Sema::TPL_TemplateMatch)
3992 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3993}
3994
Douglas Gregorddc29e12009-02-06 22:42:48 +00003995/// \brief Determine whether the given template parameter lists are
3996/// equivalent.
3997///
Mike Stump1eb44332009-09-09 15:08:12 +00003998/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003999/// source code as part of a new template declaration.
4000///
4001/// \param Old The old template parameter list, typically found via
4002/// name lookup of the template declared with this template parameter
4003/// list.
4004///
4005/// \param Complain If true, this routine will produce a diagnostic if
4006/// the template parameter lists are not equivalent.
4007///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004008/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004009///
4010/// \param TemplateArgLoc If this source location is valid, then we
4011/// are actually checking the template parameter list of a template
4012/// argument (New) against the template parameter list of its
4013/// corresponding template template parameter (Old). We produce
4014/// slightly different diagnostics in this scenario.
4015///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004016/// \returns True if the template parameter lists are equal, false
4017/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004018bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004019Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4020 TemplateParameterList *Old,
4021 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004022 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004023 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004024 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4025 if (Complain)
4026 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4027 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004028
4029 return false;
4030 }
4031
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004032 // C++0x [temp.arg.template]p3:
4033 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004034 // when each of the template parameters in the template-parameter-list of
4035 // the template-argument's corresponding class template or template alias
4036 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004037 // template-parameter-list of P. [...]
4038 TemplateParameterList::iterator NewParm = New->begin();
4039 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004040 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004041 OldParmEnd = Old->end();
4042 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004043 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4044 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004045 if (NewParm == NewParmEnd) {
4046 if (Complain)
4047 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4048 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004049
Douglas Gregora0347822011-01-13 00:08:50 +00004050 return false;
4051 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004052
Douglas Gregora0347822011-01-13 00:08:50 +00004053 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4054 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004055 return false;
4056
Douglas Gregora0347822011-01-13 00:08:50 +00004057 ++NewParm;
4058 continue;
4059 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004060
Douglas Gregora0347822011-01-13 00:08:50 +00004061 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004062 // [...] When P's template- parameter-list contains a template parameter
4063 // pack (14.5.3), the template parameter pack will match zero or more
4064 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004065 // template-parameter-list of A with the same type and form as the
4066 // template parameter pack in P (ignoring whether those template
4067 // parameters are template parameter packs).
4068 for (; NewParm != NewParmEnd; ++NewParm) {
4069 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4070 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004071 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004072 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004073 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004074
Douglas Gregora0347822011-01-13 00:08:50 +00004075 // Make sure we exhausted all of the arguments.
4076 if (NewParm != NewParmEnd) {
4077 if (Complain)
4078 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4079 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004080
Douglas Gregora0347822011-01-13 00:08:50 +00004081 return false;
4082 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004083
Douglas Gregorddc29e12009-02-06 22:42:48 +00004084 return true;
4085}
4086
4087/// \brief Check whether a template can be declared within this scope.
4088///
4089/// If the template declaration is valid in this scope, returns
4090/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004091bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004092Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004093 // Find the nearest enclosing declaration scope.
4094 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4095 (S->getFlags() & Scope::TemplateParamScope) != 0)
4096 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004097
Douglas Gregorddc29e12009-02-06 22:42:48 +00004098 // C++ [temp]p2:
4099 // A template-declaration can appear only as a namespace scope or
4100 // class scope declaration.
4101 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004102 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4103 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004104 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004105 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004106
Eli Friedman1503f772009-07-31 01:43:05 +00004107 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004108 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004109
4110 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4111 return false;
4112
Mike Stump1eb44332009-09-09 15:08:12 +00004113 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004114 diag::err_template_outside_namespace_or_class_scope)
4115 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004116}
Douglas Gregorcc636682009-02-17 23:15:12 +00004117
Douglas Gregord5cb8762009-10-07 00:13:32 +00004118/// \brief Determine what kind of template specialization the given declaration
4119/// is.
4120static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4121 if (!D)
4122 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004123
Douglas Gregorf6b11852009-10-08 15:14:33 +00004124 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4125 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004126 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4127 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004128 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4129 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004130
Douglas Gregord5cb8762009-10-07 00:13:32 +00004131 return TSK_Undeclared;
4132}
4133
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004134/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004135/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004136///
Douglas Gregor9302da62009-10-14 23:50:59 +00004137/// This routine determines whether a template specialization can be declared
4138/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004139///
4140/// \param S the semantic analysis object for which this check is being
4141/// performed.
4142///
4143/// \param Specialized the entity being specialized or instantiated, which
4144/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004145/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004146/// member class).
4147///
4148/// \param PrevDecl the previous declaration of this entity, if any.
4149///
4150/// \param Loc the location of the explicit specialization or instantiation of
4151/// this entity.
4152///
4153/// \param IsPartialSpecialization whether this is a partial specialization of
4154/// a class template.
4155///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004156/// \returns true if there was an error that we cannot recover from, false
4157/// otherwise.
4158static bool CheckTemplateSpecializationScope(Sema &S,
4159 NamedDecl *Specialized,
4160 NamedDecl *PrevDecl,
4161 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004162 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004163 // Keep these "kind" numbers in sync with the %select statements in the
4164 // various diagnostics emitted by this routine.
4165 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004166 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004167 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004168 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004169 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004170 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004171 EntityKind = 3;
4172 else if (isa<VarDecl>(Specialized))
4173 EntityKind = 4;
4174 else if (isa<RecordDecl>(Specialized))
4175 EntityKind = 5;
4176 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004177 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4178 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004179 return true;
4180 }
4181
Douglas Gregor88b70942009-02-25 22:02:03 +00004182 // C++ [temp.expl.spec]p2:
4183 // An explicit specialization shall be declared in the namespace
4184 // of which the template is a member, or, for member templates, in
4185 // the namespace of which the enclosing class or enclosing class
4186 // template is a member. An explicit specialization of a member
4187 // function, member class or static data member of a class
4188 // template shall be declared in the namespace of which the class
4189 // template is a member. Such a declaration may also be a
4190 // definition. If the declaration is not a definition, the
4191 // specialization may be defined later in the name- space in which
4192 // the explicit specialization was declared, or in a namespace
4193 // that encloses the one in which the explicit specialization was
4194 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004195 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004196 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004197 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004198 return true;
4199 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004200
Douglas Gregor0a407472009-10-07 17:30:37 +00004201 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4202 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004203 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004204 return true;
4205 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004206
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004207 // C++ [temp.class.spec]p6:
4208 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004209 // in any namespace scope in which its definition may be defined (14.5.1
4210 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004211 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004212 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004213 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004214 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004215 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004216 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4217 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004218 // C++ [temp.exp.spec]p2:
4219 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004220 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004221 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004222 // An explicit specialization of a member function, member class or
4223 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004224 // namespace of which the class template is a member.
4225 //
4226 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004227 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004228 // the specialized template.
4229 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4230 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004231 bool IsCPlusPlus0xExtension
4232 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004233 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004234 S.Diag(Loc, IsCPlusPlus0xExtension
4235 ? diag::ext_template_spec_decl_out_of_scope_global
4236 : diag::err_template_spec_decl_out_of_scope_global)
4237 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004238 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004239 S.Diag(Loc, IsCPlusPlus0xExtension
4240 ? diag::ext_template_spec_decl_out_of_scope
4241 : diag::err_template_spec_decl_out_of_scope)
4242 << EntityKind << Specialized
4243 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004244
Douglas Gregor9302da62009-10-14 23:50:59 +00004245 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4246 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004247 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004248 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004249
4250 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004251 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004252 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004253 // specializations of function templates, static data members, and member
4254 // functions, so we skip the check here for those kinds of entities.
4255 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004256 // Should we refactor that check, so that it occurs later?
4257 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004258 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4259 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004260 if (isa<TranslationUnitDecl>(SpecializedContext))
4261 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4262 << EntityKind << Specialized;
4263 else if (isa<NamespaceDecl>(SpecializedContext))
4264 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4265 << EntityKind << Specialized
4266 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004267
Douglas Gregor9302da62009-10-14 23:50:59 +00004268 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004269 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004270
Douglas Gregord5cb8762009-10-07 00:13:32 +00004271 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004272
Douglas Gregor88b70942009-02-25 22:02:03 +00004273 return false;
4274}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004275
Douglas Gregorbacb9492011-01-03 21:13:47 +00004276/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4277/// that checks non-type template partial specialization arguments.
4278static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4279 NonTypeTemplateParmDecl *Param,
4280 const TemplateArgument *Args,
4281 unsigned NumArgs) {
4282 for (unsigned I = 0; I != NumArgs; ++I) {
4283 if (Args[I].getKind() == TemplateArgument::Pack) {
4284 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004285 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004286 Args[I].pack_size()))
4287 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004288
Douglas Gregore94866f2009-06-12 21:21:02 +00004289 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004290 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004291
Douglas Gregorbacb9492011-01-03 21:13:47 +00004292 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004293 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004294 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004295 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004296
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004297 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004298 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4299 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004300
4301 // Strip off any implicit casts we added as part of type checking.
4302 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4303 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004304
Douglas Gregore94866f2009-06-12 21:21:02 +00004305 // C++ [temp.class.spec]p8:
4306 // A non-type argument is non-specialized if it is the name of a
4307 // non-type parameter. All other non-type arguments are
4308 // specialized.
4309 //
4310 // Below, we check the two conditions that only apply to
4311 // specialized non-type arguments, so skip any non-specialized
4312 // arguments.
4313 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004314 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004315 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004316
Douglas Gregore94866f2009-06-12 21:21:02 +00004317 // C++ [temp.class.spec]p9:
4318 // Within the argument list of a class template partial
4319 // specialization, the following restrictions apply:
4320 // -- A partially specialized non-type argument expression
4321 // shall not involve a template parameter of the partial
4322 // specialization except when the argument expression is a
4323 // simple identifier.
4324 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004325 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004326 diag::err_dependent_non_type_arg_in_partial_spec)
4327 << ArgExpr->getSourceRange();
4328 return true;
4329 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004330
Douglas Gregore94866f2009-06-12 21:21:02 +00004331 // -- The type of a template parameter corresponding to a
4332 // specialized non-type argument shall not be dependent on a
4333 // parameter of the specialization.
4334 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004335 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004336 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4337 << Param->getType()
4338 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004339 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004340 return true;
4341 }
4342 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004343
Douglas Gregorbacb9492011-01-03 21:13:47 +00004344 return false;
4345}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004346
Douglas Gregorbacb9492011-01-03 21:13:47 +00004347/// \brief Check the non-type template arguments of a class template
4348/// partial specialization according to C++ [temp.class.spec]p9.
4349///
4350/// \param TemplateParams the template parameters of the primary class
4351/// template.
4352///
4353/// \param TemplateArg the template arguments of the class template
4354/// partial specialization.
4355///
4356/// \returns true if there was an error, false otherwise.
4357static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4358 TemplateParameterList *TemplateParams,
4359 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4360 const TemplateArgument *ArgList = TemplateArgs.data();
4361
4362 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4363 NonTypeTemplateParmDecl *Param
4364 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4365 if (!Param)
4366 continue;
4367
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004368 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004369 &ArgList[I], 1))
4370 return true;
4371 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004372
4373 return false;
4374}
4375
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004376/// \brief Retrieve the previous declaration of the given declaration.
4377static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4378 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4379 return VD->getPreviousDeclaration();
4380 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4381 return FD->getPreviousDeclaration();
4382 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4383 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004384 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004385 return TD->getPreviousDeclaration();
4386 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4387 return FTD->getPreviousDeclaration();
4388 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4389 return CTD->getPreviousDeclaration();
4390 return 0;
4391}
4392
John McCalld226f652010-08-21 09:40:31 +00004393DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004394Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4395 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004396 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004397 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004398 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004399 SourceLocation TemplateNameLoc,
4400 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004401 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004402 SourceLocation RAngleLoc,
4403 AttributeList *Attr,
4404 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004405 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004406
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004407 // NOTE: KWLoc is the location of the tag keyword. This will instead
4408 // store the location of the outermost template keyword in the declaration.
4409 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4410 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4411
Douglas Gregorcc636682009-02-17 23:15:12 +00004412 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004413 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004414 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004415 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4416
4417 if (!ClassTemplate) {
4418 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004419 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004420 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4421 return true;
4422 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004423
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004424 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004425 bool isPartialSpecialization = false;
4426
Douglas Gregor88b70942009-02-25 22:02:03 +00004427 // Check the validity of the template headers that introduce this
4428 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004429 // FIXME: We probably shouldn't complain about these headers for
4430 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004431 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004432 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004433 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4434 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004435 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004436 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004437 isExplicitSpecialization,
4438 Invalid);
4439 if (Invalid)
4440 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004441
Douglas Gregor05396e22009-08-25 17:23:04 +00004442 if (TemplateParams && TemplateParams->size() > 0) {
4443 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004444
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004445 if (TUK == TUK_Friend) {
4446 Diag(KWLoc, diag::err_partial_specialization_friend)
4447 << SourceRange(LAngleLoc, RAngleLoc);
4448 return true;
4449 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004450
Douglas Gregor05396e22009-08-25 17:23:04 +00004451 // C++ [temp.class.spec]p10:
4452 // The template parameter list of a specialization shall not
4453 // contain default template argument values.
4454 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4455 Decl *Param = TemplateParams->getParam(I);
4456 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4457 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004458 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004459 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004460 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004461 }
4462 } else if (NonTypeTemplateParmDecl *NTTP
4463 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4464 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004465 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004466 diag::err_default_arg_in_partial_spec)
4467 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004468 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004469 }
4470 } else {
4471 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004472 if (TTP->hasDefaultArgument()) {
4473 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004474 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004475 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004476 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004477 }
4478 }
4479 }
Douglas Gregora735b202009-10-13 14:39:41 +00004480 } else if (TemplateParams) {
4481 if (TUK == TUK_Friend)
4482 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004483 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004484 SourceRange(TemplateParams->getTemplateLoc(),
4485 TemplateParams->getRAngleLoc()))
4486 << SourceRange(LAngleLoc, RAngleLoc);
4487 else
4488 isExplicitSpecialization = true;
4489 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004490 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004491 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004492 isExplicitSpecialization = true;
4493 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004494
Douglas Gregorcc636682009-02-17 23:15:12 +00004495 // Check that the specialization uses the same tag kind as the
4496 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004497 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4498 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004499 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004500 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004501 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004502 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004503 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004504 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004505 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004506 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004507 diag::note_previous_use);
4508 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4509 }
4510
Douglas Gregor40808ce2009-03-09 23:48:35 +00004511 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004512 TemplateArgumentListInfo TemplateArgs;
4513 TemplateArgs.setLAngleLoc(LAngleLoc);
4514 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004515 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004516
Douglas Gregor925910d2011-01-03 20:35:03 +00004517 // Check for unexpanded parameter packs in any of the template arguments.
4518 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004519 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004520 UPPC_PartialSpecialization))
4521 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004522
Douglas Gregorcc636682009-02-17 23:15:12 +00004523 // Check that the template argument list is well-formed for this
4524 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004525 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004526 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4527 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004528 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004529
Douglas Gregor910f8002010-11-07 23:05:16 +00004530 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004531 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004532
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004533 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004534 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004535 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004536 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004537 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004538 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004539 return true;
4540
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004541 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004542 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004543 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004544 TemplateArgs.size())) {
4545 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4546 << ClassTemplate->getDeclName();
4547 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004548 }
4549 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004550
Douglas Gregorcc636682009-02-17 23:15:12 +00004551 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004552 ClassTemplateSpecializationDecl *PrevDecl = 0;
4553
4554 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004555 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004556 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004557 = ClassTemplate->findPartialSpecialization(Converted.data(),
4558 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004559 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004560 else
4561 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004562 = ClassTemplate->findSpecialization(Converted.data(),
4563 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004564
4565 ClassTemplateSpecializationDecl *Specialization = 0;
4566
Douglas Gregor88b70942009-02-25 22:02:03 +00004567 // Check whether we can declare a class template specialization in
4568 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004569 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004570 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4571 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004572 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004573 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004574
Douglas Gregorb88e8882009-07-30 17:40:51 +00004575 // The canonical type
4576 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004577 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004578 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004579 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004580 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004581 // arguments was referenced but not declared, or we're only
4582 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004583 // declaration node as our own, updating its source location and
4584 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004585 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004586 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004587 if (TemplateParameterLists.size() > 0) {
4588 Specialization->setTemplateParameterListsInfo(Context,
4589 TemplateParameterLists.size(),
4590 (TemplateParameterList**) TemplateParameterLists.release());
4591 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004592 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004593 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004594 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004595 // Build the canonical type that describes the converted template
4596 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004597 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4598 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004599 Converted.data(),
4600 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004601
4602 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004603 ClassTemplate->getInjectedClassNameSpecialization())) {
4604 // C++ [temp.class.spec]p9b3:
4605 //
4606 // -- The argument list of the specialization shall not be identical
4607 // to the implicit argument list of the primary template.
4608 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4609 << (TUK == TUK_Definition)
4610 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4611 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4612 ClassTemplate->getIdentifier(),
4613 TemplateNameLoc,
4614 Attr,
4615 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004616 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004617 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004618 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004619 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004620
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004621 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004622 ClassTemplatePartialSpecializationDecl *PrevPartial
4623 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004624 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004625 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004626 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004627 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004628 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004629 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004630 TemplateParams,
4631 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004632 Converted.data(),
4633 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004634 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004635 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004636 PrevPartial,
4637 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004638 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004639 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004640 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004641 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004642 (TemplateParameterList**) TemplateParameterLists.release());
4643 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004644
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004645 if (!PrevPartial)
4646 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004647 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004648
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004649 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004650 // template specialization, make a note of that.
4651 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4652 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004653
Douglas Gregor031a5882009-06-13 00:26:55 +00004654 // Check that all of the template parameters of the class template
4655 // partial specialization are deducible from the template
4656 // arguments. If not, this class template partial specialization
4657 // will never be used.
4658 llvm::SmallVector<bool, 8> DeducibleParams;
4659 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004660 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004661 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004662 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004663 unsigned NumNonDeducible = 0;
4664 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4665 if (!DeducibleParams[I])
4666 ++NumNonDeducible;
4667
4668 if (NumNonDeducible) {
4669 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4670 << (NumNonDeducible > 1)
4671 << SourceRange(TemplateNameLoc, RAngleLoc);
4672 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4673 if (!DeducibleParams[I]) {
4674 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4675 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004676 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004677 diag::note_partial_spec_unused_parameter)
4678 << Param->getDeclName();
4679 else
Mike Stump1eb44332009-09-09 15:08:12 +00004680 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004681 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004682 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004683 }
4684 }
4685 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004686 } else {
4687 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004688 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004689 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004690 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004691 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004692 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004693 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004694 Converted.data(),
4695 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004696 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004697 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004698 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004699 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004700 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00004701 (TemplateParameterList**) TemplateParameterLists.release());
4702 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004703
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004704 if (!PrevDecl)
4705 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004706
4707 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004708 }
4709
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004710 // C++ [temp.expl.spec]p6:
4711 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004712 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004713 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004714 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004715 // use occurs; no diagnostic is required.
4716 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004717 bool Okay = false;
4718 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4719 // Is there any previous explicit specialization declaration?
4720 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4721 Okay = true;
4722 break;
4723 }
4724 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004725
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004726 if (!Okay) {
4727 SourceRange Range(TemplateNameLoc, RAngleLoc);
4728 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4729 << Context.getTypeDeclType(Specialization) << Range;
4730
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004731 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004732 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004733 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004734 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004735 return true;
4736 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004737 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004738
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004739 // If this is not a friend, note that this is an explicit specialization.
4740 if (TUK != TUK_Friend)
4741 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004742
4743 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004744 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004745 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004746 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004747 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004748 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004749 Diag(Def->getLocation(), diag::note_previous_definition);
4750 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004751 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004752 }
4753 }
4754
John McCall7f1b9872010-12-18 03:30:47 +00004755 if (Attr)
4756 ProcessDeclAttributeList(S, Specialization, Attr);
4757
Douglas Gregorfc705b82009-02-26 22:19:44 +00004758 // Build the fully-sugared type for this class template
4759 // specialization as the user wrote in the specialization
4760 // itself. This means that we'll pretty-print the type retrieved
4761 // from the specialization's declaration the way that the user
4762 // actually wrote the specialization, rather than formatting the
4763 // name based on the "canonical" representation used to store the
4764 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004765 TypeSourceInfo *WrittenTy
4766 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4767 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004768 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004769 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004770 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004771 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004772 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004773
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004774 // C++ [temp.expl.spec]p9:
4775 // A template explicit specialization is in the scope of the
4776 // namespace in which the template was defined.
4777 //
4778 // We actually implement this paragraph where we set the semantic
4779 // context (in the creation of the ClassTemplateSpecializationDecl),
4780 // but we also maintain the lexical context where the actual
4781 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004782 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004783
Douglas Gregorcc636682009-02-17 23:15:12 +00004784 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004785 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004786 Specialization->startDefinition();
4787
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004788 if (TUK == TUK_Friend) {
4789 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4790 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004791 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004792 /*FIXME:*/KWLoc);
4793 Friend->setAccess(AS_public);
4794 CurContext->addDecl(Friend);
4795 } else {
4796 // Add the specialization into its lexical context, so that it can
4797 // be seen when iterating through the list of declarations in that
4798 // context. However, specializations are not found by name lookup.
4799 CurContext->addDecl(Specialization);
4800 }
John McCalld226f652010-08-21 09:40:31 +00004801 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004802}
Douglas Gregord57959a2009-03-27 23:10:48 +00004803
John McCalld226f652010-08-21 09:40:31 +00004804Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004805 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004806 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004807 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4808}
4809
John McCalld226f652010-08-21 09:40:31 +00004810Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004811 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004812 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004813 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004814 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004815
Douglas Gregor52591bf2009-06-24 00:54:41 +00004816 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004817 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004818 }
Mike Stump1eb44332009-09-09 15:08:12 +00004819
Douglas Gregor52591bf2009-06-24 00:54:41 +00004820 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004821
John McCalld226f652010-08-21 09:40:31 +00004822 Decl *DP = HandleDeclarator(ParentScope, D,
4823 move(TemplateParameterLists),
4824 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004825 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004826 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004827 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004828 FunctionTemplate->getTemplatedDecl());
4829 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4830 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4831 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004832}
4833
John McCall75042392010-02-11 01:33:53 +00004834/// \brief Strips various properties off an implicit instantiation
4835/// that has just been explicitly specialized.
4836static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004837 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004838
4839 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4840 FD->setInlineSpecified(false);
4841 }
4842}
4843
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004844/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004845/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004846/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004847/// new specialization/instantiation will have any effect.
4848///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004849/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004850/// instantiation.
4851///
4852/// \param NewTSK the kind of the new explicit specialization or instantiation.
4853///
4854/// \param PrevDecl the previous declaration of the entity.
4855///
4856/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4857///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004858/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004859/// declaration was instantiated (either implicitly or explicitly).
4860///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004861/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004862/// specialization or instantiation has no effect and should be ignored.
4863///
4864/// \returns true if there was an error that should prevent the introduction of
4865/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004866bool
4867Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4868 TemplateSpecializationKind NewTSK,
4869 NamedDecl *PrevDecl,
4870 TemplateSpecializationKind PrevTSK,
4871 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004872 bool &HasNoEffect) {
4873 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004874
Douglas Gregor454885e2009-10-15 15:54:05 +00004875 switch (NewTSK) {
4876 case TSK_Undeclared:
4877 case TSK_ImplicitInstantiation:
4878 assert(false && "Don't check implicit instantiations here");
4879 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004880
Douglas Gregor454885e2009-10-15 15:54:05 +00004881 case TSK_ExplicitSpecialization:
4882 switch (PrevTSK) {
4883 case TSK_Undeclared:
4884 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004885 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004886 // explicitly specialized or has merely been mentioned without any
4887 // instantiation.
4888 return false;
4889
4890 case TSK_ImplicitInstantiation:
4891 if (PrevPointOfInstantiation.isInvalid()) {
4892 // The declaration itself has not actually been instantiated, so it is
4893 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004894 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004895 return false;
4896 }
4897 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004898
Douglas Gregor454885e2009-10-15 15:54:05 +00004899 case TSK_ExplicitInstantiationDeclaration:
4900 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004901 assert((PrevTSK == TSK_ImplicitInstantiation ||
4902 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004903 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004904
Douglas Gregor454885e2009-10-15 15:54:05 +00004905 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004906 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004907 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004908 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004909 // implicit instantiation to take place, in every translation unit in
4910 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004911 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4912 // Is there any previous explicit specialization declaration?
4913 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4914 return false;
4915 }
4916
Douglas Gregor0d035142009-10-27 18:42:08 +00004917 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004918 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004919 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004920 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004921
Douglas Gregor454885e2009-10-15 15:54:05 +00004922 return true;
4923 }
4924 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004925
Douglas Gregor454885e2009-10-15 15:54:05 +00004926 case TSK_ExplicitInstantiationDeclaration:
4927 switch (PrevTSK) {
4928 case TSK_ExplicitInstantiationDeclaration:
4929 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004930 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004931 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004932
Douglas Gregor454885e2009-10-15 15:54:05 +00004933 case TSK_Undeclared:
4934 case TSK_ImplicitInstantiation:
4935 // We're explicitly instantiating something that may have already been
4936 // implicitly instantiated; that's fine.
4937 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004938
Douglas Gregor454885e2009-10-15 15:54:05 +00004939 case TSK_ExplicitSpecialization:
4940 // C++0x [temp.explicit]p4:
4941 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004942 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004943 // specialization for that template, the explicit instantiation has no
4944 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004945 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004946 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004947
Douglas Gregor454885e2009-10-15 15:54:05 +00004948 case TSK_ExplicitInstantiationDefinition:
4949 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004950 // If an entity is the subject of both an explicit instantiation
4951 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004952 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004953 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004954 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004955 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004956 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004957 assert(PrevPointOfInstantiation.isValid() &&
4958 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004959 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004960 return false;
4961 }
4962 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004963
Douglas Gregor454885e2009-10-15 15:54:05 +00004964 case TSK_ExplicitInstantiationDefinition:
4965 switch (PrevTSK) {
4966 case TSK_Undeclared:
4967 case TSK_ImplicitInstantiation:
4968 // We're explicitly instantiating something that may have already been
4969 // implicitly instantiated; that's fine.
4970 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004971
Douglas Gregor454885e2009-10-15 15:54:05 +00004972 case TSK_ExplicitSpecialization:
4973 // C++ DR 259, C++0x [temp.explicit]p4:
4974 // For a given set of template parameters, if an explicit
4975 // instantiation of a template appears after a declaration of
4976 // an explicit specialization for that template, the explicit
4977 // instantiation has no effect.
4978 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004979 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004980 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004981 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004982 if (!getLangOptions().CPlusPlus0x) {
4983 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004984 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004985 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004986 diag::note_previous_template_specialization);
4987 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004988 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004989 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004990
Douglas Gregor454885e2009-10-15 15:54:05 +00004991 case TSK_ExplicitInstantiationDeclaration:
4992 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004993 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004994 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004995
Douglas Gregor454885e2009-10-15 15:54:05 +00004996 case TSK_ExplicitInstantiationDefinition:
4997 // C++0x [temp.spec]p5:
4998 // For a given template and a given set of template-arguments,
4999 // - an explicit instantiation definition shall appear at most once
5000 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005001 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005002 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005003 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005004 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005005 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005006 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005007 }
5008 break;
5009 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005010
Douglas Gregor454885e2009-10-15 15:54:05 +00005011 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005012
Douglas Gregor454885e2009-10-15 15:54:05 +00005013 return false;
5014}
5015
John McCallaf2094e2010-04-08 09:05:18 +00005016/// \brief Perform semantic analysis for the given dependent function
5017/// template specialization. The only possible way to get a dependent
5018/// function template specialization is with a friend declaration,
5019/// like so:
5020///
5021/// template <class T> void foo(T);
5022/// template <class T> class A {
5023/// friend void foo<>(T);
5024/// };
5025///
5026/// There really isn't any useful analysis we can do here, so we
5027/// just store the information.
5028bool
5029Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5030 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5031 LookupResult &Previous) {
5032 // Remove anything from Previous that isn't a function template in
5033 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005034 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005035 LookupResult::Filter F = Previous.makeFilter();
5036 while (F.hasNext()) {
5037 NamedDecl *D = F.next()->getUnderlyingDecl();
5038 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005039 !FDLookupContext->InEnclosingNamespaceSetOf(
5040 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005041 F.erase();
5042 }
5043 F.done();
5044
5045 // Should this be diagnosed here?
5046 if (Previous.empty()) return true;
5047
5048 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5049 ExplicitTemplateArgs);
5050 return false;
5051}
5052
Abramo Bagnarae03db982010-05-20 15:32:11 +00005053/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005054/// specialization.
5055///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005056/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005057/// explicit function template specialization. On successful completion,
5058/// the function declaration \p FD will become a function template
5059/// specialization.
5060///
5061/// \param FD the function declaration, which will be updated to become a
5062/// function template specialization.
5063///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005064/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5065/// if any. Note that this may be valid info even when 0 arguments are
5066/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5067/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005068///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005069/// \param PrevDecl the set of declarations that may be specialized by
5070/// this function specialization.
5071bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005072Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005073 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005074 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005075 // The set of function template specializations that could match this
5076 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005077 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005078
Sebastian Redl7a126a42010-08-31 00:36:30 +00005079 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005080 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5081 I != E; ++I) {
5082 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5083 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005084 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005085 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005086 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5087 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005088 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005089
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005090 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005091 // A trailing template-argument can be left unspecified in the
5092 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005093 // provided it can be deduced from the function argument type.
5094 // Perform template argument deduction to determine whether we may be
5095 // specializing this template.
5096 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005097 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005098 FunctionDecl *Specialization = 0;
5099 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005100 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005101 FD->getType(),
5102 Specialization,
5103 Info)) {
5104 // FIXME: Template argument deduction failed; record why it failed, so
5105 // that we can provide nifty diagnostics.
5106 (void)TDK;
5107 continue;
5108 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005109
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005110 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005111 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005112 }
5113 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005114
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005115 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005116 UnresolvedSetIterator Result
5117 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005118 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005119 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005120 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005121 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005122 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005123 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005124 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005125 return true;
John McCallc373d482010-01-27 01:50:18 +00005126
5127 // Ignore access information; it doesn't figure into redeclaration checking.
5128 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005129
5130 FunctionTemplateSpecializationInfo *SpecInfo
5131 = Specialization->getTemplateSpecializationInfo();
5132 assert(SpecInfo && "Function template specialization info missing?");
5133 {
5134 // Note: do not overwrite location info if previous template
5135 // specialization kind was explicit.
5136 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5137 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5138 Specialization->setLocation(FD->getLocation());
5139 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005140
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005141 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005142 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005143
5144 // If this is a friend declaration, then we're not really declaring
5145 // an explicit specialization.
5146 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005147
Douglas Gregord5cb8762009-10-07 00:13:32 +00005148 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005149 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005150 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005151 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005152 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005153 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005154 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005155
5156 // C++ [temp.expl.spec]p6:
5157 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005158 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005159 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005160 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005161 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005162 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005163 if (!isFriend &&
5164 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005165 TSK_ExplicitSpecialization,
5166 Specialization,
5167 SpecInfo->getTemplateSpecializationKind(),
5168 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005169 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005170 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005171
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005172 // Mark the prior declaration as an explicit specialization, so that later
5173 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005174 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005175 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005176 MarkUnusedFileScopedDecl(Specialization);
5177 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005178
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005179 // Turn the given function declaration into a function template
5180 // specialization, with the template arguments from the previous
5181 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005182 // Take copies of (semantic and syntactic) template argument lists.
5183 const TemplateArgumentList* TemplArgs = new (Context)
5184 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5185 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5186 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005187 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005188 TemplArgs, /*InsertPos=*/0,
5189 SpecInfo->getTemplateSpecializationKind(),
5190 TemplArgsAsWritten);
5191
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005192 // The "previous declaration" for this function template specialization is
5193 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005194 Previous.clear();
5195 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005196 return false;
5197}
5198
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005199/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005200/// specialization.
5201///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005202/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005203/// explicit member function specialization. On successful completion,
5204/// the function declaration \p FD will become a member function
5205/// specialization.
5206///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005207/// \param Member the member declaration, which will be updated to become a
5208/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005209///
John McCall68263142009-11-18 22:49:29 +00005210/// \param Previous the set of declarations, one of which may be specialized
5211/// by this function specialization; the set will be modified to contain the
5212/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005213bool
John McCall68263142009-11-18 22:49:29 +00005214Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005215 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005216
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005217 // Try to find the member we are instantiating.
5218 NamedDecl *Instantiation = 0;
5219 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005220 MemberSpecializationInfo *MSInfo = 0;
5221
John McCall68263142009-11-18 22:49:29 +00005222 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005223 // Nowhere to look anyway.
5224 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005225 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5226 I != E; ++I) {
5227 NamedDecl *D = (*I)->getUnderlyingDecl();
5228 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005229 if (Context.hasSameType(Function->getType(), Method->getType())) {
5230 Instantiation = Method;
5231 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005232 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005233 break;
5234 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005235 }
5236 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005237 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005238 VarDecl *PrevVar;
5239 if (Previous.isSingleResult() &&
5240 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005241 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005242 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005243 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005244 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005245 }
5246 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005247 CXXRecordDecl *PrevRecord;
5248 if (Previous.isSingleResult() &&
5249 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5250 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005251 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005252 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005253 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005254 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005255
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005256 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005257 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005258 // specializations are always out-of-line, the caller will complain about
5259 // this mismatch later.
5260 return false;
5261 }
John McCall77e8b112010-04-13 20:37:33 +00005262
5263 // If this is a friend, just bail out here before we start turning
5264 // things into explicit specializations.
5265 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5266 // Preserve instantiation information.
5267 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5268 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5269 cast<CXXMethodDecl>(InstantiatedFrom),
5270 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5271 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5272 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5273 cast<CXXRecordDecl>(InstantiatedFrom),
5274 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5275 }
5276
5277 Previous.clear();
5278 Previous.addDecl(Instantiation);
5279 return false;
5280 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005281
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005282 // Make sure that this is a specialization of a member.
5283 if (!InstantiatedFrom) {
5284 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5285 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005286 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5287 return true;
5288 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005289
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005290 // C++ [temp.expl.spec]p6:
5291 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005292 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005293 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005294 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005295 // use occurs; no diagnostic is required.
5296 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005297
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005298 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005299 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5300 TSK_ExplicitSpecialization,
5301 Instantiation,
5302 MSInfo->getTemplateSpecializationKind(),
5303 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005304 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005305 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005306
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005307 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005308 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005309 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005310 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005311 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005312 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005313
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005314 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005315 // the original declaration to note that it is an explicit specialization
5316 // (if it was previously an implicit instantiation). This latter step
5317 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005318 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005319 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5320 if (InstantiationFunction->getTemplateSpecializationKind() ==
5321 TSK_ImplicitInstantiation) {
5322 InstantiationFunction->setTemplateSpecializationKind(
5323 TSK_ExplicitSpecialization);
5324 InstantiationFunction->setLocation(Member->getLocation());
5325 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005326
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005327 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5328 cast<CXXMethodDecl>(InstantiatedFrom),
5329 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005330 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005331 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005332 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5333 if (InstantiationVar->getTemplateSpecializationKind() ==
5334 TSK_ImplicitInstantiation) {
5335 InstantiationVar->setTemplateSpecializationKind(
5336 TSK_ExplicitSpecialization);
5337 InstantiationVar->setLocation(Member->getLocation());
5338 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005339
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005340 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5341 cast<VarDecl>(InstantiatedFrom),
5342 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005343 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005344 } else {
5345 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005346 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5347 if (InstantiationClass->getTemplateSpecializationKind() ==
5348 TSK_ImplicitInstantiation) {
5349 InstantiationClass->setTemplateSpecializationKind(
5350 TSK_ExplicitSpecialization);
5351 InstantiationClass->setLocation(Member->getLocation());
5352 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005353
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005354 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005355 cast<CXXRecordDecl>(InstantiatedFrom),
5356 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005357 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005358
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005359 // Save the caller the trouble of having to figure out which declaration
5360 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005361 Previous.clear();
5362 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005363 return false;
5364}
5365
Douglas Gregor558c0322009-10-14 23:41:34 +00005366/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005367///
5368/// \returns true if a serious error occurs, false otherwise.
5369static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005370 SourceLocation InstLoc,
5371 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005372 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5373 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005374
Douglas Gregor669eed82010-07-13 00:10:04 +00005375 if (CurContext->isRecord()) {
5376 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5377 << D;
5378 return true;
5379 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005380
Douglas Gregor558c0322009-10-14 23:41:34 +00005381 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005382 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005383 // template.
5384 //
5385 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005386 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005387 !CurContext->Encloses(OrigContext)) {
5388 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005389 S.Diag(InstLoc,
5390 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005391 diag::err_explicit_instantiation_out_of_scope
5392 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005393 << D << NS;
5394 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005395 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005396 S.getLangOptions().CPlusPlus0x?
5397 diag::err_explicit_instantiation_must_be_global
5398 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005399 << D;
5400 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005401 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005402 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005403
Douglas Gregor558c0322009-10-14 23:41:34 +00005404 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005405 // If the name declared in the explicit instantiation is an unqualified
5406 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005407 // its template is declared or, if that namespace is inline (7.3.1), any
5408 // namespace from its enclosing namespace set.
5409 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005410 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005411
5412 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005413 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005414
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005415 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005416 S.getLangOptions().CPlusPlus0x?
5417 diag::err_explicit_instantiation_unqualified_wrong_namespace
5418 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005419 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005420 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005421 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005422}
5423
5424/// \brief Determine whether the given scope specifier has a template-id in it.
5425static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5426 if (!SS.isSet())
5427 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005428
Douglas Gregor558c0322009-10-14 23:41:34 +00005429 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005430 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005431 // or a static data member of a class template specialization, the name of
5432 // the class template specialization in the qualified-id for the member
5433 // name shall be a simple-template-id.
5434 //
5435 // C++98 has the same restriction, just worded differently.
5436 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5437 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005438 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005439 if (isa<TemplateSpecializationType>(T))
5440 return true;
5441
5442 return false;
5443}
5444
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005445// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005446DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005447Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005448 SourceLocation ExternLoc,
5449 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005450 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005451 SourceLocation KWLoc,
5452 const CXXScopeSpec &SS,
5453 TemplateTy TemplateD,
5454 SourceLocation TemplateNameLoc,
5455 SourceLocation LAngleLoc,
5456 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005457 SourceLocation RAngleLoc,
5458 AttributeList *Attr) {
5459 // Find the class template we're specializing
5460 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005461 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005462 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5463
5464 // Check that the specialization uses the same tag kind as the
5465 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005466 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5467 assert(Kind != TTK_Enum &&
5468 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005469 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005470 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005471 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005472 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005473 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005474 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005475 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005476 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005477 diag::note_previous_use);
5478 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5479 }
5480
Douglas Gregor558c0322009-10-14 23:41:34 +00005481 // C++0x [temp.explicit]p2:
5482 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005483 // definition and an explicit instantiation declaration. An explicit
5484 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005485 TemplateSpecializationKind TSK
5486 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5487 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005488
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005489 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005490 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005491 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005492
5493 // Check that the template argument list is well-formed for this
5494 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005495 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005496 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5497 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005498 return true;
5499
Douglas Gregor910f8002010-11-07 23:05:16 +00005500 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005501 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005502
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005503 // Find the class template specialization declaration that
5504 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005505 void *InsertPos = 0;
5506 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005507 = ClassTemplate->findSpecialization(Converted.data(),
5508 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005509
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005510 TemplateSpecializationKind PrevDecl_TSK
5511 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5512
Douglas Gregord5cb8762009-10-07 00:13:32 +00005513 // C++0x [temp.explicit]p2:
5514 // [...] An explicit instantiation shall appear in an enclosing
5515 // namespace of its template. [...]
5516 //
5517 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005518 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5519 SS.isSet()))
5520 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005521
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005522 ClassTemplateSpecializationDecl *Specialization = 0;
5523
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005524 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005525 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005526 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005527 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005528 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005529 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005530 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005531
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005532 // Even though HasNoEffect == true means that this explicit instantiation
5533 // has no effect on semantics, we go on to put its syntax in the AST.
5534
5535 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5536 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005537 // Since the only prior class template specialization with these
5538 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005539 // declaration node as our own, updating the source location
5540 // for the template name to reflect our new declaration.
5541 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005542 Specialization = PrevDecl;
5543 Specialization->setLocation(TemplateNameLoc);
5544 PrevDecl = 0;
5545 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005546 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005547
Douglas Gregor52604ab2009-09-11 21:19:12 +00005548 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005549 // Create a new class template specialization declaration node for
5550 // this explicit specialization.
5551 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005552 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005553 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005554 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005555 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005556 Converted.data(),
5557 Converted.size(),
5558 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005559 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005560
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005561 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005562 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005563 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005564 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005565 }
5566
5567 // Build the fully-sugared type for this explicit instantiation as
5568 // the user wrote in the explicit instantiation itself. This means
5569 // that we'll pretty-print the type retrieved from the
5570 // specialization's declaration the way that the user actually wrote
5571 // the explicit instantiation, rather than formatting the name based
5572 // on the "canonical" representation used to store the template
5573 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005574 TypeSourceInfo *WrittenTy
5575 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5576 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005577 Context.getTypeDeclType(Specialization));
5578 Specialization->setTypeAsWritten(WrittenTy);
5579 TemplateArgsIn.release();
5580
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005581 // Set source locations for keywords.
5582 Specialization->setExternLoc(ExternLoc);
5583 Specialization->setTemplateKeywordLoc(TemplateLoc);
5584
5585 // Add the explicit instantiation into its lexical context. However,
5586 // since explicit instantiations are never found by name lookup, we
5587 // just put it into the declaration context directly.
5588 Specialization->setLexicalDeclContext(CurContext);
5589 CurContext->addDecl(Specialization);
5590
5591 // Syntax is now OK, so return if it has no other effect on semantics.
5592 if (HasNoEffect) {
5593 // Set the template specialization kind.
5594 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005595 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005596 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005597
5598 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005599 // A definition of a class template or class member template
5600 // shall be in scope at the point of the explicit instantiation of
5601 // the class template or class member template.
5602 //
5603 // This check comes when we actually try to perform the
5604 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005605 ClassTemplateSpecializationDecl *Def
5606 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005607 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005608 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005609 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005610 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005611 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005612 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5613 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005614
Douglas Gregor0d035142009-10-27 18:42:08 +00005615 // Instantiate the members of this class template specialization.
5616 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005617 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005618 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005619 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5620
5621 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5622 // TSK_ExplicitInstantiationDefinition
5623 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5624 TSK == TSK_ExplicitInstantiationDefinition)
5625 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005626
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005627 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005628 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005629
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005630 // Set the template specialization kind.
5631 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005632 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005633}
5634
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005635// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005636DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005637Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005638 SourceLocation ExternLoc,
5639 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005640 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005641 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005642 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005643 IdentifierInfo *Name,
5644 SourceLocation NameLoc,
5645 AttributeList *Attr) {
5646
Douglas Gregor402abb52009-05-28 23:31:59 +00005647 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005648 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005649 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005650 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5651 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005652 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005653 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005654 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5655
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005656 if (!TagD)
5657 return true;
5658
John McCalld226f652010-08-21 09:40:31 +00005659 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005660 if (Tag->isEnum()) {
5661 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5662 << Context.getTypeDeclType(Tag);
5663 return true;
5664 }
5665
Douglas Gregord0c87372009-05-27 17:30:49 +00005666 if (Tag->isInvalidDecl())
5667 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005668
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005669 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5670 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5671 if (!Pattern) {
5672 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5673 << Context.getTypeDeclType(Record);
5674 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5675 return true;
5676 }
5677
Douglas Gregor558c0322009-10-14 23:41:34 +00005678 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005679 // If the explicit instantiation is for a class or member class, the
5680 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005681 // simple-template-id.
5682 //
5683 // C++98 has the same restriction, just worded differently.
5684 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005685 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005686 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005687
Douglas Gregor558c0322009-10-14 23:41:34 +00005688 // C++0x [temp.explicit]p2:
5689 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005690 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005691 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005692 TemplateSpecializationKind TSK
5693 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5694 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005695
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005696 // C++0x [temp.explicit]p2:
5697 // [...] An explicit instantiation shall appear in an enclosing
5698 // namespace of its template. [...]
5699 //
5700 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005701 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005702
Douglas Gregor454885e2009-10-15 15:54:05 +00005703 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005704 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005705 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005706 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005707 PrevDecl = Record;
5708 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005709 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005710 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005711 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005712 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005713 PrevDecl,
5714 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005715 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005716 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005717 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005718 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005719 return TagD;
5720 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005721
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005722 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005723 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005724 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005725 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005726 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005727 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005728 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005729 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005730 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005731 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5732 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005733 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5734 << Pattern;
5735 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005736 } else {
5737 if (InstantiateClass(NameLoc, Record, Def,
5738 getTemplateInstantiationArgs(Record),
5739 TSK))
5740 return true;
5741
Douglas Gregor952b0172010-02-11 01:04:33 +00005742 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005743 if (!RecordDef)
5744 return true;
5745 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005746 }
5747
Douglas Gregor0d035142009-10-27 18:42:08 +00005748 // Instantiate all of the members of the class.
5749 InstantiateClassMembers(NameLoc, RecordDef,
5750 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005751
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005752 if (TSK == TSK_ExplicitInstantiationDefinition)
5753 MarkVTableUsed(NameLoc, RecordDef, true);
5754
Mike Stump390b4cc2009-05-16 07:39:55 +00005755 // FIXME: We don't have any representation for explicit instantiations of
5756 // member classes. Such a representation is not needed for compilation, but it
5757 // should be available for clients that want to see all of the declarations in
5758 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005759 return TagD;
5760}
5761
John McCallf312b1e2010-08-26 23:41:50 +00005762DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5763 SourceLocation ExternLoc,
5764 SourceLocation TemplateLoc,
5765 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005766 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005767 // TODO: check if/when DNInfo should replace Name.
5768 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5769 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005770 if (!Name) {
5771 if (!D.isInvalidType())
5772 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5773 diag::err_explicit_instantiation_requires_name)
5774 << D.getDeclSpec().getSourceRange()
5775 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005776
Douglas Gregord5a423b2009-09-25 18:43:00 +00005777 return true;
5778 }
5779
5780 // The scope passed in may not be a decl scope. Zip up the scope tree until
5781 // we find one that is.
5782 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5783 (S->getFlags() & Scope::TemplateParamScope) != 0)
5784 S = S->getParent();
5785
5786 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005787 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5788 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005789 if (R.isNull())
5790 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005791
Douglas Gregord5a423b2009-09-25 18:43:00 +00005792 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5793 // Cannot explicitly instantiate a typedef.
5794 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5795 << Name;
5796 return true;
5797 }
5798
Douglas Gregor663b5a02009-10-14 20:14:33 +00005799 // C++0x [temp.explicit]p1:
5800 // [...] An explicit instantiation of a function template shall not use the
5801 // inline or constexpr specifiers.
5802 // Presumably, this also applies to member functions of class templates as
5803 // well.
5804 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005805 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005806 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005807 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005808
Douglas Gregor663b5a02009-10-14 20:14:33 +00005809 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005810
Douglas Gregor558c0322009-10-14 23:41:34 +00005811 // C++0x [temp.explicit]p2:
5812 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005813 // definition and an explicit instantiation declaration. An explicit
5814 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005815 TemplateSpecializationKind TSK
5816 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5817 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005818
Abramo Bagnara25777432010-08-11 22:01:17 +00005819 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005820 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005821
5822 if (!R->isFunctionType()) {
5823 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005824 // A [...] static data member of a class template can be explicitly
5825 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005826 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005827 if (Previous.isAmbiguous())
5828 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005829
John McCall1bcee0a2009-12-02 08:25:40 +00005830 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005831 if (!Prev || !Prev->isStaticDataMember()) {
5832 // We expect to see a data data member here.
5833 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5834 << Name;
5835 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5836 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005837 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005838 return true;
5839 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005840
Douglas Gregord5a423b2009-09-25 18:43:00 +00005841 if (!Prev->getInstantiatedFromStaticDataMember()) {
5842 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005843 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005844 diag::err_explicit_instantiation_data_member_not_instantiated)
5845 << Prev;
5846 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5847 // FIXME: Can we provide a note showing where this was declared?
5848 return true;
5849 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005850
Douglas Gregor558c0322009-10-14 23:41:34 +00005851 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005852 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005853 // or a static data member of a class template specialization, the name of
5854 // the class template specialization in the qualified-id for the member
5855 // name shall be a simple-template-id.
5856 //
5857 // C++98 has the same restriction, just worded differently.
5858 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005859 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005860 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005861 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005862
Douglas Gregor558c0322009-10-14 23:41:34 +00005863 // Check the scope of this explicit instantiation.
5864 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005865
Douglas Gregor454885e2009-10-15 15:54:05 +00005866 // Verify that it is okay to explicitly instantiate here.
5867 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5868 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005869 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005870 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005871 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005872 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005873 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005874 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005875 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005876 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005877
Douglas Gregord5a423b2009-09-25 18:43:00 +00005878 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005879 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005880 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005881 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005882
Douglas Gregord5a423b2009-09-25 18:43:00 +00005883 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005884 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005885 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005886
5887 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005888 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005889 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005890 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005891 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5892 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005893 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5894 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005895 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5896 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005897 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005898 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005899 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005900 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005901 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005902
Douglas Gregord5a423b2009-09-25 18:43:00 +00005903 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005904 // A [...] function [...] can be explicitly instantiated from its template.
5905 // A member function [...] of a class template can be explicitly
5906 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005907 // template.
John McCallc373d482010-01-27 01:50:18 +00005908 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005909 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5910 P != PEnd; ++P) {
5911 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005912 if (!HasExplicitTemplateArgs) {
5913 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5914 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5915 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005916
John McCallc373d482010-01-27 01:50:18 +00005917 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005918 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5919 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005920 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005921 }
5922 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005923
Douglas Gregord5a423b2009-09-25 18:43:00 +00005924 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5925 if (!FunTmpl)
5926 continue;
5927
John McCall5769d612010-02-08 23:07:23 +00005928 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005929 FunctionDecl *Specialization = 0;
5930 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005931 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005932 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005933 R, Specialization, Info)) {
5934 // FIXME: Keep track of almost-matches?
5935 (void)TDK;
5936 continue;
5937 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005938
John McCallc373d482010-01-27 01:50:18 +00005939 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005940 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005941
Douglas Gregord5a423b2009-09-25 18:43:00 +00005942 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005943 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005944 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005945 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005946 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5947 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5948 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005949
John McCallc373d482010-01-27 01:50:18 +00005950 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005951 return true;
John McCallc373d482010-01-27 01:50:18 +00005952
5953 // Ignore access control bits, we don't need them for redeclaration checking.
5954 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005955
Douglas Gregor0a897e32009-10-15 17:21:20 +00005956 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005957 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005958 diag::err_explicit_instantiation_member_function_not_instantiated)
5959 << Specialization
5960 << (Specialization->getTemplateSpecializationKind() ==
5961 TSK_ExplicitSpecialization);
5962 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5963 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005964 }
5965
Douglas Gregor0a897e32009-10-15 17:21:20 +00005966 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005967 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5968 PrevDecl = Specialization;
5969
Douglas Gregor0a897e32009-10-15 17:21:20 +00005970 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005971 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005972 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005973 PrevDecl,
5974 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005975 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005976 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005977 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005978
Douglas Gregor0a897e32009-10-15 17:21:20 +00005979 // FIXME: We may still want to build some representation of this
5980 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005981 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005982 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005983 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005984
5985 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005986
Douglas Gregor0a897e32009-10-15 17:21:20 +00005987 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005988 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005989
Douglas Gregor558c0322009-10-14 23:41:34 +00005990 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005991 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005992 // or a static data member of a class template specialization, the name of
5993 // the class template specialization in the qualified-id for the member
5994 // name shall be a simple-template-id.
5995 //
5996 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005997 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005998 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005999 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006000 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006001 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006002 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006003 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006004
Douglas Gregor558c0322009-10-14 23:41:34 +00006005 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006006 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006007 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006008 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006009 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006010
Douglas Gregord5a423b2009-09-25 18:43:00 +00006011 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006012 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006013}
6014
John McCallf312b1e2010-08-26 23:41:50 +00006015TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006016Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6017 const CXXScopeSpec &SS, IdentifierInfo *Name,
6018 SourceLocation TagLoc, SourceLocation NameLoc) {
6019 // This has to hold, because SS is expected to be defined.
6020 assert(Name && "Expected a name in a dependent tag");
6021
6022 NestedNameSpecifier *NNS
6023 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6024 if (!NNS)
6025 return true;
6026
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006027 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006028
Douglas Gregor48c89f42010-04-24 16:38:41 +00006029 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6030 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006031 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006032 return true;
6033 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006034
Douglas Gregor059101f2011-03-02 00:47:37 +00006035 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006036 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006037 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6038
6039 // Create type-source location information for this type.
6040 TypeLocBuilder TLB;
6041 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6042 TL.setKeywordLoc(TagLoc);
6043 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6044 TL.setNameLoc(NameLoc);
6045 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006046}
6047
John McCallf312b1e2010-08-26 23:41:50 +00006048TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006049Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6050 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006051 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006052 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006053 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006054
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006055 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6056 !getLangOptions().CPlusPlus0x)
6057 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006058 << FixItHint::CreateRemoval(TypenameLoc);
6059
Douglas Gregor2494dd02011-03-01 01:34:45 +00006060 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006061 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6062 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006063 if (T.isNull())
6064 return true;
John McCall63b43852010-04-29 23:50:39 +00006065
6066 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6067 if (isa<DependentNameType>(T)) {
6068 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006069 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006070 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006071 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006072 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006073 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006074 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006075 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006076 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006077 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006078
John McCallb3d87482010-08-24 05:47:05 +00006079 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006080}
6081
John McCallf312b1e2010-08-26 23:41:50 +00006082TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006083Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6084 const CXXScopeSpec &SS,
6085 SourceLocation TemplateLoc,
6086 TemplateTy TemplateIn,
6087 SourceLocation TemplateNameLoc,
6088 SourceLocation LAngleLoc,
6089 ASTTemplateArgsPtr TemplateArgsIn,
6090 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006091 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6092 !getLangOptions().CPlusPlus0x)
6093 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006094 << FixItHint::CreateRemoval(TypenameLoc);
6095
6096 // Translate the parser's template argument list in our AST format.
6097 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6098 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6099
6100 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006101 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6102 // Construct a dependent template specialization type.
6103 assert(DTN && "dependent template has non-dependent name?");
6104 assert(DTN->getQualifier()
6105 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6106 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6107 DTN->getQualifier(),
6108 DTN->getIdentifier(),
6109 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006110
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006111 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006112 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006113 DependentTemplateSpecializationTypeLoc SpecTL
6114 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006115 SpecTL.setLAngleLoc(LAngleLoc);
6116 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006117 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006118 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006119 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006120 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6121 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006122 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006123 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006124
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006125 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6126 if (T.isNull())
6127 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006128
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006129 // Provide source-location information for the template specialization
6130 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006131 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006132 TemplateSpecializationTypeLoc SpecTL
6133 = Builder.push<TemplateSpecializationTypeLoc>(T);
6134
6135 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006136 SpecTL.setLAngleLoc(LAngleLoc);
6137 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006138 SpecTL.setTemplateNameLoc(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());
6141
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006142 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6143 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6144 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006145 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6146
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006147 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6148 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006149}
6150
Douglas Gregora02411e2011-02-27 22:46:49 +00006151
Douglas Gregord57959a2009-03-27 23:10:48 +00006152/// \brief Build the type that describes a C++ typename specifier,
6153/// e.g., "typename T::type".
6154QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006155Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6156 SourceLocation KeywordLoc,
6157 NestedNameSpecifierLoc QualifierLoc,
6158 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006159 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006160 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006161 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006162
John McCall77bb1aa2010-05-01 00:40:08 +00006163 DeclContext *Ctx = computeDeclContext(SS);
6164 if (!Ctx) {
6165 // If the nested-name-specifier is dependent and couldn't be
6166 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006167 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6168 return Context.getDependentNameType(Keyword,
6169 QualifierLoc.getNestedNameSpecifier(),
6170 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006171 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006172
John McCall77bb1aa2010-05-01 00:40:08 +00006173 // If the nested-name-specifier refers to the current instantiation,
6174 // the "typename" keyword itself is superfluous. In C++03, the
6175 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6176 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006177 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006178
John McCall77bb1aa2010-05-01 00:40:08 +00006179 if (RequireCompleteDeclContext(SS, Ctx))
6180 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006181
6182 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006183 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006184 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006185 unsigned DiagID = 0;
6186 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006187 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006188 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006189 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006190 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006191
6192 case LookupResult::FoundUnresolvedValue: {
6193 // We found a using declaration that is a value. Most likely, the using
6194 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006195 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006196 IILoc);
6197 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6198 << Name << Ctx << FullRange;
6199 if (UnresolvedUsingValueDecl *Using
6200 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006201 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006202 Diag(Loc, diag::note_using_value_decl_missing_typename)
6203 << FixItHint::CreateInsertion(Loc, "typename ");
6204 }
6205 }
6206 // Fall through to create a dependent typename type, from which we can recover
6207 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006208
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006209 case LookupResult::NotFoundInCurrentInstantiation:
6210 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006211 return Context.getDependentNameType(Keyword,
6212 QualifierLoc.getNestedNameSpecifier(),
6213 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006214
6215 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006216 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006217 // We found a type. Build an ElaboratedType, since the
6218 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006219 return Context.getElaboratedType(ETK_Typename,
6220 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006221 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006222 }
6223
6224 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006225 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006226 break;
6227
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006228
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006229 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006230 return QualType();
6231
Douglas Gregord57959a2009-03-27 23:10:48 +00006232 case LookupResult::FoundOverloaded:
6233 DiagID = diag::err_typename_nested_not_type;
6234 Referenced = *Result.begin();
6235 break;
6236
John McCall6e247262009-10-10 05:48:19 +00006237 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006238 return QualType();
6239 }
6240
6241 // If we get here, it's because name lookup did not find a
6242 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006243 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006244 IILoc);
6245 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006246 if (Referenced)
6247 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6248 << Name;
6249 return QualType();
6250}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006251
6252namespace {
6253 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006254 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006255 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006256 SourceLocation Loc;
6257 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006258
Douglas Gregor4a959d82009-08-06 16:20:37 +00006259 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006260 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006261
Mike Stump1eb44332009-09-09 15:08:12 +00006262 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006263 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006264 DeclarationName Entity)
6265 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006266 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006267
6268 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006269 /// transformed.
6270 ///
6271 /// For the purposes of type reconstruction, a type has already been
6272 /// transformed if it is NULL or if it is not dependent.
6273 bool AlreadyTransformed(QualType T) {
6274 return T.isNull() || !T->isDependentType();
6275 }
Mike Stump1eb44332009-09-09 15:08:12 +00006276
6277 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006278 /// rebuilt.
6279 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006280
Douglas Gregor4a959d82009-08-06 16:20:37 +00006281 /// \brief Returns the name of the entity whose type is being rebuilt.
6282 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006283
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006284 /// \brief Sets the "base" location and entity when that
6285 /// information is known based on another transformation.
6286 void setBase(SourceLocation Loc, DeclarationName Entity) {
6287 this->Loc = Loc;
6288 this->Entity = Entity;
6289 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006290 };
6291}
6292
Douglas Gregor4a959d82009-08-06 16:20:37 +00006293/// \brief Rebuilds a type within the context of the current instantiation.
6294///
Mike Stump1eb44332009-09-09 15:08:12 +00006295/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006296/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006297/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006298/// partial specialization thereof). This routine will rebuild that type now
6299/// that we have entered the declarator's scope, which may produce different
6300/// canonical types, e.g.,
6301///
6302/// \code
6303/// template<typename T>
6304/// struct X {
6305/// typedef T* pointer;
6306/// pointer data();
6307/// };
6308///
6309/// template<typename T>
6310/// typename X<T>::pointer X<T>::data() { ... }
6311/// \endcode
6312///
Douglas Gregor4714c122010-03-31 17:34:00 +00006313/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006314/// since we do not know that we can look into X<T> when we parsed the type.
6315/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006316/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006317/// as the canonical type of T*, allowing the return types of the out-of-line
6318/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006319TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6320 SourceLocation Loc,
6321 DeclarationName Name) {
6322 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006323 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006324
Douglas Gregor4a959d82009-08-06 16:20:37 +00006325 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6326 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006327}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006328
John McCall60d7b3a2010-08-24 06:29:42 +00006329ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006330 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6331 DeclarationName());
6332 return Rebuilder.TransformExpr(E);
6333}
6334
John McCall63b43852010-04-29 23:50:39 +00006335bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006336 if (SS.isInvalid())
6337 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006338
Douglas Gregor7e384942011-02-25 16:07:42 +00006339 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006340 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6341 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006342 NestedNameSpecifierLoc Rebuilt
6343 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6344 if (!Rebuilt)
6345 return true;
John McCall63b43852010-04-29 23:50:39 +00006346
Douglas Gregor7e384942011-02-25 16:07:42 +00006347 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006348 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006349}
6350
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006351/// \brief Produces a formatted string that describes the binding of
6352/// template parameters to template arguments.
6353std::string
6354Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6355 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006356 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006357}
6358
6359std::string
6360Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6361 const TemplateArgument *Args,
6362 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006363 llvm::SmallString<128> Str;
6364 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006365
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006366 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006367 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006368
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006369 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006370 if (I >= NumArgs)
6371 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006372
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006373 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006374 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006375 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006376 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006377
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006378 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006379 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006380 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006381 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006382 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006383
Douglas Gregor87dd6972010-12-20 16:52:59 +00006384 Out << " = ";
6385 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006386 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006387
6388 Out << ']';
6389 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006390}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006391
6392void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6393 if (!FD)
6394 return;
6395 FD->setLateTemplateParsed(Flag);
6396}
6397
6398bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6399 DeclContext *DC = CurContext;
6400
6401 while (DC) {
6402 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6403 const FunctionDecl *FD = RD->isLocalClass();
6404 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6405 } else if (DC->isTranslationUnit() || DC->isNamespace())
6406 return false;
6407
6408 DC = DC->getParent();
6409 }
6410 return false;
6411}