blob: 9b0d2611ceefe8ca6043e1901af02f54e7088b1c [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
John McCallf7a1a742009-11-24 19:00:30 +000079static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
John McCallad00b772010-06-16 08:42:20 +000085 NamedDecl *Repl = isAcceptableTemplateName(C, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
95 // is followed by a template-argument-list, the reference refers to the
96 // class template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
98 //
99 // FIXME: Will we eventually have to do the same for alias templates?
100 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
101 if (!ClassTemplates.insert(ClassTmpl)) {
102 filter.erase();
103 continue;
104 }
John McCall8ba66912010-08-13 07:02:08 +0000105
106 // FIXME: we promote access to public here as a workaround to
107 // the fact that LookupResult doesn't let us remember that we
108 // found this template through a particular injected class name,
109 // which means we end up doing nasty things to the invariants.
110 // Pretending that access is public is *much* safer.
111 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000112 }
John McCallf7a1a742009-11-24 19:00:30 +0000113 }
114 filter.done();
115}
116
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000117TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000118 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000119 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000120 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000121 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000122 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000123 TemplateTy &TemplateResult,
124 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000125 assert(getLangOptions().CPlusPlus && "No template names in C!");
126
Douglas Gregor014e88d2009-11-03 23:16:33 +0000127 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000128 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000129
Douglas Gregor014e88d2009-11-03 23:16:33 +0000130 switch (Name.getKind()) {
131 case UnqualifiedId::IK_Identifier:
132 TName = DeclarationName(Name.Identifier);
133 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 case UnqualifiedId::IK_OperatorFunctionId:
136 TName = Context.DeclarationNames.getCXXOperatorName(
137 Name.OperatorFunctionId.Operator);
138 break;
139
Sean Hunte6252d12009-11-28 08:58:14 +0000140 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000141 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
142 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000143
Douglas Gregor014e88d2009-11-03 23:16:33 +0000144 default:
145 return TNK_Non_template;
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
John McCallb3d87482010-08-24 05:47:05 +0000148 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000150 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000151 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000152 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
153 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000154 if (R.empty()) return TNK_Non_template;
155 if (R.isAmbiguous()) {
156 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000157 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000158
159 // FIXME: we might have ambiguous templates, in which case we
160 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000161 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000162 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000163
John McCall0bd6feb2009-12-02 08:04:21 +0000164 TemplateName Template;
165 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
John McCall0bd6feb2009-12-02 08:04:21 +0000167 unsigned ResultCount = R.end() - R.begin();
168 if (ResultCount > 1) {
169 // We assume that we'll preserve the qualifier from a function
170 // template name in other ways.
171 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
172 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000173
174 // We'll do this lookup again later.
175 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000177 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
178
179 if (SS.isSet() && !SS.isInvalid()) {
180 NestedNameSpecifier *Qualifier
181 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000182 Template = Context.getQualifiedTemplateName(Qualifier,
183 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000184 } else {
185 Template = TemplateName(TD);
186 }
187
John McCallb8592062010-08-13 02:23:42 +0000188 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000189 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000190
191 // We'll do this lookup again later.
192 R.suppressDiagnostics();
193 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000194 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
195 TemplateKind = TNK_Type_template;
196 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
John McCall0bd6feb2009-12-02 08:04:21 +0000199 TemplateResult = TemplateTy::make(Template);
200 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000201}
202
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000203bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000204 SourceLocation IILoc,
205 Scope *S,
206 const CXXScopeSpec *SS,
207 TemplateTy &SuggestedTemplate,
208 TemplateNameKind &SuggestedKind) {
209 // We can't recover unless there's a dependent scope specifier preceding the
210 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000211 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000212 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
213 computeDeclContext(*SS))
214 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000215
Douglas Gregor84d0a192010-01-12 21:28:44 +0000216 // The code is missing a 'template' keyword prior to the dependent template
217 // name.
218 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
219 Diag(IILoc, diag::err_template_kw_missing)
220 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000221 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
224 SuggestedKind = TNK_Dependent_template_name;
225 return true;
226}
227
John McCallf7a1a742009-11-24 19:00:30 +0000228void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000229 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000230 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000231 bool EnteringContext,
232 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000233 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000234 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000235 DeclContext *LookupCtx = 0;
236 bool isDependent = false;
237 if (!ObjectType.isNull()) {
238 // This nested-name-specifier occurs in a member access expression, e.g.,
239 // x->B::f, and we are looking into the type of the object.
240 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
241 LookupCtx = computeDeclContext(ObjectType);
242 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000243 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000244 "Caller should have completed object type");
245 } else if (SS.isSet()) {
246 // This nested-name-specifier occurs after another nested-name-specifier,
247 // so long into the context associated with the prior nested-name-specifier.
248 LookupCtx = computeDeclContext(SS, EnteringContext);
249 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250
John McCallf7a1a742009-11-24 19:00:30 +0000251 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000252 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000253 return;
254 }
255
256 bool ObjectTypeSearchedInScope = false;
257 if (LookupCtx) {
258 // Perform "qualified" name lookup into the declaration context we
259 // computed, which is either the type of the base of a member access
260 // expression or the declaration context associated with a prior
261 // nested-name-specifier.
262 LookupQualifiedName(Found, LookupCtx);
263
264 if (!ObjectType.isNull() && Found.empty()) {
265 // C++ [basic.lookup.classref]p1:
266 // In a class member access expression (5.2.5), if the . or -> token is
267 // immediately followed by an identifier followed by a <, the
268 // identifier must be looked up to determine whether the < is the
269 // beginning of a template argument list (14.2) or a less-than operator.
270 // The identifier is first looked up in the class of the object
271 // expression. If the identifier is not found, it is then looked up in
272 // the context of the entire postfix-expression and shall name a class
273 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000274 if (S) LookupName(Found, S);
275 ObjectTypeSearchedInScope = true;
276 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000277 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000278 // We cannot look into a dependent object type or nested nme
279 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000280 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000281 return;
282 } else {
283 // Perform unqualified name lookup in the current scope.
284 LookupName(Found, S);
285 }
286
Douglas Gregor2e933882010-01-12 17:06:20 +0000287 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000288 // If we did not find any names, attempt to correct any typos.
289 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000290 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000291 false, CTC_CXXCasts)) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000292 FilterAcceptableTemplateNames(Context, Found);
John McCallad00b772010-06-16 08:42:20 +0000293 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000294 if (LookupCtx)
295 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
296 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000297 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000298 Found.getLookupName().getAsString());
299 else
300 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
301 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000302 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000303 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000304 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
305 Diag(Template->getLocation(), diag::note_previous_decl)
306 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000307 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000308 } else {
309 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000310 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000311 }
312 }
313
John McCallf7a1a742009-11-24 19:00:30 +0000314 FilterAcceptableTemplateNames(Context, Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000315 if (Found.empty()) {
316 if (isDependent)
317 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000318 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000319 }
John McCallf7a1a742009-11-24 19:00:30 +0000320
321 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
322 // C++ [basic.lookup.classref]p1:
323 // [...] If the lookup in the class of the object expression finds a
324 // template, the name is also looked up in the context of the entire
325 // postfix-expression and [...]
326 //
327 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
328 LookupOrdinaryName);
329 LookupName(FoundOuter, S);
330 FilterAcceptableTemplateNames(Context, FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000331
John McCallf7a1a742009-11-24 19:00:30 +0000332 if (FoundOuter.empty()) {
333 // - if the name is not found, the name found in the class of the
334 // object expression is used, otherwise
335 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
336 // - if the name is found in the context of the entire
337 // postfix-expression and does not name a class template, the name
338 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000339 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000340 // - if the name found is a class template, it must refer to the same
341 // entity as the one found in the class of the object expression,
342 // otherwise the program is ill-formed.
343 if (!Found.isSingleResult() ||
344 Found.getFoundDecl()->getCanonicalDecl()
345 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000346 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000347 diag::ext_nested_name_member_ref_lookup_ambiguous)
348 << Found.getLookupName()
349 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000350 Diag(Found.getRepresentativeDecl()->getLocation(),
351 diag::note_ambig_member_ref_object_type)
352 << ObjectType;
353 Diag(FoundOuter.getFoundDecl()->getLocation(),
354 diag::note_ambig_member_ref_scope);
355
356 // Recover by taking the template that we found in the object
357 // expression's type.
358 }
359 }
360 }
361}
362
John McCall2f841ba2009-12-02 03:53:29 +0000363/// ActOnDependentIdExpression - Handle a dependent id-expression that
364/// was just parsed. This is only possible with an explicit scope
365/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000366ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000367Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000369 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000371 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000372
John McCall2f841ba2009-12-02 03:53:29 +0000373 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000374 isa<CXXMethodDecl>(DC) &&
375 cast<CXXMethodDecl>(DC)->isInstance()) {
376 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000377
John McCallf7a1a742009-11-24 19:00:30 +0000378 // Since the 'this' expression is synthesized, we don't need to
379 // perform the double-lookup check.
380 NamedDecl *FirstQualifierInScope = 0;
381
John McCallaa81e162009-12-01 22:10:20 +0000382 return Owned(CXXDependentScopeMemberExpr::Create(Context,
383 /*This*/ 0, ThisType,
384 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000385 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000386 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000387 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000388 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000389 TemplateArgs));
390 }
391
Abramo Bagnara25777432010-08-11 22:01:17 +0000392 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000393}
394
John McCall60d7b3a2010-08-24 06:29:42 +0000395ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000396Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000397 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000398 const TemplateArgumentListInfo *TemplateArgs) {
399 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000400 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000401 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000402 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000403}
404
Douglas Gregor72c3f312008-12-05 18:15:24 +0000405/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
406/// that the template parameter 'PrevDecl' is being shadowed by a new
407/// declaration at location Loc. Returns true to indicate that this is
408/// an error, and false otherwise.
409bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000410 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000411
412 // Microsoft Visual C++ permits template parameters to be shadowed.
413 if (getLangOptions().Microsoft)
414 return false;
415
416 // C++ [temp.local]p4:
417 // A template-parameter shall not be redeclared within its
418 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000419 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000420 << cast<NamedDecl>(PrevDecl)->getDeclName();
421 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
422 return true;
423}
424
Douglas Gregor2943aed2009-03-03 04:44:36 +0000425/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000426/// the parameter D to reference the templated declaration and return a pointer
427/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000428TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
429 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
430 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000431 return Temp;
432 }
433 return 0;
434}
435
Douglas Gregorba68eca2011-01-05 17:40:24 +0000436ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
437 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000438 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000439 "Only template template arguments can be pack expansions here");
440 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
441 "Template template argument pack expansion without packs");
442 ParsedTemplateArgument Result(*this);
443 Result.EllipsisLoc = EllipsisLoc;
444 return Result;
445}
446
Douglas Gregor788cd062009-11-11 01:00:40 +0000447static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
448 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000449
Douglas Gregor788cd062009-11-11 01:00:40 +0000450 switch (Arg.getKind()) {
451 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000452 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000453 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000454 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000455 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000456 return TemplateArgumentLoc(TemplateArgument(T), DI);
457 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000458
Douglas Gregor788cd062009-11-11 01:00:40 +0000459 case ParsedTemplateArgument::NonType: {
460 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
461 return TemplateArgumentLoc(TemplateArgument(E), E);
462 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000465 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000466 TemplateArgument TArg;
467 if (Arg.getEllipsisLoc().isValid())
468 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
469 else
470 TArg = Template;
471 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000472 Arg.getScopeSpec().getWithLocInContext(
473 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000474 Arg.getLocation(),
475 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000476 }
477 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000478
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000479 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000480 return TemplateArgumentLoc();
481}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000482
Douglas Gregor788cd062009-11-11 01:00:40 +0000483/// \brief Translates template arguments as provided by the parser
484/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000485void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
486 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000487 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000488 TemplateArgs.addArgument(translateTemplateArgument(*this,
489 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000490}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000491
Douglas Gregor72c3f312008-12-05 18:15:24 +0000492/// ActOnTypeParameter - Called when a C++ template type parameter
493/// (e.g., "typename T") has been parsed. Typename specifies whether
494/// the keyword "typename" was used to declare the type parameter
495/// (otherwise, "class" was used), and KeyLoc is the location of the
496/// "class" or "typename" keyword. ParamName is the name of the
497/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000498/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000499/// If the type parameter has a default argument, it will be added
500/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000501Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
502 SourceLocation EllipsisLoc,
503 SourceLocation KeyLoc,
504 IdentifierInfo *ParamName,
505 SourceLocation ParamNameLoc,
506 unsigned Depth, unsigned Position,
507 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000508 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000509 assert(S->isTemplateParamScope() &&
510 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000511 bool Invalid = false;
512
513 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000514 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000515 LookupOrdinaryName,
516 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000517 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000518 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000519 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000520 }
521
Douglas Gregorddc29e12009-02-06 22:42:48 +0000522 SourceLocation Loc = ParamNameLoc;
523 if (!ParamName)
524 Loc = KeyLoc;
525
Douglas Gregor72c3f312008-12-05 18:15:24 +0000526 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000527 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
528 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000529 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000530 if (Invalid)
531 Param->setInvalidDecl();
532
533 if (ParamName) {
534 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000535 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000536 IdResolver.AddDecl(Param);
537 }
538
Douglas Gregor61c4d282011-01-05 15:48:55 +0000539 // C++0x [temp.param]p9:
540 // A default template-argument may be specified for any kind of
541 // template-parameter that is not a template parameter pack.
542 if (DefaultArg && Ellipsis) {
543 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
544 DefaultArg = ParsedType();
545 }
546
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000547 // Handle the default argument, if provided.
548 if (DefaultArg) {
549 TypeSourceInfo *DefaultTInfo;
550 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000551
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000552 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000553
Douglas Gregor6f526752010-12-16 08:48:57 +0000554 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000555 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000556 UPPC_DefaultArgument))
557 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000558
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000559 // Check the template argument itself.
560 if (CheckTemplateArgument(Param, DefaultTInfo)) {
561 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000562 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000563 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000564
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000565 Param->setDefaultArgument(DefaultTInfo, false);
566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567
John McCalld226f652010-08-21 09:40:31 +0000568 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000569}
570
Douglas Gregor2943aed2009-03-03 04:44:36 +0000571/// \brief Check that the type of a non-type template parameter is
572/// well-formed.
573///
574/// \returns the (possibly-promoted) parameter type if valid;
575/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000576QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000577Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000578 // We don't allow variably-modified types as the type of non-type template
579 // parameters.
580 if (T->isVariablyModifiedType()) {
581 Diag(Loc, diag::err_variably_modified_nontype_template_param)
582 << T;
583 return QualType();
584 }
585
Douglas Gregor2943aed2009-03-03 04:44:36 +0000586 // C++ [temp.param]p4:
587 //
588 // A non-type template-parameter shall have one of the following
589 // (optionally cv-qualified) types:
590 //
591 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000592 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000593 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000594 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000595 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000596 T->isReferenceType() ||
597 // -- pointer to member.
598 T->isMemberPointerType() ||
599 // If T is a dependent type, we can't do the check now, so we
600 // assume that it is well-formed.
601 T->isDependentType())
602 return T;
603 // C++ [temp.param]p8:
604 //
605 // A non-type template-parameter of type "array of T" or
606 // "function returning T" is adjusted to be of type "pointer to
607 // T" or "pointer to function returning T", respectively.
608 else if (T->isArrayType())
609 // FIXME: Keep the type prior to promotion?
610 return Context.getArrayDecayedType(T);
611 else if (T->isFunctionType())
612 // FIXME: Keep the type prior to promotion?
613 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000614
Douglas Gregor2943aed2009-03-03 04:44:36 +0000615 Diag(Loc, diag::err_template_nontype_parm_bad_type)
616 << T;
617
618 return QualType();
619}
620
John McCalld226f652010-08-21 09:40:31 +0000621Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
622 unsigned Depth,
623 unsigned Position,
624 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000625 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000626 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
627 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000628
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000629 assert(S->isTemplateParamScope() &&
630 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000631 bool Invalid = false;
632
633 IdentifierInfo *ParamName = D.getIdentifier();
634 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000635 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000636 LookupOrdinaryName,
637 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000638 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000639 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000640 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000641 }
642
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000643 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
644 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000645 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000646 Invalid = true;
647 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000648
Douglas Gregor10738d32010-12-23 23:51:58 +0000649 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000650 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000651 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
652 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000653 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000654 IsParameterPack, TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000655 if (Invalid)
656 Param->setInvalidDecl();
657
658 if (D.getIdentifier()) {
659 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000660 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000661 IdResolver.AddDecl(Param);
662 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000663
Douglas Gregor61c4d282011-01-05 15:48:55 +0000664 // C++0x [temp.param]p9:
665 // A default template-argument may be specified for any kind of
666 // template-parameter that is not a template parameter pack.
667 if (Default && IsParameterPack) {
668 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
669 Default = 0;
670 }
671
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000672 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000673 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000674 // Check for unexpanded parameter packs.
675 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
676 return Param;
677
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000678 TemplateArgument Converted;
679 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
680 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000681 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683
John McCall9ae2f072010-08-23 23:25:46 +0000684 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000686
John McCalld226f652010-08-21 09:40:31 +0000687 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000688}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000689
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000690/// ActOnTemplateTemplateParameter - Called when a C++ template template
691/// parameter (e.g. T in template <template <typename> class T> class array)
692/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000693Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
694 SourceLocation TmpLoc,
695 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000696 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000697 IdentifierInfo *Name,
698 SourceLocation NameLoc,
699 unsigned Depth,
700 unsigned Position,
701 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000702 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000703 assert(S->isTemplateParamScope() &&
704 "Template template parameter not in template parameter scope!");
705
706 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000707 bool IsParameterPack = EllipsisLoc.isValid();
708 // FIXME: Pack-ness is dropped
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000709 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000710 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000711 NameLoc.isInvalid()? TmpLoc : NameLoc,
712 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000713 Name, Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000714
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000715 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000716 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000717 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000718 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000719 IdResolver.AddDecl(Param);
720 }
721
Douglas Gregor6f526752010-12-16 08:48:57 +0000722 if (Params->size() == 0) {
723 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
724 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
725 Param->setInvalidDecl();
726 }
727
Douglas Gregor61c4d282011-01-05 15:48:55 +0000728 // C++0x [temp.param]p9:
729 // A default template-argument may be specified for any kind of
730 // template-parameter that is not a template parameter pack.
731 if (IsParameterPack && !Default.isInvalid()) {
732 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
733 Default = ParsedTemplateArgument();
734 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000735
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000736 if (!Default.isInvalid()) {
737 // Check only that we have a template template argument. We don't want to
738 // try to check well-formedness now, because our template template parameter
739 // might have dependent types in its template parameters, which we wouldn't
740 // be able to match now.
741 //
742 // If none of the template template parameter's template arguments mention
743 // other template parameters, we could actually perform more checking here.
744 // However, it isn't worth doing.
745 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
746 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
747 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
748 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000749 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000750 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000751
Douglas Gregor6f526752010-12-16 08:48:57 +0000752 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000753 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000754 DefaultArg.getArgument().getAsTemplate(),
755 UPPC_DefaultArgument))
756 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000758 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000759 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000760
John McCalld226f652010-08-21 09:40:31 +0000761 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000762}
763
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000764/// ActOnTemplateParameterList - Builds a TemplateParameterList that
765/// contains the template parameters in Params/NumParams.
766Sema::TemplateParamsTy *
767Sema::ActOnTemplateParameterList(unsigned Depth,
768 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000769 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000770 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000771 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000772 SourceLocation RAngleLoc) {
773 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000774 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000775
Douglas Gregorddc29e12009-02-06 22:42:48 +0000776 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000777 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000778 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000779}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000780
John McCallb6217662010-03-15 10:12:16 +0000781static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
782 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000783 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000784}
785
John McCallf312b1e2010-08-26 23:41:50 +0000786DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000787Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000788 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000789 IdentifierInfo *Name, SourceLocation NameLoc,
790 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000791 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000792 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000793 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000794 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000795 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000796 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000797
798 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000799 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000800 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000801
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000802 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
803 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000804
805 // There is no such thing as an unnamed class template.
806 if (!Name) {
807 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000808 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000809 }
810
811 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000812 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000813 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000814 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000815 if (SS.isNotEmpty() && !SS.isInvalid()) {
816 SemanticContext = computeDeclContext(SS, true);
817 if (!SemanticContext) {
818 // FIXME: Produce a reasonable diagnostic here
819 return true;
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
John McCall77bb1aa2010-05-01 00:40:08 +0000822 if (RequireCompleteDeclContext(SS, SemanticContext))
823 return true;
824
John McCalla24dc2e2009-11-17 02:14:36 +0000825 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000826 } else {
827 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000828 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Douglas Gregor57265e32010-04-12 16:00:01 +0000831 if (Previous.isAmbiguous())
832 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000833
Douglas Gregorddc29e12009-02-06 22:42:48 +0000834 NamedDecl *PrevDecl = 0;
835 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000836 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000837
Douglas Gregorddc29e12009-02-06 22:42:48 +0000838 // If there is a previous declaration with the same name, check
839 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000840 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000842
843 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000844 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000845 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000846 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000847 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
848 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000849 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000850 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
851 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
852 PrevClassTemplate
853 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
854 ->getSpecializedTemplate();
855 }
856 }
857
John McCall65c49462009-12-18 11:25:59 +0000858 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000859 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000860 // [...] When looking for a prior declaration of a class or a function
861 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000862 // function is neither a qualified name nor a template-id, scopes outside
863 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000864 if (!SS.isSet()) {
865 DeclContext *OutermostContext = CurContext;
866 while (!OutermostContext->isFileContext())
867 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000868
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000869 if (PrevDecl &&
870 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
871 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
872 SemanticContext = PrevDecl->getDeclContext();
873 } else {
874 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000875 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000876 // declaration.
877 PrevDecl = PrevClassTemplate = 0;
878 SemanticContext = OutermostContext;
879 }
John McCalle129d442009-12-17 23:21:11 +0000880 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000881
John McCalle129d442009-12-17 23:21:11 +0000882 if (CurContext->isDependentContext()) {
883 // If this is a dependent context, we don't want to link the friend
884 // class template to the template in scope, because that would perform
885 // checking of the template parameter lists that can't be performed
886 // until the outer context is instantiated.
887 PrevDecl = PrevClassTemplate = 0;
888 }
889 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
890 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000891
Douglas Gregorddc29e12009-02-06 22:42:48 +0000892 if (PrevClassTemplate) {
893 // Ensure that the template parameter lists are compatible.
894 if (!TemplateParameterListsAreEqual(TemplateParams,
895 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000896 /*Complain=*/true,
897 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000898 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000899
900 // C++ [temp.class]p4:
901 // In a redeclaration, partial specialization, explicit
902 // specialization or explicit instantiation of a class template,
903 // the class-key shall agree in kind with the original class
904 // template declaration (7.1.5.3).
905 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000906 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000907 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000908 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000909 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000910 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000911 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000912 }
913
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000915 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000916 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000917 Diag(NameLoc, diag::err_redefinition) << Name;
918 Diag(Def->getLocation(), diag::note_previous_definition);
919 // FIXME: Would it make sense to try to "forget" the previous
920 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000921 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000922 }
923 }
924 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
925 // Maybe we will complain about the shadowed template parameter.
926 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
927 // Just pretend that we didn't see the previous declaration.
928 PrevDecl = 0;
929 } else if (PrevDecl) {
930 // C++ [temp]p5:
931 // A class template shall not have the same name as any other
932 // template, class, function, object, enumeration, enumerator,
933 // namespace, or type in the same scope (3.3), except as specified
934 // in (14.5.4).
935 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
936 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000937 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000938 }
939
Douglas Gregord684b002009-02-10 19:49:53 +0000940 // Check the template parameter list of this declaration, possibly
941 // merging in the template parameter list from the previous class
942 // template declaration.
943 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000944 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000945 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000946 SemanticContext->isRecord() &&
947 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000948 ? TPC_ClassTemplateMember
949 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000950 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Douglas Gregor57265e32010-04-12 16:00:01 +0000952 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000953 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000954 // template out-of-line.
955 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
956 !(TUK == TUK_Friend && CurContext->isDependentContext()))
957 Diag(NameLoc, diag::err_member_def_does_not_match)
958 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000959 }
960
Mike Stump1eb44332009-09-09 15:08:12 +0000961 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000962 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000963 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000964 PrevClassTemplate->getTemplatedDecl() : 0,
965 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000966 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000967
968 ClassTemplateDecl *NewTemplate
969 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
970 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000971 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000972 NewClass->setDescribedClassTemplate(NewTemplate);
973
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000974 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000975 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000976 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000977 assert(T->isDependentType() && "Class template type is not dependent?");
978 (void)T;
979
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000980 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000981 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000982 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000983 PrevClassTemplate->getInstantiatedFromMemberTemplate())
984 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000985
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000986 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000987 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000988 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregorddc29e12009-02-06 22:42:48 +0000990 // Set the lexical context of these templates
991 NewClass->setLexicalDeclContext(CurContext);
992 NewTemplate->setLexicalDeclContext(CurContext);
993
John McCall0f434ec2009-07-31 02:45:11 +0000994 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000995 NewClass->startDefinition();
996
997 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000998 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000999
John McCall05b23ea2009-09-14 21:59:20 +00001000 if (TUK != TUK_Friend)
1001 PushOnScopeChains(NewTemplate, S);
1002 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001003 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001004 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001005 NewClass->setAccess(PrevClassTemplate->getAccess());
1006 }
John McCall05b23ea2009-09-14 21:59:20 +00001007
Douglas Gregord85bea22009-09-26 06:47:28 +00001008 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1009 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001010
John McCall05b23ea2009-09-14 21:59:20 +00001011 // Friend templates are visible in fairly strange ways.
1012 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001013 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001014 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1015 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1016 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001017 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001018 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019
Douglas Gregord85bea22009-09-26 06:47:28 +00001020 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1021 NewClass->getLocation(),
1022 NewTemplate,
1023 /*FIXME:*/NewClass->getLocation());
1024 Friend->setAccess(AS_public);
1025 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001026 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001027
Douglas Gregord684b002009-02-10 19:49:53 +00001028 if (Invalid) {
1029 NewTemplate->setInvalidDecl();
1030 NewClass->setInvalidDecl();
1031 }
John McCalld226f652010-08-21 09:40:31 +00001032 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001033}
1034
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001035/// \brief Diagnose the presence of a default template argument on a
1036/// template parameter, which is ill-formed in certain contexts.
1037///
1038/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001039static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001040 Sema::TemplateParamListContext TPC,
1041 SourceLocation ParamLoc,
1042 SourceRange DefArgRange) {
1043 switch (TPC) {
1044 case Sema::TPC_ClassTemplate:
1045 return false;
1046
1047 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001048 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001049 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001050 // A default template-argument shall not be specified in a
1051 // function template declaration or a function template
1052 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001053 // If a friend function template declaration specifies a default
1054 // template-argument, that declaration shall be a definition and shall be
1055 // the only declaration of the function template in the translation unit.
1056 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001057 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001058 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001059 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001060 << DefArgRange;
1061 return false;
1062
1063 case Sema::TPC_ClassTemplateMember:
1064 // C++0x [temp.param]p9:
1065 // A default template-argument shall not be specified in the
1066 // template-parameter-lists of the definition of a member of a
1067 // class template that appears outside of the member's class.
1068 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1069 << DefArgRange;
1070 return true;
1071
1072 case Sema::TPC_FriendFunctionTemplate:
1073 // C++ [temp.param]p9:
1074 // A default template-argument shall not be specified in a
1075 // friend template declaration.
1076 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1077 << DefArgRange;
1078 return true;
1079
1080 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1081 // for friend function templates if there is only a single
1082 // declaration (and it is a definition). Strange!
1083 }
1084
1085 return false;
1086}
1087
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001088/// \brief Check for unexpanded parameter packs within the template parameters
1089/// of a template template parameter, recursively.
1090bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1091 TemplateParameterList *Params = TTP->getTemplateParameters();
1092 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1093 NamedDecl *P = Params->getParam(I);
1094 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001095 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001096 NTTP->getTypeSourceInfo(),
1097 Sema::UPPC_NonTypeTemplateParameterType))
1098 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001099
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001100 continue;
1101 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001102
1103 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001104 = dyn_cast<TemplateTemplateParmDecl>(P))
1105 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1106 return true;
1107 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001108
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001109 return false;
1110}
1111
Douglas Gregord684b002009-02-10 19:49:53 +00001112/// \brief Checks the validity of a template parameter list, possibly
1113/// considering the template parameter list from a previous
1114/// declaration.
1115///
1116/// If an "old" template parameter list is provided, it must be
1117/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1118/// template parameter list.
1119///
1120/// \param NewParams Template parameter list for a new template
1121/// declaration. This template parameter list will be updated with any
1122/// default arguments that are carried through from the previous
1123/// template parameter list.
1124///
1125/// \param OldParams If provided, template parameter list from a
1126/// previous declaration of the same template. Default template
1127/// arguments will be merged from the old template parameter list to
1128/// the new template parameter list.
1129///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001130/// \param TPC Describes the context in which we are checking the given
1131/// template parameter list.
1132///
Douglas Gregord684b002009-02-10 19:49:53 +00001133/// \returns true if an error occurred, false otherwise.
1134bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001135 TemplateParameterList *OldParams,
1136 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001137 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Douglas Gregord684b002009-02-10 19:49:53 +00001139 // C++ [temp.param]p10:
1140 // The set of default template-arguments available for use with a
1141 // template declaration or definition is obtained by merging the
1142 // default arguments from the definition (if in scope) and all
1143 // declarations in scope in the same way default function
1144 // arguments are (8.3.6).
1145 bool SawDefaultArgument = false;
1146 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001147
Anders Carlsson49d25572009-06-12 23:20:15 +00001148 bool SawParameterPack = false;
1149 SourceLocation ParameterPackLoc;
1150
Mike Stump1a35fde2009-02-11 23:03:27 +00001151 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001152 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001153 if (OldParams)
1154 OldParam = OldParams->begin();
1155
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001156 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001157 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1158 NewParamEnd = NewParams->end();
1159 NewParam != NewParamEnd; ++NewParam) {
1160 // Variables used to diagnose redundant default arguments
1161 bool RedundantDefaultArg = false;
1162 SourceLocation OldDefaultLoc;
1163 SourceLocation NewDefaultLoc;
1164
1165 // Variables used to diagnose missing default arguments
1166 bool MissingDefaultArg = false;
1167
Anders Carlsson49d25572009-06-12 23:20:15 +00001168 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001169 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001170 // parameter pack, it shall be the last template parameter.
1171 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001172 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001173 diag::err_template_param_pack_must_be_last_template_parameter);
1174 Invalid = true;
1175 }
1176
Douglas Gregord684b002009-02-10 19:49:53 +00001177 if (TemplateTypeParmDecl *NewTypeParm
1178 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001179 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001180 if (NewTypeParm->hasDefaultArgument() &&
1181 DiagnoseDefaultTemplateArgument(*this, TPC,
1182 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001183 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001184 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001185 NewTypeParm->removeDefaultArgument();
1186
1187 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001188 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001189 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Anders Carlsson49d25572009-06-12 23:20:15 +00001191 if (NewTypeParm->isParameterPack()) {
1192 assert(!NewTypeParm->hasDefaultArgument() &&
1193 "Parameter packs can't have a default argument!");
1194 SawParameterPack = true;
1195 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001196 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001197 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001198 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1199 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1200 SawDefaultArgument = true;
1201 RedundantDefaultArg = true;
1202 PreviousDefaultArgLoc = NewDefaultLoc;
1203 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1204 // Merge the default argument from the old declaration to the
1205 // new declaration.
1206 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001207 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001208 true);
1209 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1210 } else if (NewTypeParm->hasDefaultArgument()) {
1211 SawDefaultArgument = true;
1212 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1213 } else if (SawDefaultArgument)
1214 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001215 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001216 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001217 // Check for unexpanded parameter packs.
1218 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001219 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001220 UPPC_NonTypeTemplateParameterType)) {
1221 Invalid = true;
1222 continue;
1223 }
1224
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001225 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001226 if (NewNonTypeParm->hasDefaultArgument() &&
1227 DiagnoseDefaultTemplateArgument(*this, TPC,
1228 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001229 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001230 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001231 }
1232
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001233 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001234 NonTypeTemplateParmDecl *OldNonTypeParm
1235 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001236 if (NewNonTypeParm->isParameterPack()) {
1237 assert(!NewNonTypeParm->hasDefaultArgument() &&
1238 "Parameter packs can't have a default argument!");
1239 SawParameterPack = true;
1240 ParameterPackLoc = NewNonTypeParm->getLocation();
1241 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001242 NewNonTypeParm->hasDefaultArgument()) {
1243 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1244 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1245 SawDefaultArgument = true;
1246 RedundantDefaultArg = true;
1247 PreviousDefaultArgLoc = NewDefaultLoc;
1248 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1249 // Merge the default argument from the old declaration to the
1250 // new declaration.
1251 SawDefaultArgument = true;
1252 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001253 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001254 // parameter.
1255 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001256 OldNonTypeParm->getDefaultArgument(),
1257 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001258 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1259 } else if (NewNonTypeParm->hasDefaultArgument()) {
1260 SawDefaultArgument = true;
1261 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1262 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001263 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001264 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001265 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001266 TemplateTemplateParmDecl *NewTemplateParm
1267 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001268
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001269 // Check for unexpanded parameter packs, recursively.
1270 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1271 Invalid = true;
1272 continue;
1273 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001274
1275 if (NewTemplateParm->hasDefaultArgument() &&
1276 DiagnoseDefaultTemplateArgument(*this, TPC,
1277 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001278 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001279 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001280
1281 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001282 TemplateTemplateParmDecl *OldTemplateParm
1283 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001284 if (NewTemplateParm->isParameterPack()) {
1285 assert(!NewTemplateParm->hasDefaultArgument() &&
1286 "Parameter packs can't have a default argument!");
1287 SawParameterPack = true;
1288 ParameterPackLoc = NewTemplateParm->getLocation();
1289 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001290 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001291 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1292 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001293 SawDefaultArgument = true;
1294 RedundantDefaultArg = true;
1295 PreviousDefaultArgLoc = NewDefaultLoc;
1296 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1297 // Merge the default argument from the old declaration to the
1298 // new declaration.
1299 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001300 // FIXME: We need to create a new kind of "default argument" expression
1301 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001302 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001303 OldTemplateParm->getDefaultArgument(),
1304 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001305 PreviousDefaultArgLoc
1306 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001307 } else if (NewTemplateParm->hasDefaultArgument()) {
1308 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001309 PreviousDefaultArgLoc
1310 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001311 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001312 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001313 }
1314
1315 if (RedundantDefaultArg) {
1316 // C++ [temp.param]p12:
1317 // A template-parameter shall not be given default arguments
1318 // by two different declarations in the same scope.
1319 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1320 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1321 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001322 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001323 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001324 // If a template-parameter of a class template has a default
1325 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001326 // have a default template-argument supplied or be a template parameter
1327 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001328 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001329 diag::err_template_param_default_arg_missing);
1330 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1331 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001332 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001333 }
1334
1335 // If we have an old template parameter list that we're merging
1336 // in, move on to the next parameter.
1337 if (OldParams)
1338 ++OldParam;
1339 }
1340
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001341 // We were missing some default arguments at the end of the list, so remove
1342 // all of the default arguments.
1343 if (RemoveDefaultArguments) {
1344 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1345 NewParamEnd = NewParams->end();
1346 NewParam != NewParamEnd; ++NewParam) {
1347 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1348 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001349 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001350 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1351 NTTP->removeDefaultArgument();
1352 else
1353 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1354 }
1355 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001356
Douglas Gregord684b002009-02-10 19:49:53 +00001357 return Invalid;
1358}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001359
John McCall4e2cbb22010-10-20 05:44:58 +00001360namespace {
1361
1362/// A class which looks for a use of a certain level of template
1363/// parameter.
1364struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1365 typedef RecursiveASTVisitor<DependencyChecker> super;
1366
1367 unsigned Depth;
1368 bool Match;
1369
1370 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1371 NamedDecl *ND = Params->getParam(0);
1372 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1373 Depth = PD->getDepth();
1374 } else if (NonTypeTemplateParmDecl *PD =
1375 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1376 Depth = PD->getDepth();
1377 } else {
1378 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1379 }
1380 }
1381
1382 bool Matches(unsigned ParmDepth) {
1383 if (ParmDepth >= Depth) {
1384 Match = true;
1385 return true;
1386 }
1387 return false;
1388 }
1389
1390 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1391 return !Matches(T->getDepth());
1392 }
1393
1394 bool TraverseTemplateName(TemplateName N) {
1395 if (TemplateTemplateParmDecl *PD =
1396 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1397 if (Matches(PD->getDepth())) return false;
1398 return super::TraverseTemplateName(N);
1399 }
1400
1401 bool VisitDeclRefExpr(DeclRefExpr *E) {
1402 if (NonTypeTemplateParmDecl *PD =
1403 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1404 if (PD->getDepth() == Depth) {
1405 Match = true;
1406 return false;
1407 }
1408 }
1409 return super::VisitDeclRefExpr(E);
1410 }
1411};
1412}
1413
1414/// Determines whether a template-id depends on the given parameter
1415/// list.
1416static bool
1417DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1418 TemplateParameterList *Params) {
1419 DependencyChecker Checker(Params);
1420 Checker.TraverseType(QualType(TemplateId, 0));
1421 return Checker.Match;
1422}
1423
Mike Stump1eb44332009-09-09 15:08:12 +00001424/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001425/// specifier, returning the template parameter list that applies to the
1426/// name.
1427///
1428/// \param DeclStartLoc the start of the declaration that has a scope
1429/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001430///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001431/// \param SS the scope specifier that will be matched to the given template
1432/// parameter lists. This scope specifier precedes a qualified name that is
1433/// being declared.
1434///
1435/// \param ParamLists the template parameter lists, from the outermost to the
1436/// innermost template parameter lists.
1437///
1438/// \param NumParamLists the number of template parameter lists in ParamLists.
1439///
John McCall77e8b112010-04-13 20:37:33 +00001440/// \param IsFriend Whether to apply the slightly different rules for
1441/// matching template parameters to scope specifiers in friend
1442/// declarations.
1443///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001444/// \param IsExplicitSpecialization will be set true if the entity being
1445/// declared is an explicit specialization, false otherwise.
1446///
Mike Stump1eb44332009-09-09 15:08:12 +00001447/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001448/// name that is preceded by the scope specifier @p SS. This template
1449/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001450/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001451/// template specialization), or may be NULL (if we were's declaring isn't
1452/// itself a template).
1453TemplateParameterList *
1454Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1455 const CXXScopeSpec &SS,
1456 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001457 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001458 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001459 bool &IsExplicitSpecialization,
1460 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001461 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001462
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001463 // Find the template-ids that occur within the nested-name-specifier. These
1464 // template-ids will match up with the template parameter lists.
1465 llvm::SmallVector<const TemplateSpecializationType *, 4>
1466 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001467 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1468 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001469 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1470 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001471 const Type *T = NNS->getAsType();
1472 if (!T) break;
1473
1474 // C++0x [temp.expl.spec]p17:
1475 // A member or a member template may be nested within many
1476 // enclosing class templates. In an explicit specialization for
1477 // such a member, the member declaration shall be preceded by a
1478 // template<> for each enclosing class template that is
1479 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001480 //
1481 // Following the existing practice of GNU and EDG, we allow a typedef of a
1482 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001483 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1484 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001485
Mike Stump1eb44332009-09-09 15:08:12 +00001486 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001487 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001488 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1489 if (!Template)
1490 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Ted Kremenek6217b802009-07-29 21:53:49 +00001492 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001493 ClassTemplateSpecializationDecl *SpecDecl
1494 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1495 // If the nested name specifier refers to an explicit specialization,
1496 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001497 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1498 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001499 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001500 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503 TemplateIdsInSpecifier.push_back(SpecType);
1504 }
1505 }
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001507 // Reverse the list of template-ids in the scope specifier, so that we can
1508 // more easily match up the template-ids and the template parameter lists.
1509 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001511 SourceLocation FirstTemplateLoc = DeclStartLoc;
1512 if (NumParamLists)
1513 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001515 // Match the template-ids found in the specifier to the template parameter
1516 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001517 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001518 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001519 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1520 const TemplateSpecializationType *TemplateId
1521 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001522 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001523
1524 // In friend declarations we can have template-ids which don't
1525 // depend on the corresponding template parameter lists. But
1526 // assume that empty parameter lists are supposed to match this
1527 // template-id.
1528 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1529 if (!DependentTemplateId ||
1530 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1531 continue;
1532 }
1533
1534 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001535 // We have a template-id without a corresponding template parameter
1536 // list.
John McCall77e8b112010-04-13 20:37:33 +00001537
1538 // ...which is fine if this is a friend declaration.
1539 if (IsFriend) {
1540 IsExplicitSpecialization = true;
1541 break;
1542 }
1543
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001544 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001545 // FIXME: the location information here isn't great.
1546 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001547 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001548 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001549 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001550 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001551 } else {
1552 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1553 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001554 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001555 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001556 }
1557 return 0;
1558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001560 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001561 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001562 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001563
John McCall31f17ec2010-04-27 00:57:59 +00001564 // Are there cases in (e.g.) friends where this won't match?
1565 if (const InjectedClassNameType *Injected
1566 = TemplateId->getAs<InjectedClassNameType>()) {
1567 CXXRecordDecl *Record = Injected->getDecl();
1568 if (ClassTemplatePartialSpecializationDecl *Partial =
1569 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1570 ExpectedTemplateParams = Partial->getTemplateParameters();
1571 else
1572 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1573 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001574 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001575
John McCall31f17ec2010-04-27 00:57:59 +00001576 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001577 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001578 ExpectedTemplateParams,
1579 true, TPL_TemplateMatch);
1580
John McCall4e2cbb22010-10-20 05:44:58 +00001581 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1582 TPC_ClassTemplateMember);
1583 } else if (ParamLists[ParamIdx]->size() > 0)
1584 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001585 diag::err_template_param_list_matches_nontemplate)
1586 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001587 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001588 else
1589 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001590
1591 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001594 // If there were at least as many template-ids as there were template
1595 // parameter lists, then there are no template parameter lists remaining for
1596 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001597 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001598 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001600 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001601 if (ParamIdx != NumParamLists - 1) {
1602 while (ParamIdx < NumParamLists - 1) {
1603 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1604 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001605 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1606 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001607 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1608 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001609
1610 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1611 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1612 diag::note_explicit_template_spec_does_not_need_header)
1613 << ExplicitSpecializationsInSpecifier.back();
1614 ExplicitSpecializationsInSpecifier.pop_back();
1615 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001616
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001617 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001618 // means that the resulting template declaration can't be instantiated
1619 // properly (we'll end up with dependent nodes when we shouldn't).
1620 if (!isExplicitSpecHeader)
1621 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001622
John McCall4e2cbb22010-10-20 05:44:58 +00001623 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001624 }
1625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001627 // Return the last template parameter list, which corresponds to the
1628 // entity being declared.
1629 return ParamLists[NumParamLists - 1];
1630}
1631
Douglas Gregor7532dc62009-03-30 22:58:21 +00001632QualType Sema::CheckTemplateIdType(TemplateName Name,
1633 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001634 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001635 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001636 if (!Template) {
1637 // The template name does not resolve to a template, so we just
1638 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001639 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001640 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001641
Douglas Gregor40808ce2009-03-09 23:48:35 +00001642 // Check that the template argument list is well-formed for this
1643 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001644 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001645 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001646 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001647 return QualType();
1648
Douglas Gregor910f8002010-11-07 23:05:16 +00001649 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001650 "Converted template argument list is too short!");
1651
1652 QualType CanonType;
1653
Douglas Gregorcaddba02009-11-12 18:38:13 +00001654 if (Name.isDependent() ||
1655 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001656 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001657 // This class template specialization is a dependent
1658 // type. Therefore, its canonical type is another class template
1659 // specialization type that contains all of the converted
1660 // arguments in canonical form. This ensures that, e.g., A<T> and
1661 // A<T, T> have identical types when A is declared as:
1662 //
1663 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001664 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001665 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001666 Converted.data(),
1667 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Douglas Gregor1275ae02009-07-28 23:00:59 +00001669 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001670 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001671 // In the future, we need to teach getTemplateSpecializationType to only
1672 // build the canonical type and return that to us.
1673 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001674
1675 // This might work out to be a current instantiation, in which
1676 // case the canonical type needs to be the InjectedClassNameType.
1677 //
1678 // TODO: in theory this could be a simple hashtable lookup; most
1679 // changes to CurContext don't change the set of current
1680 // instantiations.
1681 if (isa<ClassTemplateDecl>(Template)) {
1682 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1683 // If we get out to a namespace, we're done.
1684 if (Ctx->isFileContext()) break;
1685
1686 // If this isn't a record, keep looking.
1687 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1688 if (!Record) continue;
1689
1690 // Look for one of the two cases with InjectedClassNameTypes
1691 // and check whether it's the same template.
1692 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1693 !Record->getDescribedClassTemplate())
1694 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001695
John McCall31f17ec2010-04-27 00:57:59 +00001696 // Fetch the injected class name type and check whether its
1697 // injected type is equal to the type we just built.
1698 QualType ICNT = Context.getTypeDeclType(Record);
1699 QualType Injected = cast<InjectedClassNameType>(ICNT)
1700 ->getInjectedSpecializationType();
1701
1702 if (CanonType != Injected->getCanonicalTypeInternal())
1703 continue;
1704
1705 // If so, the canonical type of this TST is the injected
1706 // class name type of the record we just found.
1707 assert(ICNT.isCanonical());
1708 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001709 break;
1710 }
1711 }
Mike Stump1eb44332009-09-09 15:08:12 +00001712 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001713 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001714 // Find the class template specialization declaration that
1715 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001716 void *InsertPos = 0;
1717 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001718 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001719 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001720 if (!Decl) {
1721 // This is the first time we have referenced this class template
1722 // specialization. Create the canonical declaration and add it to
1723 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001724 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001725 ClassTemplate->getTemplatedDecl()->getTagKind(),
1726 ClassTemplate->getDeclContext(),
1727 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001728 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001729 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001730 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001731 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001732 Decl->setLexicalDeclContext(CurContext);
1733 }
1734
1735 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001736 assert(isa<RecordType>(CanonType) &&
1737 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001738 }
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Douglas Gregor40808ce2009-03-09 23:48:35 +00001740 // Build the fully-sugared type for this class template
1741 // specialization, which refers back to the class template
1742 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001743 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001744}
1745
John McCallf312b1e2010-08-26 23:41:50 +00001746TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001747Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1748 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001749 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001750 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001751 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001752 if (SS.isInvalid())
1753 return true;
1754
Douglas Gregor7532dc62009-03-30 22:58:21 +00001755 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001756
Douglas Gregor40808ce2009-03-09 23:48:35 +00001757 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001758 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001759 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001760
Douglas Gregora88f09f2011-02-28 17:23:35 +00001761 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1762 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1763 DTN->getQualifier(),
1764 DTN->getIdentifier(),
1765 TemplateArgs);
1766
1767 // Build type-source information.
1768 TypeLocBuilder TLB;
1769 DependentTemplateSpecializationTypeLoc SpecTL
1770 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001771 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001772 SpecTL.setNameLoc(TemplateLoc);
1773 SpecTL.setLAngleLoc(LAngleLoc);
1774 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001775 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001776 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1777 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1778 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1779 }
1780
John McCalld5532b62009-11-23 01:53:49 +00001781 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001782 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001783
1784 if (Result.isNull())
1785 return true;
1786
Douglas Gregor059101f2011-03-02 00:47:37 +00001787 // Build type-source information.
1788 TypeLocBuilder TLB;
1789 TemplateSpecializationTypeLoc SpecTL
1790 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1791 SpecTL.setTemplateNameLoc(TemplateLoc);
1792 SpecTL.setLAngleLoc(LAngleLoc);
1793 SpecTL.setRAngleLoc(RAngleLoc);
1794 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1795 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001796
Douglas Gregor059101f2011-03-02 00:47:37 +00001797 if (SS.isNotEmpty()) {
1798 // Create an elaborated-type-specifier containing the nested-name-specifier.
1799 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1800 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1801 ElabTL.setKeywordLoc(SourceLocation());
1802 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1803 }
1804
1805 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001806}
John McCallf1bbbb42009-09-04 01:14:41 +00001807
Douglas Gregor059101f2011-03-02 00:47:37 +00001808TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001809 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001810 SourceLocation TagLoc,
1811 CXXScopeSpec &SS,
1812 TemplateTy TemplateD,
1813 SourceLocation TemplateLoc,
1814 SourceLocation LAngleLoc,
1815 ASTTemplateArgsPtr TemplateArgsIn,
1816 SourceLocation RAngleLoc) {
1817 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1818
1819 // Translate the parser's template argument list in our AST format.
1820 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1821 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1822
1823 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001824 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001825 ElaboratedTypeKeyword Keyword
1826 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Douglas Gregor059101f2011-03-02 00:47:37 +00001828 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1829 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1830 DTN->getQualifier(),
1831 DTN->getIdentifier(),
1832 TemplateArgs);
1833
1834 // Build type-source information.
1835 TypeLocBuilder TLB;
1836 DependentTemplateSpecializationTypeLoc SpecTL
1837 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1838 SpecTL.setKeywordLoc(TagLoc);
1839 SpecTL.setNameLoc(TemplateLoc);
1840 SpecTL.setLAngleLoc(LAngleLoc);
1841 SpecTL.setRAngleLoc(RAngleLoc);
1842 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1843 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1844 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1845 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1846 }
1847
1848 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1849 if (Result.isNull())
1850 return TypeResult();
1851
1852 // Check the tag kind
1853 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001854 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001855
John McCall6b2becf2009-09-08 17:47:29 +00001856 IdentifierInfo *Id = D->getIdentifier();
1857 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001858
John McCall6b2becf2009-09-08 17:47:29 +00001859 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1860 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001861 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001862 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001863 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001864 }
1865 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001866
1867 // Provide source-location information for the template specialization.
1868 TypeLocBuilder TLB;
1869 TemplateSpecializationTypeLoc SpecTL
1870 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1871 SpecTL.setTemplateNameLoc(TemplateLoc);
1872 SpecTL.setLAngleLoc(LAngleLoc);
1873 SpecTL.setRAngleLoc(RAngleLoc);
1874 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1875 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001876
Douglas Gregor059101f2011-03-02 00:47:37 +00001877 // Construct an elaborated type containing the nested-name-specifier (if any)
1878 // and keyword.
1879 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1880 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1881 ElabTL.setKeywordLoc(TagLoc);
1882 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1883 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001884}
1885
John McCall60d7b3a2010-08-24 06:29:42 +00001886ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001887 LookupResult &R,
1888 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001889 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001890 // FIXME: Can we do any checking at this point? I guess we could check the
1891 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001892 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001893 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001894 // foo<int> could identify a single function unambiguously
1895 // This approach does NOT work, since f<int>(1);
1896 // gets resolved prior to resorting to overload resolution
1897 // i.e., template<class T> void f(double);
1898 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001899
1900 // These should be filtered out by our callers.
1901 assert(!R.empty() && "empty lookup results when building templateid");
1902 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1903
John McCallc373d482010-01-27 01:50:18 +00001904 // We don't want lookup warnings at this point.
1905 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001906
John McCallf7a1a742009-11-24 19:00:30 +00001907 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001908 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001909 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001910 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001911 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001912 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001913
1914 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001915}
1916
John McCallf7a1a742009-11-24 19:00:30 +00001917// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001918ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001919Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001920 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001921 const TemplateArgumentListInfo &TemplateArgs) {
1922 DeclContext *DC;
1923 if (!(DC = computeDeclContext(SS, false)) ||
1924 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001925 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001926 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001928 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001929 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001930 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1931 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
John McCallf7a1a742009-11-24 19:00:30 +00001933 if (R.isAmbiguous())
1934 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001935
John McCallf7a1a742009-11-24 19:00:30 +00001936 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001937 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1938 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001939 return ExprError();
1940 }
1941
1942 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001943 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1944 << (NestedNameSpecifier*) SS.getScopeRep()
1945 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001946 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1947 return ExprError();
1948 }
1949
1950 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001951}
1952
Douglas Gregorc45c2322009-03-31 00:43:58 +00001953/// \brief Form a dependent template name.
1954///
1955/// This action forms a dependent template name given the template
1956/// name and its (presumably dependent) scope specifier. For
1957/// example, given "MetaFun::template apply", the scope specifier \p
1958/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1959/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001960TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001961 SourceLocation TemplateKWLoc,
1962 CXXScopeSpec &SS,
1963 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001964 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001965 bool EnteringContext,
1966 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001967 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1968 !getLangOptions().CPlusPlus0x)
1969 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001970 << FixItHint::CreateRemoval(TemplateKWLoc);
1971
Douglas Gregor0707bc52010-01-19 16:01:07 +00001972 DeclContext *LookupCtx = 0;
1973 if (SS.isSet())
1974 LookupCtx = computeDeclContext(SS, EnteringContext);
1975 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00001976 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00001977 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001978 // C++0x [temp.names]p5:
1979 // If a name prefixed by the keyword template is not the name of
1980 // a template, the program is ill-formed. [Note: the keyword
1981 // template may not be applied to non-template members of class
1982 // templates. -end note ] [ Note: as is the case with the
1983 // typename prefix, the template prefix is allowed in cases
1984 // where it is not strictly necessary; i.e., when the
1985 // nested-name-specifier or the expression on the left of the ->
1986 // or . is not dependent on a template-parameter, or the use
1987 // does not appear in the scope of a template. -end note]
1988 //
1989 // Note: C++03 was more strict here, because it banned the use of
1990 // the "template" keyword prior to a template-name that was not a
1991 // dependent name. C++ DR468 relaxed this requirement (the
1992 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00001993 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001994 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001995 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
1996 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001997 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001998 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1999 isa<CXXRecordDecl>(LookupCtx) &&
2000 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002001 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002002 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002003 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002004 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002005 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002006 << Name.getSourceRange()
2007 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002008 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002009 } else {
2010 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002011 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002012 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002013 }
2014
Mike Stump1eb44332009-09-09 15:08:12 +00002015 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002016 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002017
Douglas Gregor014e88d2009-11-03 23:16:33 +00002018 switch (Name.getKind()) {
2019 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002020 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002021 Name.Identifier));
2022 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002023
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002024 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002025 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002026 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002027 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002028
2029 case UnqualifiedId::IK_LiteralOperatorId:
2030 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2031
Douglas Gregor014e88d2009-11-03 23:16:33 +00002032 default:
2033 break;
2034 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002035
2036 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002037 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002038 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002039 << Name.getSourceRange()
2040 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002041 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002042}
2043
Mike Stump1eb44332009-09-09 15:08:12 +00002044bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002045 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002046 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002047 const TemplateArgument &Arg = AL.getArgument();
2048
Anders Carlsson436b1562009-06-13 00:33:33 +00002049 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002050 switch(Arg.getKind()) {
2051 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002052 // C++ [temp.arg.type]p1:
2053 // A template-argument for a template-parameter which is a
2054 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002055 break;
2056 case TemplateArgument::Template: {
2057 // We have a template type parameter but the template argument
2058 // is a template without any arguments.
2059 SourceRange SR = AL.getSourceRange();
2060 TemplateName Name = Arg.getAsTemplate();
2061 Diag(SR.getBegin(), diag::err_template_missing_args)
2062 << Name << SR;
2063 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2064 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002065
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002066 return true;
2067 }
2068 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002069 // We have a template type parameter but the template argument
2070 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002071 SourceRange SR = AL.getSourceRange();
2072 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002073 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Anders Carlsson436b1562009-06-13 00:33:33 +00002075 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002076 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002077 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002078
John McCalla93c9342009-12-07 02:54:59 +00002079 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002080 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Anders Carlsson436b1562009-06-13 00:33:33 +00002082 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002083 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002084 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002085 return false;
2086}
2087
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002088/// \brief Substitute template arguments into the default template argument for
2089/// the given template type parameter.
2090///
2091/// \param SemaRef the semantic analysis object for which we are performing
2092/// the substitution.
2093///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002094/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002095/// for.
2096///
2097/// \param TemplateLoc the location of the template name that started the
2098/// template-id we are checking.
2099///
2100/// \param RAngleLoc the location of the right angle bracket ('>') that
2101/// terminates the template-id.
2102///
2103/// \param Param the template template parameter whose default we are
2104/// substituting into.
2105///
2106/// \param Converted the list of template arguments provided for template
2107/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002108/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002109static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002110SubstDefaultTemplateArgument(Sema &SemaRef,
2111 TemplateDecl *Template,
2112 SourceLocation TemplateLoc,
2113 SourceLocation RAngleLoc,
2114 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002115 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002116 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002117
2118 // If the argument type is dependent, instantiate it now based
2119 // on the previously-computed template arguments.
2120 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002121 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002122 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002123
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002124 MultiLevelTemplateArgumentList AllTemplateArgs
2125 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2126
2127 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002128 Template, Converted.data(),
2129 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002130 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002131
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002132 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2133 Param->getDefaultArgumentLoc(),
2134 Param->getDeclName());
2135 }
2136
2137 return ArgType;
2138}
2139
2140/// \brief Substitute template arguments into the default template argument for
2141/// the given non-type template parameter.
2142///
2143/// \param SemaRef the semantic analysis object for which we are performing
2144/// the substitution.
2145///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002146/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002147/// for.
2148///
2149/// \param TemplateLoc the location of the template name that started the
2150/// template-id we are checking.
2151///
2152/// \param RAngleLoc the location of the right angle bracket ('>') that
2153/// terminates the template-id.
2154///
Douglas Gregor788cd062009-11-11 01:00:40 +00002155/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002156/// substituting into.
2157///
2158/// \param Converted the list of template arguments provided for template
2159/// parameters that precede \p Param in the template parameter list.
2160///
2161/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002162static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002163SubstDefaultTemplateArgument(Sema &SemaRef,
2164 TemplateDecl *Template,
2165 SourceLocation TemplateLoc,
2166 SourceLocation RAngleLoc,
2167 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002168 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002169 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002170 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002171
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002172 MultiLevelTemplateArgumentList AllTemplateArgs
2173 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002174
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002175 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002176 Template, Converted.data(),
2177 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002178 SourceRange(TemplateLoc, RAngleLoc));
2179
2180 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2181}
2182
Douglas Gregor788cd062009-11-11 01:00:40 +00002183/// \brief Substitute template arguments into the default template argument for
2184/// the given template template parameter.
2185///
2186/// \param SemaRef the semantic analysis object for which we are performing
2187/// the substitution.
2188///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002189/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002190/// for.
2191///
2192/// \param TemplateLoc the location of the template name that started the
2193/// template-id we are checking.
2194///
2195/// \param RAngleLoc the location of the right angle bracket ('>') that
2196/// terminates the template-id.
2197///
2198/// \param Param the template template parameter whose default we are
2199/// substituting into.
2200///
2201/// \param Converted the list of template arguments provided for template
2202/// parameters that precede \p Param in the template parameter list.
2203///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002204/// \param QualifierLoc Will be set to the nested-name-specifier (with
2205/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002206///
Douglas Gregor788cd062009-11-11 01:00:40 +00002207/// \returns the substituted template argument, or NULL if an error occurred.
2208static TemplateName
2209SubstDefaultTemplateArgument(Sema &SemaRef,
2210 TemplateDecl *Template,
2211 SourceLocation TemplateLoc,
2212 SourceLocation RAngleLoc,
2213 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002214 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2215 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002216 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002217 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002218
Douglas Gregor788cd062009-11-11 01:00:40 +00002219 MultiLevelTemplateArgumentList AllTemplateArgs
2220 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002221
Douglas Gregor788cd062009-11-11 01:00:40 +00002222 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002223 Template, Converted.data(),
2224 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002225 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002226
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002227 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002228 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002229 if (QualifierLoc) {
2230 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2231 AllTemplateArgs);
2232 if (!QualifierLoc)
2233 return TemplateName();
2234 }
2235
Douglas Gregor1d752d72011-03-02 18:46:51 +00002236 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002237 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002238 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002239 AllTemplateArgs);
2240}
2241
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002242/// \brief If the given template parameter has a default template
2243/// argument, substitute into that default template argument and
2244/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002245TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002246Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2247 SourceLocation TemplateLoc,
2248 SourceLocation RAngleLoc,
2249 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002250 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2251 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002252 if (!TypeParm->hasDefaultArgument())
2253 return TemplateArgumentLoc();
2254
John McCalla93c9342009-12-07 02:54:59 +00002255 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002256 TemplateLoc,
2257 RAngleLoc,
2258 TypeParm,
2259 Converted);
2260 if (DI)
2261 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2262
2263 return TemplateArgumentLoc();
2264 }
2265
2266 if (NonTypeTemplateParmDecl *NonTypeParm
2267 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2268 if (!NonTypeParm->hasDefaultArgument())
2269 return TemplateArgumentLoc();
2270
John McCall60d7b3a2010-08-24 06:29:42 +00002271 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002272 TemplateLoc,
2273 RAngleLoc,
2274 NonTypeParm,
2275 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002276 if (Arg.isInvalid())
2277 return TemplateArgumentLoc();
2278
2279 Expr *ArgE = Arg.takeAs<Expr>();
2280 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2281 }
2282
2283 TemplateTemplateParmDecl *TempTempParm
2284 = cast<TemplateTemplateParmDecl>(Param);
2285 if (!TempTempParm->hasDefaultArgument())
2286 return TemplateArgumentLoc();
2287
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002288
Douglas Gregor1d752d72011-03-02 18:46:51 +00002289 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002290 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002291 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002292 RAngleLoc,
2293 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002294 Converted,
2295 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002296 if (TName.isNull())
2297 return TemplateArgumentLoc();
2298
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002299 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002300 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002301 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2302}
2303
Douglas Gregore7526412009-11-11 19:31:23 +00002304/// \brief Check that the given template argument corresponds to the given
2305/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002306///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002307/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002308/// checked.
2309///
2310/// \param Arg The template argument.
2311///
2312/// \param Template The template in which the template argument resides.
2313///
2314/// \param TemplateLoc The location of the template name for the template
2315/// whose argument list we're matching.
2316///
2317/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2318/// the template argument list.
2319///
2320/// \param ArgumentPackIndex The index into the argument pack where this
2321/// argument will be placed. Only valid if the parameter is a parameter pack.
2322///
2323/// \param Converted The checked, converted argument will be added to the
2324/// end of this small vector.
2325///
2326/// \param CTAK Describes how we arrived at this particular template argument:
2327/// explicitly written, deduced, etc.
2328///
2329/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002330bool Sema::CheckTemplateArgument(NamedDecl *Param,
2331 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002332 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002333 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002334 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002335 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002336 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002337 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002338 // Check template type parameters.
2339 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002340 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002341
Douglas Gregord9e15302009-11-11 19:41:09 +00002342 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002343 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002344 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002345 // with the template arguments we've seen thus far. But if the
2346 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002347 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002348 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2349 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002350
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002351 if (NTTPType->isDependentType() &&
2352 !isa<TemplateTemplateParmDecl>(Template) &&
2353 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002354 // Do substitution on the type of the non-type template parameter.
2355 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002356 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002357 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002358
2359 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002360 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002361 NTTPType = SubstType(NTTPType,
2362 MultiLevelTemplateArgumentList(TemplateArgs),
2363 NTTP->getLocation(),
2364 NTTP->getDeclName());
2365 // If that worked, check the non-type template parameter type
2366 // for validity.
2367 if (!NTTPType.isNull())
2368 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2369 NTTP->getLocation());
2370 if (NTTPType.isNull())
2371 return true;
2372 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002373
Douglas Gregore7526412009-11-11 19:31:23 +00002374 switch (Arg.getArgument().getKind()) {
2375 case TemplateArgument::Null:
2376 assert(false && "Should never see a NULL template argument here");
2377 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002378
Douglas Gregore7526412009-11-11 19:31:23 +00002379 case TemplateArgument::Expression: {
2380 Expr *E = Arg.getArgument().getAsExpr();
2381 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002382 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002383 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002384
Douglas Gregor910f8002010-11-07 23:05:16 +00002385 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002386 break;
2387 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002388
Douglas Gregore7526412009-11-11 19:31:23 +00002389 case TemplateArgument::Declaration:
2390 case TemplateArgument::Integral:
2391 // We've already checked this template argument, so just copy
2392 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002393 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002394 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002395
Douglas Gregore7526412009-11-11 19:31:23 +00002396 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002397 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002398 // We were given a template template argument. It may not be ill-formed;
2399 // see below.
2400 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002401 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2402 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002403 // We have a template argument such as \c T::template X, which we
2404 // parsed as a template template argument. However, since we now
2405 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002406 // template name into an expression.
2407
2408 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2409 Arg.getTemplateNameLoc());
2410
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002411 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002412 SS.Adopt(Arg.getTemplateQualifierLoc());
John McCallf7a1a742009-11-24 19:00:30 +00002413 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002414 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002415 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002416
Douglas Gregora7fc9012011-01-05 18:58:31 +00002417 // If we parsed the template argument as a pack expansion, create a
2418 // pack expansion expression.
2419 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002420 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002421 Arg.getTemplateEllipsisLoc());
2422 if (Expansion.isInvalid())
2423 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002424
Douglas Gregora7fc9012011-01-05 18:58:31 +00002425 E = Expansion.get();
2426 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002427
Douglas Gregore7526412009-11-11 19:31:23 +00002428 TemplateArgument Result;
2429 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2430 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002431
Douglas Gregor910f8002010-11-07 23:05:16 +00002432 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002433 break;
2434 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002435
Douglas Gregore7526412009-11-11 19:31:23 +00002436 // We have a template argument that actually does refer to a class
2437 // template, template alias, or template template parameter, and
2438 // therefore cannot be a non-type template argument.
2439 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2440 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002441
Douglas Gregore7526412009-11-11 19:31:23 +00002442 Diag(Param->getLocation(), diag::note_template_param_here);
2443 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002444
Douglas Gregore7526412009-11-11 19:31:23 +00002445 case TemplateArgument::Type: {
2446 // We have a non-type template parameter but the template
2447 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002448
Douglas Gregore7526412009-11-11 19:31:23 +00002449 // C++ [temp.arg]p2:
2450 // In a template-argument, an ambiguity between a type-id and
2451 // an expression is resolved to a type-id, regardless of the
2452 // form of the corresponding template-parameter.
2453 //
2454 // We warn specifically about this case, since it can be rather
2455 // confusing for users.
2456 QualType T = Arg.getArgument().getAsType();
2457 SourceRange SR = Arg.getSourceRange();
2458 if (T->isFunctionType())
2459 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2460 else
2461 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2462 Diag(Param->getLocation(), diag::note_template_param_here);
2463 return true;
2464 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002465
Douglas Gregore7526412009-11-11 19:31:23 +00002466 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002467 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002468 break;
2469 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002470
Douglas Gregore7526412009-11-11 19:31:23 +00002471 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002472 }
2473
2474
Douglas Gregore7526412009-11-11 19:31:23 +00002475 // Check template template parameters.
2476 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002477
Douglas Gregore7526412009-11-11 19:31:23 +00002478 // Substitute into the template parameter list of the template
2479 // template parameter, since previously-supplied template arguments
2480 // may appear within the template template parameter.
2481 {
2482 // Set up a template instantiation context.
2483 LocalInstantiationScope Scope(*this);
2484 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002485 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002486 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002487
2488 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002489 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002490 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002491 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002492 MultiLevelTemplateArgumentList(TemplateArgs)));
2493 if (!TempParm)
2494 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002495 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002496
Douglas Gregore7526412009-11-11 19:31:23 +00002497 switch (Arg.getArgument().getKind()) {
2498 case TemplateArgument::Null:
2499 assert(false && "Should never see a NULL template argument here");
2500 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002501
Douglas Gregore7526412009-11-11 19:31:23 +00002502 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002503 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002504 if (CheckTemplateArgument(TempParm, Arg))
2505 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002506
Douglas Gregor910f8002010-11-07 23:05:16 +00002507 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002508 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002509
Douglas Gregore7526412009-11-11 19:31:23 +00002510 case TemplateArgument::Expression:
2511 case TemplateArgument::Type:
2512 // We have a template template parameter but the template
2513 // argument does not refer to a template.
2514 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2515 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002516
Douglas Gregore7526412009-11-11 19:31:23 +00002517 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002518 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002519 "Declaration argument with template template parameter");
2520 break;
2521 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002522 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002523 "Integral argument with template template parameter");
2524 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002525
Douglas Gregore7526412009-11-11 19:31:23 +00002526 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002527 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002528 break;
2529 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002530
Douglas Gregore7526412009-11-11 19:31:23 +00002531 return false;
2532}
2533
Douglas Gregorc15cb382009-02-09 23:23:08 +00002534/// \brief Check that the given template argument list is well-formed
2535/// for specializing the given template.
2536bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2537 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002538 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002539 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002540 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002541 TemplateParameterList *Params = Template->getTemplateParameters();
2542 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002543 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002544 bool Invalid = false;
2545
John McCalld5532b62009-11-23 01:53:49 +00002546 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2547
Mike Stump1eb44332009-09-09 15:08:12 +00002548 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002549 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002550
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002551 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002552 (NumArgs < Params->getMinRequiredArguments() &&
2553 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002554 // FIXME: point at either the first arg beyond what we can handle,
2555 // or the '>', depending on whether we have too many or too few
2556 // arguments.
2557 SourceRange Range;
2558 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002559 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002560 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2561 << (NumArgs > NumParams)
2562 << (isa<ClassTemplateDecl>(Template)? 0 :
2563 isa<FunctionTemplateDecl>(Template)? 1 :
2564 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2565 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002566 Diag(Template->getLocation(), diag::note_template_decl_here)
2567 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002568 Invalid = true;
2569 }
Mike Stump1eb44332009-09-09 15:08:12 +00002570
2571 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002572 // [...] The type and form of each template-argument specified in
2573 // a template-id shall match the type and form specified for the
2574 // corresponding parameter declared by the template in its
2575 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002576 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002577 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2578 TemplateParameterList::iterator Param = Params->begin(),
2579 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002580 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002581 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002582 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002583 if (ArgIdx > NumArgs && PartialTemplateArgs)
2584 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002585
Douglas Gregorf35f8282009-11-11 21:54:23 +00002586 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002587 // If we have an expanded parameter pack, make sure we don't have too
2588 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002589 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002590 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002591 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002592 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2593 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2594 << true
2595 << (isa<ClassTemplateDecl>(Template)? 0 :
2596 isa<FunctionTemplateDecl>(Template)? 1 :
2597 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2598 << Template;
2599 Diag(Template->getLocation(), diag::note_template_decl_here)
2600 << Params->getSourceRange();
2601 return true;
2602 }
2603 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002604
Douglas Gregorf35f8282009-11-11 21:54:23 +00002605 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002606 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2607 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002608 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002609 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002610
Douglas Gregor14be16b2010-12-20 16:57:52 +00002611 if ((*Param)->isTemplateParameterPack()) {
2612 // The template parameter was a template parameter pack, so take the
2613 // deduced argument and place it on the argument pack. Note that we
2614 // stay on the same template parameter so that we can deduce more
2615 // arguments.
2616 ArgumentPack.push_back(Converted.back());
2617 Converted.pop_back();
2618 } else {
2619 // Move to the next template parameter.
2620 ++Param;
2621 }
2622 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002623 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002624 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002625
2626 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002627 // arguments, just break out now and we'll fill in the argument pack below.
2628 if ((*Param)->isTemplateParameterPack())
2629 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002630
Douglas Gregorf35f8282009-11-11 21:54:23 +00002631 // We have a default template argument that we will use.
2632 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002633
Douglas Gregorf35f8282009-11-11 21:54:23 +00002634 // Retrieve the default template argument from the template
2635 // parameter. For each kind of template parameter, we substitute the
2636 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002637 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002638 // the default argument.
2639 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2640 if (!TTP->hasDefaultArgument()) {
2641 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2642 break;
2643 }
2644
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002645 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002646 Template,
2647 TemplateLoc,
2648 RAngleLoc,
2649 TTP,
2650 Converted);
2651 if (!ArgType)
2652 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653
Douglas Gregorf35f8282009-11-11 21:54:23 +00002654 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2655 ArgType);
2656 } else if (NonTypeTemplateParmDecl *NTTP
2657 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2658 if (!NTTP->hasDefaultArgument()) {
2659 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2660 break;
2661 }
2662
John McCall60d7b3a2010-08-24 06:29:42 +00002663 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002664 TemplateLoc,
2665 RAngleLoc,
2666 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002667 Converted);
2668 if (E.isInvalid())
2669 return true;
2670
2671 Expr *Ex = E.takeAs<Expr>();
2672 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2673 } else {
2674 TemplateTemplateParmDecl *TempParm
2675 = cast<TemplateTemplateParmDecl>(*Param);
2676
2677 if (!TempParm->hasDefaultArgument()) {
2678 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2679 break;
2680 }
2681
Douglas Gregor1d752d72011-03-02 18:46:51 +00002682 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002683 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002684 TemplateLoc,
2685 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002686 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002687 Converted,
2688 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002689 if (Name.isNull())
2690 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002691
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002692 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2693 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002694 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002695
Douglas Gregorf35f8282009-11-11 21:54:23 +00002696 // Introduce an instantiation record that describes where we are using
2697 // the default template argument.
2698 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002699 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002700 SourceRange(TemplateLoc, RAngleLoc));
2701
Douglas Gregorf35f8282009-11-11 21:54:23 +00002702 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002703 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002704 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002705 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002706
Douglas Gregor67714232011-03-03 02:41:12 +00002707 // Core issue 150 (assumed resolution): if this is a template template
2708 // parameter, keep track of the default template arguments from the
2709 // template definition.
2710 if (isTemplateTemplateParameter)
2711 TemplateArgs.addArgument(Arg);
2712
Douglas Gregor14be16b2010-12-20 16:57:52 +00002713 // Move to the next template parameter and argument.
2714 ++Param;
2715 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002716 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002717
Douglas Gregor14be16b2010-12-20 16:57:52 +00002718 // Form argument packs for each of the parameter packs remaining.
2719 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002720 // If we're checking a partial list of template arguments, don't fill
2721 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002722
2723 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002724 if (PartialTemplateArgs && ArgumentPack.empty()) {
2725 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002726 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002727 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002728 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002729 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2730 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002731 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002732 ArgumentPack.clear();
2733 }
2734 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002735
Douglas Gregor14be16b2010-12-20 16:57:52 +00002736 ++Param;
2737 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002738
Douglas Gregorc15cb382009-02-09 23:23:08 +00002739 return Invalid;
2740}
2741
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002742namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002743 class UnnamedLocalNoLinkageFinder
2744 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002745 {
2746 Sema &S;
2747 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002748
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002749 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002750
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002751 public:
2752 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2753
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002754 bool Visit(QualType T) {
2755 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002756 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002757
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002758#define TYPE(Class, Parent) \
2759 bool Visit##Class##Type(const Class##Type *);
2760#define ABSTRACT_TYPE(Class, Parent) \
2761 bool Visit##Class##Type(const Class##Type *) { return false; }
2762#define NON_CANONICAL_TYPE(Class, Parent) \
2763 bool Visit##Class##Type(const Class##Type *) { return false; }
2764#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002765
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002766 bool VisitTagDecl(const TagDecl *Tag);
2767 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2768 };
2769}
2770
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002771bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002772 return false;
2773}
2774
2775bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2776 return Visit(T->getElementType());
2777}
2778
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002779bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002780 return Visit(T->getPointeeType());
2781}
2782
2783bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002784 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002785 return Visit(T->getPointeeType());
2786}
2787
2788bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002790 return Visit(T->getPointeeType());
2791}
2792
2793bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002794 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002795 return Visit(T->getPointeeType());
2796}
2797
2798bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002799 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002800 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2801}
2802
2803bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002804 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002805 return Visit(T->getElementType());
2806}
2807
2808bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002809 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002810 return Visit(T->getElementType());
2811}
2812
2813bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002814 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002815 return Visit(T->getElementType());
2816}
2817
2818bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002819 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002820 return Visit(T->getElementType());
2821}
2822
2823bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002824 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002825 return Visit(T->getElementType());
2826}
2827
2828bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2829 return Visit(T->getElementType());
2830}
2831
2832bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2833 return Visit(T->getElementType());
2834}
2835
2836bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2837 const FunctionProtoType* T) {
2838 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002839 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002840 A != AEnd; ++A) {
2841 if (Visit(*A))
2842 return true;
2843 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002844
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002845 return Visit(T->getResultType());
2846}
2847
2848bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2849 const FunctionNoProtoType* T) {
2850 return Visit(T->getResultType());
2851}
2852
2853bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2854 const UnresolvedUsingType*) {
2855 return false;
2856}
2857
2858bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2859 return false;
2860}
2861
2862bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2863 return Visit(T->getUnderlyingType());
2864}
2865
2866bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2867 return false;
2868}
2869
Richard Smith34b41d92011-02-20 03:19:35 +00002870bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2871 return Visit(T->getDeducedType());
2872}
2873
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002874bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2875 return VisitTagDecl(T->getDecl());
2876}
2877
2878bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2879 return VisitTagDecl(T->getDecl());
2880}
2881
2882bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2883 const TemplateTypeParmType*) {
2884 return false;
2885}
2886
Douglas Gregorc3069d62011-01-14 02:55:32 +00002887bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2888 const SubstTemplateTypeParmPackType *) {
2889 return false;
2890}
2891
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002892bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2893 const TemplateSpecializationType*) {
2894 return false;
2895}
2896
2897bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2898 const InjectedClassNameType* T) {
2899 return VisitTagDecl(T->getDecl());
2900}
2901
2902bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2903 const DependentNameType* T) {
2904 return VisitNestedNameSpecifier(T->getQualifier());
2905}
2906
2907bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2908 const DependentTemplateSpecializationType* T) {
2909 return VisitNestedNameSpecifier(T->getQualifier());
2910}
2911
Douglas Gregor7536dd52010-12-20 02:24:11 +00002912bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2913 const PackExpansionType* T) {
2914 return Visit(T->getPattern());
2915}
2916
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002917bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2918 return false;
2919}
2920
2921bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2922 const ObjCInterfaceType *) {
2923 return false;
2924}
2925
2926bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2927 const ObjCObjectPointerType *) {
2928 return false;
2929}
2930
2931bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2932 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2933 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2934 << S.Context.getTypeDeclType(Tag) << SR;
2935 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002936 }
2937
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002938 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2939 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2940 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2941 return true;
2942 }
2943
2944 return false;
2945}
2946
2947bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2948 NestedNameSpecifier *NNS) {
2949 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2950 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002951
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002952 switch (NNS->getKind()) {
2953 case NestedNameSpecifier::Identifier:
2954 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002955 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002956 case NestedNameSpecifier::Global:
2957 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002958
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002959 case NestedNameSpecifier::TypeSpec:
2960 case NestedNameSpecifier::TypeSpecWithTemplate:
2961 return Visit(QualType(NNS->getAsType(), 0));
2962 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002963 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002964}
2965
2966
Douglas Gregorc15cb382009-02-09 23:23:08 +00002967/// \brief Check a template argument against its corresponding
2968/// template type parameter.
2969///
2970/// This routine implements the semantics of C++ [temp.arg.type]. It
2971/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002972bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002973 TypeSourceInfo *ArgInfo) {
2974 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002975 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00002976 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00002977
2978 if (Arg->isVariablyModifiedType()) {
2979 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002980 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002981 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002982 }
2983
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002984 // C++03 [temp.arg.type]p2:
2985 // A local type, a type with no linkage, an unnamed type or a type
2986 // compounded from any of these types shall not be used as a
2987 // template-argument for a template type-parameter.
2988 //
2989 // C++0x allows these, and even in C++03 we allow them as an extension with
2990 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00002991 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002992 UnnamedLocalNoLinkageFinder Finder(*this, SR);
2993 (void)Finder.Visit(Context.getCanonicalType(Arg));
2994 }
2995
Douglas Gregorc15cb382009-02-09 23:23:08 +00002996 return false;
2997}
2998
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002999/// \brief Checks whether the given template argument is the address
3000/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003001static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003002CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3003 NonTypeTemplateParmDecl *Param,
3004 QualType ParamType,
3005 Expr *ArgIn,
3006 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003007 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003008 Expr *Arg = ArgIn;
3009 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003010
3011 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003012 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003013 Arg = Cast->getSubExpr();
3014
3015 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003016 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003017 // A template-argument for a non-type, non-template
3018 // template-parameter shall be one of: [...]
3019 //
3020 // -- the address of an object or function with external
3021 // linkage, including function templates and function
3022 // template-ids but excluding non-static class members,
3023 // expressed as & id-expression where the & is optional if
3024 // the name refers to a function or array, or if the
3025 // corresponding template-parameter is a reference; or
3026 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003027
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003028 // In C++98/03 mode, give an extension warning on any extra parentheses.
3029 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3030 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003031 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003032 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003033 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003034 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003035 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003036 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003037 }
3038
3039 Arg = Parens->getSubExpr();
3040 }
3041
Douglas Gregorb7a09262010-04-01 18:32:35 +00003042 bool AddressTaken = false;
3043 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003044 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003045 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003046 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003047 AddressTaken = true;
3048 AddrOpLoc = UnOp->getOperatorLoc();
3049 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003050 } else
3051 DRE = dyn_cast<DeclRefExpr>(Arg);
3052
Douglas Gregorb7a09262010-04-01 18:32:35 +00003053 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003054 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3055 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003056 S.Diag(Param->getLocation(), diag::note_template_param_here);
3057 return true;
3058 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003059
3060 // Stop checking the precise nature of the argument if it is value dependent,
3061 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003062 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003063 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003064 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003065 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003066
Douglas Gregorb7a09262010-04-01 18:32:35 +00003067 if (!isa<ValueDecl>(DRE->getDecl())) {
3068 S.Diag(Arg->getSourceRange().getBegin(),
3069 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003070 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003071 S.Diag(Param->getLocation(), diag::note_template_param_here);
3072 return true;
3073 }
3074
3075 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003076
3077 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003078 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3079 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003080 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003081 S.Diag(Param->getLocation(), diag::note_template_param_here);
3082 return true;
3083 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003084
3085 // Cannot refer to non-static member functions
3086 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003087 if (!Method->isStatic()) {
3088 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003089 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003090 S.Diag(Param->getLocation(), diag::note_template_param_here);
3091 return true;
3092 }
Mike Stump1eb44332009-09-09 15:08:12 +00003093
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003094 // Functions must have external linkage.
3095 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003096 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003097 S.Diag(Arg->getSourceRange().getBegin(),
3098 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003099 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003100 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003101 << true;
3102 return true;
3103 }
3104
3105 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003106 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003107
Douglas Gregorb7a09262010-04-01 18:32:35 +00003108 // If the template parameter has pointer type, the function decays.
3109 if (ParamType->isPointerType() && !AddressTaken)
3110 ArgType = S.Context.getPointerType(Func->getType());
3111 else if (AddressTaken && ParamType->isReferenceType()) {
3112 // If we originally had an address-of operator, but the
3113 // parameter has reference type, complain and (if things look
3114 // like they will work) drop the address-of operator.
3115 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3116 ParamType.getNonReferenceType())) {
3117 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3118 << ParamType;
3119 S.Diag(Param->getLocation(), diag::note_template_param_here);
3120 return true;
3121 }
3122
3123 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3124 << ParamType
3125 << FixItHint::CreateRemoval(AddrOpLoc);
3126 S.Diag(Param->getLocation(), diag::note_template_param_here);
3127
3128 ArgType = Func->getType();
3129 }
3130 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003131 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003132 S.Diag(Arg->getSourceRange().getBegin(),
3133 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003134 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003135 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003136 << true;
3137 return true;
3138 }
3139
Douglas Gregorb7a09262010-04-01 18:32:35 +00003140 // A value of reference type is not an object.
3141 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003142 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003143 diag::err_template_arg_reference_var)
3144 << Var->getType() << Arg->getSourceRange();
3145 S.Diag(Param->getLocation(), diag::note_template_param_here);
3146 return true;
3147 }
3148
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003149 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003150 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003151
3152 // If the template parameter has pointer type, we must have taken
3153 // the address of this object.
3154 if (ParamType->isReferenceType()) {
3155 if (AddressTaken) {
3156 // If we originally had an address-of operator, but the
3157 // parameter has reference type, complain and (if things look
3158 // like they will work) drop the address-of operator.
3159 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3160 ParamType.getNonReferenceType())) {
3161 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3162 << ParamType;
3163 S.Diag(Param->getLocation(), diag::note_template_param_here);
3164 return true;
3165 }
3166
3167 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3168 << ParamType
3169 << FixItHint::CreateRemoval(AddrOpLoc);
3170 S.Diag(Param->getLocation(), diag::note_template_param_here);
3171
3172 ArgType = Var->getType();
3173 }
3174 } else if (!AddressTaken && ParamType->isPointerType()) {
3175 if (Var->getType()->isArrayType()) {
3176 // Array-to-pointer decay.
3177 ArgType = S.Context.getArrayDecayedType(Var->getType());
3178 } else {
3179 // If the template parameter has pointer type but the address of
3180 // this object was not taken, complain and (possibly) recover by
3181 // taking the address of the entity.
3182 ArgType = S.Context.getPointerType(Var->getType());
3183 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3184 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3185 << ParamType;
3186 S.Diag(Param->getLocation(), diag::note_template_param_here);
3187 return true;
3188 }
3189
3190 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3191 << ParamType
3192 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3193
3194 S.Diag(Param->getLocation(), diag::note_template_param_here);
3195 }
3196 }
3197 } else {
3198 // We found something else, but we don't know specifically what it is.
3199 S.Diag(Arg->getSourceRange().getBegin(),
3200 diag::err_template_arg_not_object_or_func)
3201 << Arg->getSourceRange();
3202 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3203 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003204 }
Mike Stump1eb44332009-09-09 15:08:12 +00003205
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003206 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003207 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003208 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003209 // For pointer-to-object types, qualification conversions are
3210 // permitted.
3211 } else {
3212 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3213 if (!ParamRef->getPointeeType()->isFunctionType()) {
3214 // C++ [temp.arg.nontype]p5b3:
3215 // For a non-type template-parameter of type reference to
3216 // object, no conversions apply. The type referred to by the
3217 // reference may be more cv-qualified than the (otherwise
3218 // identical) type of the template- argument. The
3219 // template-parameter is bound directly to the
3220 // template-argument, which shall be an lvalue.
3221
3222 // FIXME: Other qualifiers?
3223 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3224 unsigned ArgQuals = ArgType.getCVRQualifiers();
3225
3226 if ((ParamQuals | ArgQuals) != ParamQuals) {
3227 S.Diag(Arg->getSourceRange().getBegin(),
3228 diag::err_template_arg_ref_bind_ignores_quals)
3229 << ParamType << Arg->getType()
3230 << Arg->getSourceRange();
3231 S.Diag(Param->getLocation(), diag::note_template_param_here);
3232 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003233 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003234 }
3235 }
3236
3237 // At this point, the template argument refers to an object or
3238 // function with external linkage. We now need to check whether the
3239 // argument and parameter types are compatible.
3240 if (!S.Context.hasSameUnqualifiedType(ArgType,
3241 ParamType.getNonReferenceType())) {
3242 // We can't perform this conversion or binding.
3243 if (ParamType->isReferenceType())
3244 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3245 << ParamType << Arg->getType() << Arg->getSourceRange();
3246 else
3247 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3248 << Arg->getType() << ParamType << Arg->getSourceRange();
3249 S.Diag(Param->getLocation(), diag::note_template_param_here);
3250 return true;
3251 }
3252 }
3253
3254 // Create the template argument.
3255 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003256 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003257 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003258}
3259
3260/// \brief Checks whether the given template argument is a pointer to
3261/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003262bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003263 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003264 bool Invalid = false;
3265
3266 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003267 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003268 Arg = Cast->getSubExpr();
3269
3270 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003271 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003272 // A template-argument for a non-type, non-template
3273 // template-parameter shall be one of: [...]
3274 //
3275 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003276 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003277
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003278 // In C++98/03 mode, give an extension warning on any extra parentheses.
3279 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3280 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003281 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003282 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003283 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003284 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003285 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003286 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003287 }
3288
3289 Arg = Parens->getSubExpr();
3290 }
3291
Douglas Gregorcaddba02009-11-12 18:38:13 +00003292 // A pointer-to-member constant written &Class::member.
3293 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003294 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003295 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3296 if (DRE && !DRE->getQualifier())
3297 DRE = 0;
3298 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003299 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003300 // A constant of pointer-to-member type.
3301 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3302 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3303 if (VD->getType()->isMemberPointerType()) {
3304 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003305 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003306 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3307 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003308 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003309 else
3310 Converted = TemplateArgument(VD->getCanonicalDecl());
3311 return Invalid;
3312 }
3313 }
3314 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003315
Douglas Gregorcaddba02009-11-12 18:38:13 +00003316 DRE = 0;
3317 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003318
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003319 if (!DRE)
3320 return Diag(Arg->getSourceRange().getBegin(),
3321 diag::err_template_arg_not_pointer_to_member_form)
3322 << Arg->getSourceRange();
3323
3324 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3325 assert((isa<FieldDecl>(DRE->getDecl()) ||
3326 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3327 "Only non-static member pointers can make it here");
3328
3329 // Okay: this is the address of a non-static member, and therefore
3330 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003331 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003332 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003333 else
3334 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003335 return Invalid;
3336 }
3337
3338 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003339 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003340 diag::err_template_arg_not_pointer_to_member_form)
3341 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003342 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003343 diag::note_template_arg_refers_here);
3344 return true;
3345}
3346
Douglas Gregorc15cb382009-02-09 23:23:08 +00003347/// \brief Check a template argument against its corresponding
3348/// non-type template parameter.
3349///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003350/// This routine implements the semantics of C++ [temp.arg.nontype].
3351/// It returns true if an error occurred, and false otherwise. \p
3352/// InstantiatedParamType is the type of the non-type template
3353/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003354///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003355/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003356bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003357 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003358 TemplateArgument &Converted,
3359 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003360 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3361
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003362 // If either the parameter has a dependent type or the argument is
3363 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003364 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3365 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003366 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003367 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003368 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003369
3370 // C++ [temp.arg.nontype]p5:
3371 // The following conversions are performed on each expression used
3372 // as a non-type template-argument. If a non-type
3373 // template-argument cannot be converted to the type of the
3374 // corresponding template-parameter then the program is
3375 // ill-formed.
3376 //
3377 // -- for a non-type template-parameter of integral or
3378 // enumeration type, integral promotions (4.5) and integral
3379 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003380 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003381 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003382 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003383 // C++ [temp.arg.nontype]p1:
3384 // A template-argument for a non-type, non-template
3385 // template-parameter shall be one of:
3386 //
3387 // -- an integral constant-expression of integral or enumeration
3388 // type; or
3389 // -- the name of a non-type template-parameter; or
3390 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003391 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003392 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003393 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003394 diag::err_template_arg_not_integral_or_enumeral)
3395 << ArgType << Arg->getSourceRange();
3396 Diag(Param->getLocation(), diag::note_template_param_here);
3397 return true;
3398 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003399 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003400 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3401 << ArgType << Arg->getSourceRange();
3402 return true;
3403 }
3404
Douglas Gregor02024a92010-03-28 02:42:43 +00003405 // From here on out, all we care about are the unqualified forms
3406 // of the parameter and argument types.
3407 ParamType = ParamType.getUnqualifiedType();
3408 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003409
3410 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003411 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003412 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003413 } else if (CTAK == CTAK_Deduced) {
3414 // C++ [temp.deduct.type]p17:
3415 // If, in the declaration of a function template with a non-type
3416 // template-parameter, the non-type template- parameter is used
3417 // in an expression in the function parameter-list and, if the
3418 // corresponding template-argument is deduced, the
3419 // template-argument type shall match the type of the
3420 // template-parameter exactly, except that a template-argument
3421 // deduced from an array bound may be of any integral type.
3422 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3423 << ArgType << ParamType;
3424 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003425 return true;
3426 } else if (ParamType->isBooleanType()) {
3427 // This is an integral-to-boolean conversion.
3428 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003429 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3430 !ParamType->isEnumeralType()) {
3431 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003432 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003433 } else {
3434 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003435 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003436 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003437 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003438 Diag(Param->getLocation(), diag::note_template_param_here);
3439 return true;
3440 }
3441
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003442 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003443 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003444 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003445
3446 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003447 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003448
3449 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003450 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003451 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003452 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003453 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003454 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003455
3456 // Complain if an unsigned parameter received a negative value.
3457 if (IntegerType->isUnsignedIntegerType()
3458 && (OldValue.isSigned() && OldValue.isNegative())) {
3459 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3460 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3461 << Arg->getSourceRange();
3462 Diag(Param->getLocation(), diag::note_template_param_here);
3463 }
3464
3465 // Complain if we overflowed the template parameter's type.
3466 unsigned RequiredBits;
3467 if (IntegerType->isUnsignedIntegerType())
3468 RequiredBits = OldValue.getActiveBits();
3469 else if (OldValue.isUnsigned())
3470 RequiredBits = OldValue.getActiveBits() + 1;
3471 else
3472 RequiredBits = OldValue.getMinSignedBits();
3473 if (RequiredBits > AllowedBits) {
3474 Diag(Arg->getSourceRange().getBegin(),
3475 diag::warn_template_arg_too_large)
3476 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3477 << Arg->getSourceRange();
3478 Diag(Param->getLocation(), diag::note_template_param_here);
3479 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003480 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003481
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003482 // Add the value of this argument to the list of converted
3483 // arguments. We use the bitwidth and signedness of the template
3484 // parameter.
3485 if (Arg->isValueDependent()) {
3486 // The argument is value-dependent. Create a new
3487 // TemplateArgument with the converted expression.
3488 Converted = TemplateArgument(Arg);
3489 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003490 }
3491
John McCall833ca992009-10-29 08:12:44 +00003492 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003493 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003494 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003495 return false;
3496 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003497
John McCall6bb80172010-03-30 21:47:33 +00003498 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3499
Douglas Gregorb7a09262010-04-01 18:32:35 +00003500 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3501 // from a template argument of type std::nullptr_t to a non-type
3502 // template parameter of type pointer to object, pointer to
3503 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003504 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003505 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3506 Converted = TemplateArgument((NamedDecl *)0);
3507 return false;
3508 }
3509
Douglas Gregorb86b0572009-02-11 01:18:59 +00003510 // Handle pointer-to-function, reference-to-function, and
3511 // pointer-to-member-function all in (roughly) the same way.
3512 if (// -- For a non-type template-parameter of type pointer to
3513 // function, only the function-to-pointer conversion (4.3) is
3514 // applied. If the template-argument represents a set of
3515 // overloaded functions (or a pointer to such), the matching
3516 // function is selected from the set (13.4).
3517 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003518 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003519 // -- For a non-type template-parameter of type reference to
3520 // function, no conversions apply. If the template-argument
3521 // represents a set of overloaded functions, the matching
3522 // function is selected from the set (13.4).
3523 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003524 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003525 // -- For a non-type template-parameter of type pointer to
3526 // member function, no conversions apply. If the
3527 // template-argument represents a set of overloaded member
3528 // functions, the matching member function is selected from
3529 // the set (13.4).
3530 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003531 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003532 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003533
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003534 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003535 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003536 true,
3537 FoundResult)) {
3538 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3539 return true;
3540
3541 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3542 ArgType = Arg->getType();
3543 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003544 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003545 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003546
Douglas Gregorb7a09262010-04-01 18:32:35 +00003547 if (!ParamType->isMemberPointerType())
3548 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003549 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003550 Arg, Converted);
3551
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003552 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3553 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003554 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003555 } else if (!Context.hasSameUnqualifiedType(ArgType,
3556 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003557 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003558 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003559 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003560 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003561 Diag(Param->getLocation(), diag::note_template_param_here);
3562 return true;
3563 }
Mike Stump1eb44332009-09-09 15:08:12 +00003564
Douglas Gregorb7a09262010-04-01 18:32:35 +00003565 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003566 }
3567
Chris Lattnerfe90de72009-02-20 21:37:53 +00003568 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003569 // -- for a non-type template-parameter of type pointer to
3570 // object, qualification conversions (4.4) and the
3571 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003572 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003573 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003574 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003575
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003576 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003577 ParamType,
3578 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003579 }
Mike Stump1eb44332009-09-09 15:08:12 +00003580
Ted Kremenek6217b802009-07-29 21:53:49 +00003581 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003582 // -- For a non-type template-parameter of type reference to
3583 // object, no conversions apply. The type referred to by the
3584 // reference may be more cv-qualified than the (otherwise
3585 // identical) type of the template-argument. The
3586 // template-parameter is bound directly to the
3587 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003588 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003589 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003590
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003591 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003592 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3593 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003594 true,
3595 FoundResult)) {
3596 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3597 return true;
3598
3599 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3600 ArgType = Arg->getType();
3601 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003602 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003603 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003604
3605 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003606 ParamType,
3607 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003608 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003609
3610 // -- For a non-type template-parameter of type pointer to data
3611 // member, qualification conversions (4.4) are applied.
3612 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3613
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003614 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003615 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003616 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003617 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003618 } else {
3619 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003620 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003621 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003622 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003623 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003624 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003625 }
3626
Douglas Gregorcaddba02009-11-12 18:38:13 +00003627 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003628}
3629
3630/// \brief Check a template argument against its corresponding
3631/// template template parameter.
3632///
3633/// This routine implements the semantics of C++ [temp.arg.template].
3634/// It returns true if an error occurred, and false otherwise.
3635bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003636 const TemplateArgumentLoc &Arg) {
3637 TemplateName Name = Arg.getArgument().getAsTemplate();
3638 TemplateDecl *Template = Name.getAsTemplateDecl();
3639 if (!Template) {
3640 // Any dependent template name is fine.
3641 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3642 return false;
3643 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003644
3645 // C++ [temp.arg.template]p1:
3646 // A template-argument for a template template-parameter shall be
3647 // the name of a class template, expressed as id-expression. Only
3648 // primary class templates are considered when matching the
3649 // template template argument with the corresponding parameter;
3650 // partial specializations are not considered even if their
3651 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003652 //
3653 // Note that we also allow template template parameters here, which
3654 // will happen when we are dealing with, e.g., class template
3655 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003656 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003657 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003658 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003659 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003660 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003661 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003662 << Template;
3663 }
3664
3665 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3666 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003667 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003668 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003669 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003670}
3671
Douglas Gregor02024a92010-03-28 02:42:43 +00003672/// \brief Given a non-type template argument that refers to a
3673/// declaration and the type of its corresponding non-type template
3674/// parameter, produce an expression that properly refers to that
3675/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003676ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003677Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3678 QualType ParamType,
3679 SourceLocation Loc) {
3680 assert(Arg.getKind() == TemplateArgument::Declaration &&
3681 "Only declaration template arguments permitted here");
3682 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3683
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003684 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003685 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3686 // If the value is a class member, we might have a pointer-to-member.
3687 // Determine whether the non-type template template parameter is of
3688 // pointer-to-member type. If so, we need to build an appropriate
3689 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3690 // would refer to the member itself.
3691 if (ParamType->isMemberPointerType()) {
3692 QualType ClassType
3693 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3694 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003695 = NestedNameSpecifier::Create(Context, 0, false,
3696 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003697 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003698 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003699
3700 // The actual value-ness of this is unimportant, but for
3701 // internal consistency's sake, references to instance methods
3702 // are r-values.
3703 ExprValueKind VK = VK_LValue;
3704 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3705 VK = VK_RValue;
3706
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003707 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003708 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003709 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003710 Loc,
3711 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003712 if (RefExpr.isInvalid())
3713 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003714
John McCall2de56d12010-08-25 11:45:40 +00003715 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003716
Douglas Gregorc0c83002010-04-30 21:46:38 +00003717 // We might need to perform a trailing qualification conversion, since
3718 // the element type on the parameter could be more qualified than the
3719 // element type in the expression we constructed.
3720 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003721 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003722 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003723 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003724 RefExpr = Owned(RefE);
3725 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003726
Douglas Gregor02024a92010-03-28 02:42:43 +00003727 assert(!RefExpr.isInvalid() &&
3728 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003729 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003730 return move(RefExpr);
3731 }
3732 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003733
Douglas Gregor02024a92010-03-28 02:42:43 +00003734 QualType T = VD->getType().getNonReferenceType();
3735 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003736 // When the non-type template parameter is a pointer, take the
3737 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003738 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003739 if (RefExpr.isInvalid())
3740 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003741
3742 if (T->isFunctionType() || T->isArrayType()) {
3743 // Decay functions and arrays.
3744 Expr *RefE = (Expr *)RefExpr.get();
3745 DefaultFunctionArrayConversion(RefE);
3746 if (RefE != RefExpr.get()) {
3747 RefExpr.release();
3748 RefExpr = Owned(RefE);
3749 }
3750
3751 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003752 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003753
Douglas Gregorb7a09262010-04-01 18:32:35 +00003754 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003755 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003756 }
3757
John McCallf89e55a2010-11-18 06:31:45 +00003758 ExprValueKind VK = VK_RValue;
3759
Douglas Gregor02024a92010-03-28 02:42:43 +00003760 // If the non-type template parameter has reference type, qualify the
3761 // resulting declaration reference with the extra qualifiers on the
3762 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003763 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3764 VK = VK_LValue;
3765 T = Context.getQualifiedType(T,
3766 TargetRef->getPointeeType().getQualifiers());
3767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003768
John McCallf89e55a2010-11-18 06:31:45 +00003769 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003770}
3771
3772/// \brief Construct a new expression that refers to the given
3773/// integral template argument with the given source-location
3774/// information.
3775///
3776/// This routine takes care of the mapping from an integral template
3777/// argument (which may have any integral type) to the appropriate
3778/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003779ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003780Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3781 SourceLocation Loc) {
3782 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003783 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003784 QualType T = Arg.getIntegralType();
3785 if (T->isCharType() || T->isWideCharType())
3786 return Owned(new (Context) CharacterLiteral(
3787 Arg.getAsIntegral()->getZExtValue(),
3788 T->isWideCharType(),
3789 T,
3790 Loc));
3791 if (T->isBooleanType())
3792 return Owned(new (Context) CXXBoolLiteralExpr(
3793 Arg.getAsIntegral()->getBoolValue(),
3794 T,
3795 Loc));
3796
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003797 QualType BT;
3798 if (const EnumType *ET = T->getAs<EnumType>())
3799 BT = ET->getDecl()->getPromotionType();
3800 else
3801 BT = T;
3802
3803 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003804 if (T->isEnumeralType()) {
3805 // FIXME: This is a hack. We need a better way to handle substituted
3806 // non-type template parameters.
3807 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3808 E, 0,
3809 Context.getTrivialTypeSourceInfo(T, Loc),
3810 Loc, Loc);
3811 }
3812
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003813 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003814}
3815
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003816/// \brief Match two template parameters within template parameter lists.
3817static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3818 bool Complain,
3819 Sema::TemplateParameterListEqualKind Kind,
3820 SourceLocation TemplateArgLoc) {
3821 // Check the actual kind (type, non-type, template).
3822 if (Old->getKind() != New->getKind()) {
3823 if (Complain) {
3824 unsigned NextDiag = diag::err_template_param_different_kind;
3825 if (TemplateArgLoc.isValid()) {
3826 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3827 NextDiag = diag::note_template_param_different_kind;
3828 }
3829 S.Diag(New->getLocation(), NextDiag)
3830 << (Kind != Sema::TPL_TemplateMatch);
3831 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3832 << (Kind != Sema::TPL_TemplateMatch);
3833 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003834
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003835 return false;
3836 }
3837
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003838 // Check that both are parameter packs are neither are parameter packs.
3839 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003840 // template template parameter, the template template parameter can have
3841 // a parameter pack where the template template argument does not.
3842 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3843 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3844 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003845 if (Complain) {
3846 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3847 if (TemplateArgLoc.isValid()) {
3848 S.Diag(TemplateArgLoc,
3849 diag::err_template_arg_template_params_mismatch);
3850 NextDiag = diag::note_template_parameter_pack_non_pack;
3851 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003852
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003853 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3854 : isa<NonTypeTemplateParmDecl>(New)? 1
3855 : 2;
3856 S.Diag(New->getLocation(), NextDiag)
3857 << ParamKind << New->isParameterPack();
3858 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3859 << ParamKind << Old->isParameterPack();
3860 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003861
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003862 return false;
3863 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003864
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003865 // For non-type template parameters, check the type of the parameter.
3866 if (NonTypeTemplateParmDecl *OldNTTP
3867 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3868 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003869
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003870 // If we are matching a template template argument to a template
3871 // template parameter and one of the non-type template parameter types
3872 // is dependent, then we must wait until template instantiation time
3873 // to actually compare the arguments.
3874 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3875 (OldNTTP->getType()->isDependentType() ||
3876 NewNTTP->getType()->isDependentType()))
3877 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003878
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003879 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3880 if (Complain) {
3881 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3882 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003883 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003884 diag::err_template_arg_template_params_mismatch);
3885 NextDiag = diag::note_template_nontype_parm_different_type;
3886 }
3887 S.Diag(NewNTTP->getLocation(), NextDiag)
3888 << NewNTTP->getType()
3889 << (Kind != Sema::TPL_TemplateMatch);
3890 S.Diag(OldNTTP->getLocation(),
3891 diag::note_template_nontype_parm_prev_declaration)
3892 << OldNTTP->getType();
3893 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003894
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003895 return false;
3896 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003897
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003898 return true;
3899 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003900
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003901 // For template template parameters, check the template parameter types.
3902 // The template parameter lists of template template
3903 // parameters must agree.
3904 if (TemplateTemplateParmDecl *OldTTP
3905 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003906 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003907 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3908 OldTTP->getTemplateParameters(),
3909 Complain,
3910 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003911 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003912 : Kind),
3913 TemplateArgLoc);
3914 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003915
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003916 return true;
3917}
Douglas Gregor02024a92010-03-28 02:42:43 +00003918
Douglas Gregora0347822011-01-13 00:08:50 +00003919/// \brief Diagnose a known arity mismatch when comparing template argument
3920/// lists.
3921static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003922void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003923 TemplateParameterList *New,
3924 TemplateParameterList *Old,
3925 Sema::TemplateParameterListEqualKind Kind,
3926 SourceLocation TemplateArgLoc) {
3927 unsigned NextDiag = diag::err_template_param_list_different_arity;
3928 if (TemplateArgLoc.isValid()) {
3929 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3930 NextDiag = diag::note_template_param_list_different_arity;
3931 }
3932 S.Diag(New->getTemplateLoc(), NextDiag)
3933 << (New->size() > Old->size())
3934 << (Kind != Sema::TPL_TemplateMatch)
3935 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3936 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3937 << (Kind != Sema::TPL_TemplateMatch)
3938 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3939}
3940
Douglas Gregorddc29e12009-02-06 22:42:48 +00003941/// \brief Determine whether the given template parameter lists are
3942/// equivalent.
3943///
Mike Stump1eb44332009-09-09 15:08:12 +00003944/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003945/// source code as part of a new template declaration.
3946///
3947/// \param Old The old template parameter list, typically found via
3948/// name lookup of the template declared with this template parameter
3949/// list.
3950///
3951/// \param Complain If true, this routine will produce a diagnostic if
3952/// the template parameter lists are not equivalent.
3953///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003954/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003955///
3956/// \param TemplateArgLoc If this source location is valid, then we
3957/// are actually checking the template parameter list of a template
3958/// argument (New) against the template parameter list of its
3959/// corresponding template template parameter (Old). We produce
3960/// slightly different diagnostics in this scenario.
3961///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003962/// \returns True if the template parameter lists are equal, false
3963/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003964bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003965Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3966 TemplateParameterList *Old,
3967 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003968 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00003969 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00003970 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
3971 if (Complain)
3972 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3973 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00003974
3975 return false;
3976 }
3977
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003978 // C++0x [temp.arg.template]p3:
3979 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003980 // when each of the template parameters in the template-parameter-list of
3981 // the template-argument's corresponding class template or template alias
3982 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00003983 // template-parameter-list of P. [...]
3984 TemplateParameterList::iterator NewParm = New->begin();
3985 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003986 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00003987 OldParmEnd = Old->end();
3988 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00003989 if (Kind != TPL_TemplateTemplateArgumentMatch ||
3990 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00003991 if (NewParm == NewParmEnd) {
3992 if (Complain)
3993 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3994 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003995
Douglas Gregora0347822011-01-13 00:08:50 +00003996 return false;
3997 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003998
Douglas Gregora0347822011-01-13 00:08:50 +00003999 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4000 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004001 return false;
4002
Douglas Gregora0347822011-01-13 00:08:50 +00004003 ++NewParm;
4004 continue;
4005 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004006
Douglas Gregora0347822011-01-13 00:08:50 +00004007 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004008 // [...] When P's template- parameter-list contains a template parameter
4009 // pack (14.5.3), the template parameter pack will match zero or more
4010 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004011 // template-parameter-list of A with the same type and form as the
4012 // template parameter pack in P (ignoring whether those template
4013 // parameters are template parameter packs).
4014 for (; NewParm != NewParmEnd; ++NewParm) {
4015 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4016 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004017 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004018 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004019 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004020
Douglas Gregora0347822011-01-13 00:08:50 +00004021 // Make sure we exhausted all of the arguments.
4022 if (NewParm != NewParmEnd) {
4023 if (Complain)
4024 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4025 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004026
Douglas Gregora0347822011-01-13 00:08:50 +00004027 return false;
4028 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004029
Douglas Gregorddc29e12009-02-06 22:42:48 +00004030 return true;
4031}
4032
4033/// \brief Check whether a template can be declared within this scope.
4034///
4035/// If the template declaration is valid in this scope, returns
4036/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004037bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004038Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004039 // Find the nearest enclosing declaration scope.
4040 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4041 (S->getFlags() & Scope::TemplateParamScope) != 0)
4042 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004043
Douglas Gregorddc29e12009-02-06 22:42:48 +00004044 // C++ [temp]p2:
4045 // A template-declaration can appear only as a namespace scope or
4046 // class scope declaration.
4047 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004048 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4049 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004050 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004051 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004052
Eli Friedman1503f772009-07-31 01:43:05 +00004053 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004054 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004055
4056 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4057 return false;
4058
Mike Stump1eb44332009-09-09 15:08:12 +00004059 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004060 diag::err_template_outside_namespace_or_class_scope)
4061 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004062}
Douglas Gregorcc636682009-02-17 23:15:12 +00004063
Douglas Gregord5cb8762009-10-07 00:13:32 +00004064/// \brief Determine what kind of template specialization the given declaration
4065/// is.
4066static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4067 if (!D)
4068 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004069
Douglas Gregorf6b11852009-10-08 15:14:33 +00004070 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4071 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004072 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4073 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004074 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4075 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004076
Douglas Gregord5cb8762009-10-07 00:13:32 +00004077 return TSK_Undeclared;
4078}
4079
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004080/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004081/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004082///
Douglas Gregor9302da62009-10-14 23:50:59 +00004083/// This routine determines whether a template specialization can be declared
4084/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004085///
4086/// \param S the semantic analysis object for which this check is being
4087/// performed.
4088///
4089/// \param Specialized the entity being specialized or instantiated, which
4090/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004091/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004092/// member class).
4093///
4094/// \param PrevDecl the previous declaration of this entity, if any.
4095///
4096/// \param Loc the location of the explicit specialization or instantiation of
4097/// this entity.
4098///
4099/// \param IsPartialSpecialization whether this is a partial specialization of
4100/// a class template.
4101///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004102/// \returns true if there was an error that we cannot recover from, false
4103/// otherwise.
4104static bool CheckTemplateSpecializationScope(Sema &S,
4105 NamedDecl *Specialized,
4106 NamedDecl *PrevDecl,
4107 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004108 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004109 // Keep these "kind" numbers in sync with the %select statements in the
4110 // various diagnostics emitted by this routine.
4111 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004112 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004113 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004114 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004115 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004116 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004117 EntityKind = 3;
4118 else if (isa<VarDecl>(Specialized))
4119 EntityKind = 4;
4120 else if (isa<RecordDecl>(Specialized))
4121 EntityKind = 5;
4122 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004123 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4124 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004125 return true;
4126 }
4127
Douglas Gregor88b70942009-02-25 22:02:03 +00004128 // C++ [temp.expl.spec]p2:
4129 // An explicit specialization shall be declared in the namespace
4130 // of which the template is a member, or, for member templates, in
4131 // the namespace of which the enclosing class or enclosing class
4132 // template is a member. An explicit specialization of a member
4133 // function, member class or static data member of a class
4134 // template shall be declared in the namespace of which the class
4135 // template is a member. Such a declaration may also be a
4136 // definition. If the declaration is not a definition, the
4137 // specialization may be defined later in the name- space in which
4138 // the explicit specialization was declared, or in a namespace
4139 // that encloses the one in which the explicit specialization was
4140 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004141 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004142 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004143 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004144 return true;
4145 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004146
Douglas Gregor0a407472009-10-07 17:30:37 +00004147 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4148 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004149 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004150 return true;
4151 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004152
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004153 // C++ [temp.class.spec]p6:
4154 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004155 // in any namespace scope in which its definition may be defined (14.5.1
4156 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004157 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004158 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004159 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004160 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004161 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004162 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4163 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004164 // C++ [temp.exp.spec]p2:
4165 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004166 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004167 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004168 // An explicit specialization of a member function, member class or
4169 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004170 // namespace of which the class template is a member.
4171 //
4172 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004173 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004174 // the specialized template.
4175 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4176 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004177 bool IsCPlusPlus0xExtension
4178 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004179 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004180 S.Diag(Loc, IsCPlusPlus0xExtension
4181 ? diag::ext_template_spec_decl_out_of_scope_global
4182 : diag::err_template_spec_decl_out_of_scope_global)
4183 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004184 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004185 S.Diag(Loc, IsCPlusPlus0xExtension
4186 ? diag::ext_template_spec_decl_out_of_scope
4187 : diag::err_template_spec_decl_out_of_scope)
4188 << EntityKind << Specialized
4189 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004190
Douglas Gregor9302da62009-10-14 23:50:59 +00004191 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4192 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004193 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004194 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004195
4196 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004197 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004198 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004199 // specializations of function templates, static data members, and member
4200 // functions, so we skip the check here for those kinds of entities.
4201 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004202 // Should we refactor that check, so that it occurs later?
4203 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004204 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4205 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004206 if (isa<TranslationUnitDecl>(SpecializedContext))
4207 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4208 << EntityKind << Specialized;
4209 else if (isa<NamespaceDecl>(SpecializedContext))
4210 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4211 << EntityKind << Specialized
4212 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004213
Douglas Gregor9302da62009-10-14 23:50:59 +00004214 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004215 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004216
Douglas Gregord5cb8762009-10-07 00:13:32 +00004217 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004218
Douglas Gregor88b70942009-02-25 22:02:03 +00004219 return false;
4220}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004221
Douglas Gregorbacb9492011-01-03 21:13:47 +00004222/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4223/// that checks non-type template partial specialization arguments.
4224static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4225 NonTypeTemplateParmDecl *Param,
4226 const TemplateArgument *Args,
4227 unsigned NumArgs) {
4228 for (unsigned I = 0; I != NumArgs; ++I) {
4229 if (Args[I].getKind() == TemplateArgument::Pack) {
4230 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004231 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004232 Args[I].pack_size()))
4233 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004234
Douglas Gregore94866f2009-06-12 21:21:02 +00004235 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004236 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004237
Douglas Gregorbacb9492011-01-03 21:13:47 +00004238 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004239 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004240 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004241 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004242
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004243 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004244 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4245 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004246
4247 // Strip off any implicit casts we added as part of type checking.
4248 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4249 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004250
Douglas Gregore94866f2009-06-12 21:21:02 +00004251 // C++ [temp.class.spec]p8:
4252 // A non-type argument is non-specialized if it is the name of a
4253 // non-type parameter. All other non-type arguments are
4254 // specialized.
4255 //
4256 // Below, we check the two conditions that only apply to
4257 // specialized non-type arguments, so skip any non-specialized
4258 // arguments.
4259 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004260 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004261 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004262
Douglas Gregore94866f2009-06-12 21:21:02 +00004263 // C++ [temp.class.spec]p9:
4264 // Within the argument list of a class template partial
4265 // specialization, the following restrictions apply:
4266 // -- A partially specialized non-type argument expression
4267 // shall not involve a template parameter of the partial
4268 // specialization except when the argument expression is a
4269 // simple identifier.
4270 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004271 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004272 diag::err_dependent_non_type_arg_in_partial_spec)
4273 << ArgExpr->getSourceRange();
4274 return true;
4275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004276
Douglas Gregore94866f2009-06-12 21:21:02 +00004277 // -- The type of a template parameter corresponding to a
4278 // specialized non-type argument shall not be dependent on a
4279 // parameter of the specialization.
4280 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004281 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004282 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4283 << Param->getType()
4284 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004285 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004286 return true;
4287 }
4288 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004289
Douglas Gregorbacb9492011-01-03 21:13:47 +00004290 return false;
4291}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004292
Douglas Gregorbacb9492011-01-03 21:13:47 +00004293/// \brief Check the non-type template arguments of a class template
4294/// partial specialization according to C++ [temp.class.spec]p9.
4295///
4296/// \param TemplateParams the template parameters of the primary class
4297/// template.
4298///
4299/// \param TemplateArg the template arguments of the class template
4300/// partial specialization.
4301///
4302/// \returns true if there was an error, false otherwise.
4303static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4304 TemplateParameterList *TemplateParams,
4305 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4306 const TemplateArgument *ArgList = TemplateArgs.data();
4307
4308 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4309 NonTypeTemplateParmDecl *Param
4310 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4311 if (!Param)
4312 continue;
4313
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004314 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004315 &ArgList[I], 1))
4316 return true;
4317 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004318
4319 return false;
4320}
4321
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004322/// \brief Retrieve the previous declaration of the given declaration.
4323static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4324 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4325 return VD->getPreviousDeclaration();
4326 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4327 return FD->getPreviousDeclaration();
4328 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4329 return TD->getPreviousDeclaration();
4330 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4331 return TD->getPreviousDeclaration();
4332 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4333 return FTD->getPreviousDeclaration();
4334 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4335 return CTD->getPreviousDeclaration();
4336 return 0;
4337}
4338
John McCalld226f652010-08-21 09:40:31 +00004339DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004340Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4341 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004342 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004343 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004344 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004345 SourceLocation TemplateNameLoc,
4346 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004347 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004348 SourceLocation RAngleLoc,
4349 AttributeList *Attr,
4350 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004351 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004352
Douglas Gregorcc636682009-02-17 23:15:12 +00004353 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004354 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004355 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004356 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4357
4358 if (!ClassTemplate) {
4359 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004360 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004361 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4362 return true;
4363 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004364
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004365 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004366 bool isPartialSpecialization = false;
4367
Douglas Gregor88b70942009-02-25 22:02:03 +00004368 // Check the validity of the template headers that introduce this
4369 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004370 // FIXME: We probably shouldn't complain about these headers for
4371 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004372 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004373 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004374 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4375 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004376 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004377 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004378 isExplicitSpecialization,
4379 Invalid);
4380 if (Invalid)
4381 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004382
Abramo Bagnara9b934882010-06-12 08:15:14 +00004383 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4384 if (TemplateParams)
4385 --NumMatchedTemplateParamLists;
4386
Douglas Gregor05396e22009-08-25 17:23:04 +00004387 if (TemplateParams && TemplateParams->size() > 0) {
4388 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004389
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004390 if (TUK == TUK_Friend) {
4391 Diag(KWLoc, diag::err_partial_specialization_friend)
4392 << SourceRange(LAngleLoc, RAngleLoc);
4393 return true;
4394 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004395
Douglas Gregor05396e22009-08-25 17:23:04 +00004396 // C++ [temp.class.spec]p10:
4397 // The template parameter list of a specialization shall not
4398 // contain default template argument values.
4399 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4400 Decl *Param = TemplateParams->getParam(I);
4401 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4402 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004403 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004404 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004405 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004406 }
4407 } else if (NonTypeTemplateParmDecl *NTTP
4408 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4409 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004410 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004411 diag::err_default_arg_in_partial_spec)
4412 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004413 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004414 }
4415 } else {
4416 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004417 if (TTP->hasDefaultArgument()) {
4418 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004419 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004420 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004421 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004422 }
4423 }
4424 }
Douglas Gregora735b202009-10-13 14:39:41 +00004425 } else if (TemplateParams) {
4426 if (TUK == TUK_Friend)
4427 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004428 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004429 SourceRange(TemplateParams->getTemplateLoc(),
4430 TemplateParams->getRAngleLoc()))
4431 << SourceRange(LAngleLoc, RAngleLoc);
4432 else
4433 isExplicitSpecialization = true;
4434 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004435 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004436 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004437 isExplicitSpecialization = true;
4438 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004439
Douglas Gregorcc636682009-02-17 23:15:12 +00004440 // Check that the specialization uses the same tag kind as the
4441 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004442 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4443 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004444 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004445 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004446 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004447 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004448 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004449 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004450 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004451 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004452 diag::note_previous_use);
4453 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4454 }
4455
Douglas Gregor40808ce2009-03-09 23:48:35 +00004456 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004457 TemplateArgumentListInfo TemplateArgs;
4458 TemplateArgs.setLAngleLoc(LAngleLoc);
4459 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004460 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004461
Douglas Gregor925910d2011-01-03 20:35:03 +00004462 // Check for unexpanded parameter packs in any of the template arguments.
4463 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004464 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004465 UPPC_PartialSpecialization))
4466 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004467
Douglas Gregorcc636682009-02-17 23:15:12 +00004468 // Check that the template argument list is well-formed for this
4469 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004470 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004471 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4472 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004473 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004474
Douglas Gregor910f8002010-11-07 23:05:16 +00004475 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004476 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004477
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004478 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004479 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004480 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004481 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004482 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004483 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004484 return true;
4485
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004486 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004487 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004488 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004489 TemplateArgs.size())) {
4490 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4491 << ClassTemplate->getDeclName();
4492 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004493 }
4494 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004495
Douglas Gregorcc636682009-02-17 23:15:12 +00004496 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004497 ClassTemplateSpecializationDecl *PrevDecl = 0;
4498
4499 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004500 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004501 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004502 = ClassTemplate->findPartialSpecialization(Converted.data(),
4503 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004504 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004505 else
4506 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004507 = ClassTemplate->findSpecialization(Converted.data(),
4508 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004509
4510 ClassTemplateSpecializationDecl *Specialization = 0;
4511
Douglas Gregor88b70942009-02-25 22:02:03 +00004512 // Check whether we can declare a class template specialization in
4513 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004514 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004515 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4516 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004517 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004518 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004519
Douglas Gregorb88e8882009-07-30 17:40:51 +00004520 // The canonical type
4521 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004522 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004523 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004524 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004525 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004526 // arguments was referenced but not declared, or we're only
4527 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004528 // declaration node as our own, updating its source location to
4529 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004530 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004531 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004532 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004533 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004534 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004535 // Build the canonical type that describes the converted template
4536 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004537 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4538 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004539 Converted.data(),
4540 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004541
4542 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004543 ClassTemplate->getInjectedClassNameSpecialization())) {
4544 // C++ [temp.class.spec]p9b3:
4545 //
4546 // -- The argument list of the specialization shall not be identical
4547 // to the implicit argument list of the primary template.
4548 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4549 << (TUK == TUK_Definition)
4550 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4551 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4552 ClassTemplate->getIdentifier(),
4553 TemplateNameLoc,
4554 Attr,
4555 TemplateParams,
4556 AS_none);
4557 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004558
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004559 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004560 ClassTemplatePartialSpecializationDecl *PrevPartial
4561 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004562 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004563 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004564 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004565 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004566 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004567 TemplateNameLoc,
4568 TemplateParams,
4569 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004570 Converted.data(),
4571 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004572 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004573 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004574 PrevPartial,
4575 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004576 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004577 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004578 Partial->setTemplateParameterListsInfo(Context,
4579 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004580 (TemplateParameterList**) TemplateParameterLists.release());
4581 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004582
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004583 if (!PrevPartial)
4584 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004585 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004586
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004587 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004588 // template specialization, make a note of that.
4589 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4590 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004591
Douglas Gregor031a5882009-06-13 00:26:55 +00004592 // Check that all of the template parameters of the class template
4593 // partial specialization are deducible from the template
4594 // arguments. If not, this class template partial specialization
4595 // will never be used.
4596 llvm::SmallVector<bool, 8> DeducibleParams;
4597 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004598 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004599 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004600 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004601 unsigned NumNonDeducible = 0;
4602 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4603 if (!DeducibleParams[I])
4604 ++NumNonDeducible;
4605
4606 if (NumNonDeducible) {
4607 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4608 << (NumNonDeducible > 1)
4609 << SourceRange(TemplateNameLoc, RAngleLoc);
4610 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4611 if (!DeducibleParams[I]) {
4612 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4613 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004614 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004615 diag::note_partial_spec_unused_parameter)
4616 << Param->getDeclName();
4617 else
Mike Stump1eb44332009-09-09 15:08:12 +00004618 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004619 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004620 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004621 }
4622 }
4623 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004624 } else {
4625 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004626 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004627 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004628 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004629 ClassTemplate->getDeclContext(),
4630 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004631 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004632 Converted.data(),
4633 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004634 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004635 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004636 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004637 Specialization->setTemplateParameterListsInfo(Context,
4638 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004639 (TemplateParameterList**) TemplateParameterLists.release());
4640 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004641
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004642 if (!PrevDecl)
4643 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004644
4645 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004646 }
4647
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004648 // C++ [temp.expl.spec]p6:
4649 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004650 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004651 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004652 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004653 // use occurs; no diagnostic is required.
4654 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004655 bool Okay = false;
4656 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4657 // Is there any previous explicit specialization declaration?
4658 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4659 Okay = true;
4660 break;
4661 }
4662 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004663
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004664 if (!Okay) {
4665 SourceRange Range(TemplateNameLoc, RAngleLoc);
4666 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4667 << Context.getTypeDeclType(Specialization) << Range;
4668
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004669 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004670 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004671 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004672 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004673 return true;
4674 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004676
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004677 // If this is not a friend, note that this is an explicit specialization.
4678 if (TUK != TUK_Friend)
4679 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004680
4681 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004682 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004683 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004684 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004685 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004686 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004687 Diag(Def->getLocation(), diag::note_previous_definition);
4688 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004689 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004690 }
4691 }
4692
John McCall7f1b9872010-12-18 03:30:47 +00004693 if (Attr)
4694 ProcessDeclAttributeList(S, Specialization, Attr);
4695
Douglas Gregorfc705b82009-02-26 22:19:44 +00004696 // Build the fully-sugared type for this class template
4697 // specialization as the user wrote in the specialization
4698 // itself. This means that we'll pretty-print the type retrieved
4699 // from the specialization's declaration the way that the user
4700 // actually wrote the specialization, rather than formatting the
4701 // name based on the "canonical" representation used to store the
4702 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004703 TypeSourceInfo *WrittenTy
4704 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4705 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004706 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004707 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004708 if (TemplateParams)
4709 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004710 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004711 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004712
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004713 // C++ [temp.expl.spec]p9:
4714 // A template explicit specialization is in the scope of the
4715 // namespace in which the template was defined.
4716 //
4717 // We actually implement this paragraph where we set the semantic
4718 // context (in the creation of the ClassTemplateSpecializationDecl),
4719 // but we also maintain the lexical context where the actual
4720 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004721 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004722
Douglas Gregorcc636682009-02-17 23:15:12 +00004723 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004724 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004725 Specialization->startDefinition();
4726
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004727 if (TUK == TUK_Friend) {
4728 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4729 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004730 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004731 /*FIXME:*/KWLoc);
4732 Friend->setAccess(AS_public);
4733 CurContext->addDecl(Friend);
4734 } else {
4735 // Add the specialization into its lexical context, so that it can
4736 // be seen when iterating through the list of declarations in that
4737 // context. However, specializations are not found by name lookup.
4738 CurContext->addDecl(Specialization);
4739 }
John McCalld226f652010-08-21 09:40:31 +00004740 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004741}
Douglas Gregord57959a2009-03-27 23:10:48 +00004742
John McCalld226f652010-08-21 09:40:31 +00004743Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004744 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004745 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004746 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4747}
4748
John McCalld226f652010-08-21 09:40:31 +00004749Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004750 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004751 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004752 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004753 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004754
Douglas Gregor52591bf2009-06-24 00:54:41 +00004755 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004756 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004757 }
Mike Stump1eb44332009-09-09 15:08:12 +00004758
Douglas Gregor52591bf2009-06-24 00:54:41 +00004759 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004760
John McCalld226f652010-08-21 09:40:31 +00004761 Decl *DP = HandleDeclarator(ParentScope, D,
4762 move(TemplateParameterLists),
4763 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004764 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004765 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004766 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004767 FunctionTemplate->getTemplatedDecl());
4768 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4769 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4770 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004771}
4772
John McCall75042392010-02-11 01:33:53 +00004773/// \brief Strips various properties off an implicit instantiation
4774/// that has just been explicitly specialized.
4775static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004776 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004777
4778 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4779 FD->setInlineSpecified(false);
4780 }
4781}
4782
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004783/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004784/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004785/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004786/// new specialization/instantiation will have any effect.
4787///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004788/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004789/// instantiation.
4790///
4791/// \param NewTSK the kind of the new explicit specialization or instantiation.
4792///
4793/// \param PrevDecl the previous declaration of the entity.
4794///
4795/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4796///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004797/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004798/// declaration was instantiated (either implicitly or explicitly).
4799///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004800/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004801/// specialization or instantiation has no effect and should be ignored.
4802///
4803/// \returns true if there was an error that should prevent the introduction of
4804/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004805bool
4806Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4807 TemplateSpecializationKind NewTSK,
4808 NamedDecl *PrevDecl,
4809 TemplateSpecializationKind PrevTSK,
4810 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004811 bool &HasNoEffect) {
4812 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004813
Douglas Gregor454885e2009-10-15 15:54:05 +00004814 switch (NewTSK) {
4815 case TSK_Undeclared:
4816 case TSK_ImplicitInstantiation:
4817 assert(false && "Don't check implicit instantiations here");
4818 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004819
Douglas Gregor454885e2009-10-15 15:54:05 +00004820 case TSK_ExplicitSpecialization:
4821 switch (PrevTSK) {
4822 case TSK_Undeclared:
4823 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004824 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004825 // explicitly specialized or has merely been mentioned without any
4826 // instantiation.
4827 return false;
4828
4829 case TSK_ImplicitInstantiation:
4830 if (PrevPointOfInstantiation.isInvalid()) {
4831 // The declaration itself has not actually been instantiated, so it is
4832 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004833 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004834 return false;
4835 }
4836 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004837
Douglas Gregor454885e2009-10-15 15:54:05 +00004838 case TSK_ExplicitInstantiationDeclaration:
4839 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004840 assert((PrevTSK == TSK_ImplicitInstantiation ||
4841 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004842 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004843
Douglas Gregor454885e2009-10-15 15:54:05 +00004844 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004845 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004846 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004847 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004848 // implicit instantiation to take place, in every translation unit in
4849 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004850 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4851 // Is there any previous explicit specialization declaration?
4852 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4853 return false;
4854 }
4855
Douglas Gregor0d035142009-10-27 18:42:08 +00004856 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004857 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004858 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004859 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004860
Douglas Gregor454885e2009-10-15 15:54:05 +00004861 return true;
4862 }
4863 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004864
Douglas Gregor454885e2009-10-15 15:54:05 +00004865 case TSK_ExplicitInstantiationDeclaration:
4866 switch (PrevTSK) {
4867 case TSK_ExplicitInstantiationDeclaration:
4868 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004869 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004870 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004871
Douglas Gregor454885e2009-10-15 15:54:05 +00004872 case TSK_Undeclared:
4873 case TSK_ImplicitInstantiation:
4874 // We're explicitly instantiating something that may have already been
4875 // implicitly instantiated; that's fine.
4876 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004877
Douglas Gregor454885e2009-10-15 15:54:05 +00004878 case TSK_ExplicitSpecialization:
4879 // C++0x [temp.explicit]p4:
4880 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004881 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004882 // specialization for that template, the explicit instantiation has no
4883 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004884 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004885 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004886
Douglas Gregor454885e2009-10-15 15:54:05 +00004887 case TSK_ExplicitInstantiationDefinition:
4888 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004889 // If an entity is the subject of both an explicit instantiation
4890 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004891 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004892 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004893 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004894 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004895 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004896 assert(PrevPointOfInstantiation.isValid() &&
4897 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004898 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004899 return false;
4900 }
4901 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004902
Douglas Gregor454885e2009-10-15 15:54:05 +00004903 case TSK_ExplicitInstantiationDefinition:
4904 switch (PrevTSK) {
4905 case TSK_Undeclared:
4906 case TSK_ImplicitInstantiation:
4907 // We're explicitly instantiating something that may have already been
4908 // implicitly instantiated; that's fine.
4909 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004910
Douglas Gregor454885e2009-10-15 15:54:05 +00004911 case TSK_ExplicitSpecialization:
4912 // C++ DR 259, C++0x [temp.explicit]p4:
4913 // For a given set of template parameters, if an explicit
4914 // instantiation of a template appears after a declaration of
4915 // an explicit specialization for that template, the explicit
4916 // instantiation has no effect.
4917 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004918 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004919 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004920 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004921 if (!getLangOptions().CPlusPlus0x) {
4922 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004923 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004924 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004925 diag::note_previous_template_specialization);
4926 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004927 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004928 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004929
Douglas Gregor454885e2009-10-15 15:54:05 +00004930 case TSK_ExplicitInstantiationDeclaration:
4931 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004932 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004933 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004934
Douglas Gregor454885e2009-10-15 15:54:05 +00004935 case TSK_ExplicitInstantiationDefinition:
4936 // C++0x [temp.spec]p5:
4937 // For a given template and a given set of template-arguments,
4938 // - an explicit instantiation definition shall appear at most once
4939 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004940 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004941 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004942 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004943 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004944 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004945 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004946 }
4947 break;
4948 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004949
Douglas Gregor454885e2009-10-15 15:54:05 +00004950 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004951
Douglas Gregor454885e2009-10-15 15:54:05 +00004952 return false;
4953}
4954
John McCallaf2094e2010-04-08 09:05:18 +00004955/// \brief Perform semantic analysis for the given dependent function
4956/// template specialization. The only possible way to get a dependent
4957/// function template specialization is with a friend declaration,
4958/// like so:
4959///
4960/// template <class T> void foo(T);
4961/// template <class T> class A {
4962/// friend void foo<>(T);
4963/// };
4964///
4965/// There really isn't any useful analysis we can do here, so we
4966/// just store the information.
4967bool
4968Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
4969 const TemplateArgumentListInfo &ExplicitTemplateArgs,
4970 LookupResult &Previous) {
4971 // Remove anything from Previous that isn't a function template in
4972 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004973 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00004974 LookupResult::Filter F = Previous.makeFilter();
4975 while (F.hasNext()) {
4976 NamedDecl *D = F.next()->getUnderlyingDecl();
4977 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00004978 !FDLookupContext->InEnclosingNamespaceSetOf(
4979 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00004980 F.erase();
4981 }
4982 F.done();
4983
4984 // Should this be diagnosed here?
4985 if (Previous.empty()) return true;
4986
4987 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
4988 ExplicitTemplateArgs);
4989 return false;
4990}
4991
Abramo Bagnarae03db982010-05-20 15:32:11 +00004992/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004993/// specialization.
4994///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004995/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004996/// explicit function template specialization. On successful completion,
4997/// the function declaration \p FD will become a function template
4998/// specialization.
4999///
5000/// \param FD the function declaration, which will be updated to become a
5001/// function template specialization.
5002///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005003/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5004/// if any. Note that this may be valid info even when 0 arguments are
5005/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5006/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005007///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005008/// \param PrevDecl the set of declarations that may be specialized by
5009/// this function specialization.
5010bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005011Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005012 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005013 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005014 // The set of function template specializations that could match this
5015 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005016 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005017
Sebastian Redl7a126a42010-08-31 00:36:30 +00005018 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005019 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5020 I != E; ++I) {
5021 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5022 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005023 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005024 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005025 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5026 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005027 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005028
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005029 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005030 // A trailing template-argument can be left unspecified in the
5031 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005032 // provided it can be deduced from the function argument type.
5033 // Perform template argument deduction to determine whether we may be
5034 // specializing this template.
5035 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005036 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005037 FunctionDecl *Specialization = 0;
5038 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005039 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005040 FD->getType(),
5041 Specialization,
5042 Info)) {
5043 // FIXME: Template argument deduction failed; record why it failed, so
5044 // that we can provide nifty diagnostics.
5045 (void)TDK;
5046 continue;
5047 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005048
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005049 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005050 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005051 }
5052 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005053
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005054 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005055 UnresolvedSetIterator Result
5056 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005057 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005058 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005059 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005060 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005061 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005062 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005063 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005064 return true;
John McCallc373d482010-01-27 01:50:18 +00005065
5066 // Ignore access information; it doesn't figure into redeclaration checking.
5067 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005068
5069 FunctionTemplateSpecializationInfo *SpecInfo
5070 = Specialization->getTemplateSpecializationInfo();
5071 assert(SpecInfo && "Function template specialization info missing?");
5072 {
5073 // Note: do not overwrite location info if previous template
5074 // specialization kind was explicit.
5075 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5076 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5077 Specialization->setLocation(FD->getLocation());
5078 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005079
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005080 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005081 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005082
5083 // If this is a friend declaration, then we're not really declaring
5084 // an explicit specialization.
5085 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005086
Douglas Gregord5cb8762009-10-07 00:13:32 +00005087 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005088 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005089 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005090 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005091 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005092 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005093 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005094
5095 // C++ [temp.expl.spec]p6:
5096 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005097 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005098 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005099 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005100 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005101 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005102 if (!isFriend &&
5103 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005104 TSK_ExplicitSpecialization,
5105 Specialization,
5106 SpecInfo->getTemplateSpecializationKind(),
5107 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005108 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005109 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005110
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005111 // Mark the prior declaration as an explicit specialization, so that later
5112 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005113 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005114 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005115 MarkUnusedFileScopedDecl(Specialization);
5116 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005117
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005118 // Turn the given function declaration into a function template
5119 // specialization, with the template arguments from the previous
5120 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005121 // Take copies of (semantic and syntactic) template argument lists.
5122 const TemplateArgumentList* TemplArgs = new (Context)
5123 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5124 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5125 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005126 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005127 TemplArgs, /*InsertPos=*/0,
5128 SpecInfo->getTemplateSpecializationKind(),
5129 TemplArgsAsWritten);
5130
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005131 // The "previous declaration" for this function template specialization is
5132 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005133 Previous.clear();
5134 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005135 return false;
5136}
5137
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005138/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005139/// specialization.
5140///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005141/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005142/// explicit member function specialization. On successful completion,
5143/// the function declaration \p FD will become a member function
5144/// specialization.
5145///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005146/// \param Member the member declaration, which will be updated to become a
5147/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005148///
John McCall68263142009-11-18 22:49:29 +00005149/// \param Previous the set of declarations, one of which may be specialized
5150/// by this function specialization; the set will be modified to contain the
5151/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005152bool
John McCall68263142009-11-18 22:49:29 +00005153Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005154 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005155
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005156 // Try to find the member we are instantiating.
5157 NamedDecl *Instantiation = 0;
5158 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005159 MemberSpecializationInfo *MSInfo = 0;
5160
John McCall68263142009-11-18 22:49:29 +00005161 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005162 // Nowhere to look anyway.
5163 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005164 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5165 I != E; ++I) {
5166 NamedDecl *D = (*I)->getUnderlyingDecl();
5167 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005168 if (Context.hasSameType(Function->getType(), Method->getType())) {
5169 Instantiation = Method;
5170 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005171 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005172 break;
5173 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005174 }
5175 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005176 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005177 VarDecl *PrevVar;
5178 if (Previous.isSingleResult() &&
5179 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005180 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005181 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005182 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005183 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005184 }
5185 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005186 CXXRecordDecl *PrevRecord;
5187 if (Previous.isSingleResult() &&
5188 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5189 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005190 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005191 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005192 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005193 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005194
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005195 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005196 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005197 // specializations are always out-of-line, the caller will complain about
5198 // this mismatch later.
5199 return false;
5200 }
John McCall77e8b112010-04-13 20:37:33 +00005201
5202 // If this is a friend, just bail out here before we start turning
5203 // things into explicit specializations.
5204 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5205 // Preserve instantiation information.
5206 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5207 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5208 cast<CXXMethodDecl>(InstantiatedFrom),
5209 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5210 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5211 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5212 cast<CXXRecordDecl>(InstantiatedFrom),
5213 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5214 }
5215
5216 Previous.clear();
5217 Previous.addDecl(Instantiation);
5218 return false;
5219 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005220
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005221 // Make sure that this is a specialization of a member.
5222 if (!InstantiatedFrom) {
5223 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5224 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005225 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5226 return true;
5227 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005228
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005229 // C++ [temp.expl.spec]p6:
5230 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005231 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005232 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005233 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005234 // use occurs; no diagnostic is required.
5235 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005236
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005237 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005238 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5239 TSK_ExplicitSpecialization,
5240 Instantiation,
5241 MSInfo->getTemplateSpecializationKind(),
5242 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005243 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005244 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005245
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005246 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005247 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005248 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005249 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005250 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005251 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005252
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005253 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005254 // the original declaration to note that it is an explicit specialization
5255 // (if it was previously an implicit instantiation). This latter step
5256 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005257 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005258 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5259 if (InstantiationFunction->getTemplateSpecializationKind() ==
5260 TSK_ImplicitInstantiation) {
5261 InstantiationFunction->setTemplateSpecializationKind(
5262 TSK_ExplicitSpecialization);
5263 InstantiationFunction->setLocation(Member->getLocation());
5264 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005265
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005266 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5267 cast<CXXMethodDecl>(InstantiatedFrom),
5268 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005269 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005270 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005271 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5272 if (InstantiationVar->getTemplateSpecializationKind() ==
5273 TSK_ImplicitInstantiation) {
5274 InstantiationVar->setTemplateSpecializationKind(
5275 TSK_ExplicitSpecialization);
5276 InstantiationVar->setLocation(Member->getLocation());
5277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005278
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005279 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5280 cast<VarDecl>(InstantiatedFrom),
5281 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005282 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005283 } else {
5284 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005285 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5286 if (InstantiationClass->getTemplateSpecializationKind() ==
5287 TSK_ImplicitInstantiation) {
5288 InstantiationClass->setTemplateSpecializationKind(
5289 TSK_ExplicitSpecialization);
5290 InstantiationClass->setLocation(Member->getLocation());
5291 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005292
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005293 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005294 cast<CXXRecordDecl>(InstantiatedFrom),
5295 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005296 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005297
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005298 // Save the caller the trouble of having to figure out which declaration
5299 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005300 Previous.clear();
5301 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005302 return false;
5303}
5304
Douglas Gregor558c0322009-10-14 23:41:34 +00005305/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005306///
5307/// \returns true if a serious error occurs, false otherwise.
5308static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005309 SourceLocation InstLoc,
5310 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005311 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5312 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005313
Douglas Gregor669eed82010-07-13 00:10:04 +00005314 if (CurContext->isRecord()) {
5315 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5316 << D;
5317 return true;
5318 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005319
Douglas Gregor558c0322009-10-14 23:41:34 +00005320 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005321 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005322 // template.
5323 //
5324 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005325 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005326 !CurContext->Encloses(OrigContext)) {
5327 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005328 S.Diag(InstLoc,
5329 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005330 diag::err_explicit_instantiation_out_of_scope
5331 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005332 << D << NS;
5333 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005334 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005335 S.getLangOptions().CPlusPlus0x?
5336 diag::err_explicit_instantiation_must_be_global
5337 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005338 << D;
5339 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005340 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005341 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005342
Douglas Gregor558c0322009-10-14 23:41:34 +00005343 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005344 // If the name declared in the explicit instantiation is an unqualified
5345 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005346 // its template is declared or, if that namespace is inline (7.3.1), any
5347 // namespace from its enclosing namespace set.
5348 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005349 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005350
5351 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005352 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005353
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005354 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005355 S.getLangOptions().CPlusPlus0x?
5356 diag::err_explicit_instantiation_unqualified_wrong_namespace
5357 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005358 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005359 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005360 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005361}
5362
5363/// \brief Determine whether the given scope specifier has a template-id in it.
5364static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5365 if (!SS.isSet())
5366 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005367
Douglas Gregor558c0322009-10-14 23:41:34 +00005368 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005369 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005370 // or a static data member of a class template specialization, the name of
5371 // the class template specialization in the qualified-id for the member
5372 // name shall be a simple-template-id.
5373 //
5374 // C++98 has the same restriction, just worded differently.
5375 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5376 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005377 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005378 if (isa<TemplateSpecializationType>(T))
5379 return true;
5380
5381 return false;
5382}
5383
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005384// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005385DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005386Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005387 SourceLocation ExternLoc,
5388 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005389 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005390 SourceLocation KWLoc,
5391 const CXXScopeSpec &SS,
5392 TemplateTy TemplateD,
5393 SourceLocation TemplateNameLoc,
5394 SourceLocation LAngleLoc,
5395 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005396 SourceLocation RAngleLoc,
5397 AttributeList *Attr) {
5398 // Find the class template we're specializing
5399 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005400 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005401 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5402
5403 // Check that the specialization uses the same tag kind as the
5404 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005405 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5406 assert(Kind != TTK_Enum &&
5407 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005408 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005409 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005410 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005411 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005412 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005413 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005414 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005415 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005416 diag::note_previous_use);
5417 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5418 }
5419
Douglas Gregor558c0322009-10-14 23:41:34 +00005420 // C++0x [temp.explicit]p2:
5421 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005422 // definition and an explicit instantiation declaration. An explicit
5423 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005424 TemplateSpecializationKind TSK
5425 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5426 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005427
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005428 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005429 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005430 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005431
5432 // Check that the template argument list is well-formed for this
5433 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005434 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005435 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5436 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005437 return true;
5438
Douglas Gregor910f8002010-11-07 23:05:16 +00005439 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005440 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005441
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005442 // Find the class template specialization declaration that
5443 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005444 void *InsertPos = 0;
5445 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005446 = ClassTemplate->findSpecialization(Converted.data(),
5447 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005448
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005449 TemplateSpecializationKind PrevDecl_TSK
5450 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5451
Douglas Gregord5cb8762009-10-07 00:13:32 +00005452 // C++0x [temp.explicit]p2:
5453 // [...] An explicit instantiation shall appear in an enclosing
5454 // namespace of its template. [...]
5455 //
5456 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005457 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5458 SS.isSet()))
5459 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005460
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005461 ClassTemplateSpecializationDecl *Specialization = 0;
5462
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005463 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005464 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005465 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005466 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005467 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005468 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005469 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005470
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005471 // Even though HasNoEffect == true means that this explicit instantiation
5472 // has no effect on semantics, we go on to put its syntax in the AST.
5473
5474 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5475 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005476 // Since the only prior class template specialization with these
5477 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005478 // declaration node as our own, updating the source location
5479 // for the template name to reflect our new declaration.
5480 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005481 Specialization = PrevDecl;
5482 Specialization->setLocation(TemplateNameLoc);
5483 PrevDecl = 0;
5484 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005485 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005486
Douglas Gregor52604ab2009-09-11 21:19:12 +00005487 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005488 // Create a new class template specialization declaration node for
5489 // this explicit specialization.
5490 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005491 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005492 ClassTemplate->getDeclContext(),
5493 TemplateNameLoc,
5494 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005495 Converted.data(),
5496 Converted.size(),
5497 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005498 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005499
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005500 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005501 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005502 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005503 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005504 }
5505
5506 // Build the fully-sugared type for this explicit instantiation as
5507 // the user wrote in the explicit instantiation itself. This means
5508 // that we'll pretty-print the type retrieved from the
5509 // specialization's declaration the way that the user actually wrote
5510 // the explicit instantiation, rather than formatting the name based
5511 // on the "canonical" representation used to store the template
5512 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005513 TypeSourceInfo *WrittenTy
5514 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5515 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005516 Context.getTypeDeclType(Specialization));
5517 Specialization->setTypeAsWritten(WrittenTy);
5518 TemplateArgsIn.release();
5519
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005520 // Set source locations for keywords.
5521 Specialization->setExternLoc(ExternLoc);
5522 Specialization->setTemplateKeywordLoc(TemplateLoc);
5523
5524 // Add the explicit instantiation into its lexical context. However,
5525 // since explicit instantiations are never found by name lookup, we
5526 // just put it into the declaration context directly.
5527 Specialization->setLexicalDeclContext(CurContext);
5528 CurContext->addDecl(Specialization);
5529
5530 // Syntax is now OK, so return if it has no other effect on semantics.
5531 if (HasNoEffect) {
5532 // Set the template specialization kind.
5533 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005534 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005535 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005536
5537 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005538 // A definition of a class template or class member template
5539 // shall be in scope at the point of the explicit instantiation of
5540 // the class template or class member template.
5541 //
5542 // This check comes when we actually try to perform the
5543 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005544 ClassTemplateSpecializationDecl *Def
5545 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005546 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005547 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005548 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005549 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005550 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005551 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5552 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005553
Douglas Gregor0d035142009-10-27 18:42:08 +00005554 // Instantiate the members of this class template specialization.
5555 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005556 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005557 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005558 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5559
5560 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5561 // TSK_ExplicitInstantiationDefinition
5562 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5563 TSK == TSK_ExplicitInstantiationDefinition)
5564 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005565
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005566 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005567 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005568
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005569 // Set the template specialization kind.
5570 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005571 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005572}
5573
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005574// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005575DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005576Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005577 SourceLocation ExternLoc,
5578 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005579 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005580 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005581 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005582 IdentifierInfo *Name,
5583 SourceLocation NameLoc,
5584 AttributeList *Attr) {
5585
Douglas Gregor402abb52009-05-28 23:31:59 +00005586 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005587 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005588 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005589 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5590 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005591 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005592 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005593 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5594
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005595 if (!TagD)
5596 return true;
5597
John McCalld226f652010-08-21 09:40:31 +00005598 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005599 if (Tag->isEnum()) {
5600 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5601 << Context.getTypeDeclType(Tag);
5602 return true;
5603 }
5604
Douglas Gregord0c87372009-05-27 17:30:49 +00005605 if (Tag->isInvalidDecl())
5606 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005607
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005608 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5609 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5610 if (!Pattern) {
5611 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5612 << Context.getTypeDeclType(Record);
5613 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5614 return true;
5615 }
5616
Douglas Gregor558c0322009-10-14 23:41:34 +00005617 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005618 // If the explicit instantiation is for a class or member class, the
5619 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005620 // simple-template-id.
5621 //
5622 // C++98 has the same restriction, just worded differently.
5623 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005624 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005625 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005626
Douglas Gregor558c0322009-10-14 23:41:34 +00005627 // C++0x [temp.explicit]p2:
5628 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005629 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005630 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005631 TemplateSpecializationKind TSK
5632 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5633 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005634
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005635 // C++0x [temp.explicit]p2:
5636 // [...] An explicit instantiation shall appear in an enclosing
5637 // namespace of its template. [...]
5638 //
5639 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005640 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005641
Douglas Gregor454885e2009-10-15 15:54:05 +00005642 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005643 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005644 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005645 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005646 PrevDecl = Record;
5647 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005648 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005649 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005650 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005651 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005652 PrevDecl,
5653 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005654 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005655 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005656 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005657 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005658 return TagD;
5659 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005660
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005661 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005662 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005663 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005664 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005665 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005666 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005667 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005668 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005669 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005670 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5671 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005672 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5673 << Pattern;
5674 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005675 } else {
5676 if (InstantiateClass(NameLoc, Record, Def,
5677 getTemplateInstantiationArgs(Record),
5678 TSK))
5679 return true;
5680
Douglas Gregor952b0172010-02-11 01:04:33 +00005681 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005682 if (!RecordDef)
5683 return true;
5684 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005685 }
5686
Douglas Gregor0d035142009-10-27 18:42:08 +00005687 // Instantiate all of the members of the class.
5688 InstantiateClassMembers(NameLoc, RecordDef,
5689 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005690
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005691 if (TSK == TSK_ExplicitInstantiationDefinition)
5692 MarkVTableUsed(NameLoc, RecordDef, true);
5693
Mike Stump390b4cc2009-05-16 07:39:55 +00005694 // FIXME: We don't have any representation for explicit instantiations of
5695 // member classes. Such a representation is not needed for compilation, but it
5696 // should be available for clients that want to see all of the declarations in
5697 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005698 return TagD;
5699}
5700
John McCallf312b1e2010-08-26 23:41:50 +00005701DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5702 SourceLocation ExternLoc,
5703 SourceLocation TemplateLoc,
5704 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005705 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005706 // TODO: check if/when DNInfo should replace Name.
5707 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5708 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005709 if (!Name) {
5710 if (!D.isInvalidType())
5711 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5712 diag::err_explicit_instantiation_requires_name)
5713 << D.getDeclSpec().getSourceRange()
5714 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005715
Douglas Gregord5a423b2009-09-25 18:43:00 +00005716 return true;
5717 }
5718
5719 // The scope passed in may not be a decl scope. Zip up the scope tree until
5720 // we find one that is.
5721 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5722 (S->getFlags() & Scope::TemplateParamScope) != 0)
5723 S = S->getParent();
5724
5725 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005726 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5727 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005728 if (R.isNull())
5729 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005730
Douglas Gregord5a423b2009-09-25 18:43:00 +00005731 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5732 // Cannot explicitly instantiate a typedef.
5733 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5734 << Name;
5735 return true;
5736 }
5737
Douglas Gregor663b5a02009-10-14 20:14:33 +00005738 // C++0x [temp.explicit]p1:
5739 // [...] An explicit instantiation of a function template shall not use the
5740 // inline or constexpr specifiers.
5741 // Presumably, this also applies to member functions of class templates as
5742 // well.
5743 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005744 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005745 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005746 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005747
Douglas Gregor663b5a02009-10-14 20:14:33 +00005748 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005749
Douglas Gregor558c0322009-10-14 23:41:34 +00005750 // C++0x [temp.explicit]p2:
5751 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005752 // definition and an explicit instantiation declaration. An explicit
5753 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005754 TemplateSpecializationKind TSK
5755 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5756 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005757
Abramo Bagnara25777432010-08-11 22:01:17 +00005758 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005759 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005760
5761 if (!R->isFunctionType()) {
5762 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005763 // A [...] static data member of a class template can be explicitly
5764 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005765 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005766 if (Previous.isAmbiguous())
5767 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005768
John McCall1bcee0a2009-12-02 08:25:40 +00005769 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005770 if (!Prev || !Prev->isStaticDataMember()) {
5771 // We expect to see a data data member here.
5772 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5773 << Name;
5774 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5775 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005776 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005777 return true;
5778 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005779
Douglas Gregord5a423b2009-09-25 18:43:00 +00005780 if (!Prev->getInstantiatedFromStaticDataMember()) {
5781 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005782 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005783 diag::err_explicit_instantiation_data_member_not_instantiated)
5784 << Prev;
5785 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5786 // FIXME: Can we provide a note showing where this was declared?
5787 return true;
5788 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005789
Douglas Gregor558c0322009-10-14 23:41:34 +00005790 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005791 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005792 // or a static data member of a class template specialization, the name of
5793 // the class template specialization in the qualified-id for the member
5794 // name shall be a simple-template-id.
5795 //
5796 // C++98 has the same restriction, just worded differently.
5797 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005798 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005799 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005800 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005801
Douglas Gregor558c0322009-10-14 23:41:34 +00005802 // Check the scope of this explicit instantiation.
5803 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005804
Douglas Gregor454885e2009-10-15 15:54:05 +00005805 // Verify that it is okay to explicitly instantiate here.
5806 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5807 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005808 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005809 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005810 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005811 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005812 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005813 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005814 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005815 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005816
Douglas Gregord5a423b2009-09-25 18:43:00 +00005817 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005818 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005819 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005820 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005821
Douglas Gregord5a423b2009-09-25 18:43:00 +00005822 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005823 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005824 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005825
5826 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005827 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005828 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005829 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005830 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5831 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005832 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5833 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005834 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5835 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005836 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005837 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005838 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005839 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005840 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005841
Douglas Gregord5a423b2009-09-25 18:43:00 +00005842 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005843 // A [...] function [...] can be explicitly instantiated from its template.
5844 // A member function [...] of a class template can be explicitly
5845 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005846 // template.
John McCallc373d482010-01-27 01:50:18 +00005847 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005848 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5849 P != PEnd; ++P) {
5850 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005851 if (!HasExplicitTemplateArgs) {
5852 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5853 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5854 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005855
John McCallc373d482010-01-27 01:50:18 +00005856 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005857 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5858 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005859 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005860 }
5861 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005862
Douglas Gregord5a423b2009-09-25 18:43:00 +00005863 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5864 if (!FunTmpl)
5865 continue;
5866
John McCall5769d612010-02-08 23:07:23 +00005867 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005868 FunctionDecl *Specialization = 0;
5869 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005870 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005871 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005872 R, Specialization, Info)) {
5873 // FIXME: Keep track of almost-matches?
5874 (void)TDK;
5875 continue;
5876 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005877
John McCallc373d482010-01-27 01:50:18 +00005878 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005879 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005880
Douglas Gregord5a423b2009-09-25 18:43:00 +00005881 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005882 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005883 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005884 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005885 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5886 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5887 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005888
John McCallc373d482010-01-27 01:50:18 +00005889 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005890 return true;
John McCallc373d482010-01-27 01:50:18 +00005891
5892 // Ignore access control bits, we don't need them for redeclaration checking.
5893 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005894
Douglas Gregor0a897e32009-10-15 17:21:20 +00005895 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005896 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005897 diag::err_explicit_instantiation_member_function_not_instantiated)
5898 << Specialization
5899 << (Specialization->getTemplateSpecializationKind() ==
5900 TSK_ExplicitSpecialization);
5901 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5902 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005903 }
5904
Douglas Gregor0a897e32009-10-15 17:21:20 +00005905 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005906 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5907 PrevDecl = Specialization;
5908
Douglas Gregor0a897e32009-10-15 17:21:20 +00005909 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005910 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005911 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005912 PrevDecl,
5913 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005914 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005915 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005916 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005917
Douglas Gregor0a897e32009-10-15 17:21:20 +00005918 // FIXME: We may still want to build some representation of this
5919 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005920 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005921 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005922 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005923
5924 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005925
Douglas Gregor0a897e32009-10-15 17:21:20 +00005926 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005927 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005928
Douglas Gregor558c0322009-10-14 23:41:34 +00005929 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005930 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005931 // or a static data member of a class template specialization, the name of
5932 // the class template specialization in the qualified-id for the member
5933 // name shall be a simple-template-id.
5934 //
5935 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005936 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005937 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005938 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005939 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005940 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005941 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005942 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005943
Douglas Gregor558c0322009-10-14 23:41:34 +00005944 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005945 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005946 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005947 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005948 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005949
Douglas Gregord5a423b2009-09-25 18:43:00 +00005950 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005951 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005952}
5953
John McCallf312b1e2010-08-26 23:41:50 +00005954TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005955Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5956 const CXXScopeSpec &SS, IdentifierInfo *Name,
5957 SourceLocation TagLoc, SourceLocation NameLoc) {
5958 // This has to hold, because SS is expected to be defined.
5959 assert(Name && "Expected a name in a dependent tag");
5960
5961 NestedNameSpecifier *NNS
5962 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5963 if (!NNS)
5964 return true;
5965
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005966 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005967
Douglas Gregor48c89f42010-04-24 16:38:41 +00005968 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
5969 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005970 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00005971 return true;
5972 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005973
Douglas Gregor059101f2011-03-02 00:47:37 +00005974 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005975 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00005976 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
5977
5978 // Create type-source location information for this type.
5979 TypeLocBuilder TLB;
5980 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
5981 TL.setKeywordLoc(TagLoc);
5982 TL.setQualifierLoc(SS.getWithLocInContext(Context));
5983 TL.setNameLoc(NameLoc);
5984 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00005985}
5986
John McCallf312b1e2010-08-26 23:41:50 +00005987TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005988Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5989 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005990 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00005991 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00005992 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00005993
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005994 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5995 !getLangOptions().CPlusPlus0x)
5996 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005997 << FixItHint::CreateRemoval(TypenameLoc);
5998
Douglas Gregor2494dd02011-03-01 01:34:45 +00005999 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006000 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6001 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006002 if (T.isNull())
6003 return true;
John McCall63b43852010-04-29 23:50:39 +00006004
6005 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6006 if (isa<DependentNameType>(T)) {
6007 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006008 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006009 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006010 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006011 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006012 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006013 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006014 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006015 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006016 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006017
John McCallb3d87482010-08-24 05:47:05 +00006018 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006019}
6020
John McCallf312b1e2010-08-26 23:41:50 +00006021TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006022Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6023 const CXXScopeSpec &SS,
6024 SourceLocation TemplateLoc,
6025 TemplateTy TemplateIn,
6026 SourceLocation TemplateNameLoc,
6027 SourceLocation LAngleLoc,
6028 ASTTemplateArgsPtr TemplateArgsIn,
6029 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006030 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6031 !getLangOptions().CPlusPlus0x)
6032 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006033 << FixItHint::CreateRemoval(TypenameLoc);
6034
6035 // Translate the parser's template argument list in our AST format.
6036 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6037 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6038
6039 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006040 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6041 // Construct a dependent template specialization type.
6042 assert(DTN && "dependent template has non-dependent name?");
6043 assert(DTN->getQualifier()
6044 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6045 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6046 DTN->getQualifier(),
6047 DTN->getIdentifier(),
6048 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006049
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006050 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006051 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006052 DependentTemplateSpecializationTypeLoc SpecTL
6053 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006054 SpecTL.setLAngleLoc(LAngleLoc);
6055 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006056 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006057 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006058 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006059 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6060 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006061 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006062 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006063
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006064 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6065 if (T.isNull())
6066 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006067
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006068 // Provide source-location information for the template specialization
6069 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006070 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006071 TemplateSpecializationTypeLoc SpecTL
6072 = Builder.push<TemplateSpecializationTypeLoc>(T);
6073
6074 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006075 SpecTL.setLAngleLoc(LAngleLoc);
6076 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006077 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006078 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6079 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6080
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006081 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6082 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6083 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006084 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6085
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006086 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6087 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006088}
6089
Douglas Gregora02411e2011-02-27 22:46:49 +00006090
Douglas Gregord57959a2009-03-27 23:10:48 +00006091/// \brief Build the type that describes a C++ typename specifier,
6092/// e.g., "typename T::type".
6093QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006094Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6095 SourceLocation KeywordLoc,
6096 NestedNameSpecifierLoc QualifierLoc,
6097 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006098 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006099 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006100 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006101
John McCall77bb1aa2010-05-01 00:40:08 +00006102 DeclContext *Ctx = computeDeclContext(SS);
6103 if (!Ctx) {
6104 // If the nested-name-specifier is dependent and couldn't be
6105 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006106 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6107 return Context.getDependentNameType(Keyword,
6108 QualifierLoc.getNestedNameSpecifier(),
6109 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006110 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006111
John McCall77bb1aa2010-05-01 00:40:08 +00006112 // If the nested-name-specifier refers to the current instantiation,
6113 // the "typename" keyword itself is superfluous. In C++03, the
6114 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6115 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006116 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006117
John McCall77bb1aa2010-05-01 00:40:08 +00006118 if (RequireCompleteDeclContext(SS, Ctx))
6119 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006120
6121 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006122 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006123 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006124 unsigned DiagID = 0;
6125 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006126 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006127 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006128 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006129 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006130
6131 case LookupResult::FoundUnresolvedValue: {
6132 // We found a using declaration that is a value. Most likely, the using
6133 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006134 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006135 IILoc);
6136 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6137 << Name << Ctx << FullRange;
6138 if (UnresolvedUsingValueDecl *Using
6139 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006140 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006141 Diag(Loc, diag::note_using_value_decl_missing_typename)
6142 << FixItHint::CreateInsertion(Loc, "typename ");
6143 }
6144 }
6145 // Fall through to create a dependent typename type, from which we can recover
6146 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006147
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006148 case LookupResult::NotFoundInCurrentInstantiation:
6149 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006150 return Context.getDependentNameType(Keyword,
6151 QualifierLoc.getNestedNameSpecifier(),
6152 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006153
6154 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006155 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006156 // We found a type. Build an ElaboratedType, since the
6157 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006158 return Context.getElaboratedType(ETK_Typename,
6159 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006160 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006161 }
6162
6163 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006164 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006165 break;
6166
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006167
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006168 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006169 return QualType();
6170
Douglas Gregord57959a2009-03-27 23:10:48 +00006171 case LookupResult::FoundOverloaded:
6172 DiagID = diag::err_typename_nested_not_type;
6173 Referenced = *Result.begin();
6174 break;
6175
John McCall6e247262009-10-10 05:48:19 +00006176 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006177 return QualType();
6178 }
6179
6180 // If we get here, it's because name lookup did not find a
6181 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006182 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006183 IILoc);
6184 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006185 if (Referenced)
6186 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6187 << Name;
6188 return QualType();
6189}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006190
6191namespace {
6192 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006193 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006194 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006195 SourceLocation Loc;
6196 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006197
Douglas Gregor4a959d82009-08-06 16:20:37 +00006198 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006199 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006200
Mike Stump1eb44332009-09-09 15:08:12 +00006201 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006202 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006203 DeclarationName Entity)
6204 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006205 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006206
6207 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006208 /// transformed.
6209 ///
6210 /// For the purposes of type reconstruction, a type has already been
6211 /// transformed if it is NULL or if it is not dependent.
6212 bool AlreadyTransformed(QualType T) {
6213 return T.isNull() || !T->isDependentType();
6214 }
Mike Stump1eb44332009-09-09 15:08:12 +00006215
6216 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006217 /// rebuilt.
6218 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006219
Douglas Gregor4a959d82009-08-06 16:20:37 +00006220 /// \brief Returns the name of the entity whose type is being rebuilt.
6221 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006222
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006223 /// \brief Sets the "base" location and entity when that
6224 /// information is known based on another transformation.
6225 void setBase(SourceLocation Loc, DeclarationName Entity) {
6226 this->Loc = Loc;
6227 this->Entity = Entity;
6228 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006229 };
6230}
6231
Douglas Gregor4a959d82009-08-06 16:20:37 +00006232/// \brief Rebuilds a type within the context of the current instantiation.
6233///
Mike Stump1eb44332009-09-09 15:08:12 +00006234/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006235/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006236/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006237/// partial specialization thereof). This routine will rebuild that type now
6238/// that we have entered the declarator's scope, which may produce different
6239/// canonical types, e.g.,
6240///
6241/// \code
6242/// template<typename T>
6243/// struct X {
6244/// typedef T* pointer;
6245/// pointer data();
6246/// };
6247///
6248/// template<typename T>
6249/// typename X<T>::pointer X<T>::data() { ... }
6250/// \endcode
6251///
Douglas Gregor4714c122010-03-31 17:34:00 +00006252/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006253/// since we do not know that we can look into X<T> when we parsed the type.
6254/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006255/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006256/// as the canonical type of T*, allowing the return types of the out-of-line
6257/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006258TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6259 SourceLocation Loc,
6260 DeclarationName Name) {
6261 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006262 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006263
Douglas Gregor4a959d82009-08-06 16:20:37 +00006264 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6265 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006266}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006267
John McCall60d7b3a2010-08-24 06:29:42 +00006268ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006269 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6270 DeclarationName());
6271 return Rebuilder.TransformExpr(E);
6272}
6273
John McCall63b43852010-04-29 23:50:39 +00006274bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006275 if (SS.isInvalid())
6276 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006277
Douglas Gregor7e384942011-02-25 16:07:42 +00006278 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006279 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6280 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006281 NestedNameSpecifierLoc Rebuilt
6282 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6283 if (!Rebuilt)
6284 return true;
John McCall63b43852010-04-29 23:50:39 +00006285
Douglas Gregor7e384942011-02-25 16:07:42 +00006286 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006287 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006288}
6289
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006290/// \brief Produces a formatted string that describes the binding of
6291/// template parameters to template arguments.
6292std::string
6293Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6294 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006295 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006296}
6297
6298std::string
6299Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6300 const TemplateArgument *Args,
6301 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006302 llvm::SmallString<128> Str;
6303 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006304
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006305 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006306 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006307
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006308 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006309 if (I >= NumArgs)
6310 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006311
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006312 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006313 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006314 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006315 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006316
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006317 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006318 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006319 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006320 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006321 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006322
Douglas Gregor87dd6972010-12-20 16:52:59 +00006323 Out << " = ";
6324 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006325 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006326
6327 Out << ']';
6328 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006329}