blob: 68cb6f4dfe6d18b36e064ad6fbed2f556837d462 [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(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000528 KeyLoc, Loc, Depth, Position, ParamName,
529 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000530 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000531 if (Invalid)
532 Param->setInvalidDecl();
533
534 if (ParamName) {
535 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000536 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000537 IdResolver.AddDecl(Param);
538 }
539
Douglas Gregor61c4d282011-01-05 15:48:55 +0000540 // C++0x [temp.param]p9:
541 // A default template-argument may be specified for any kind of
542 // template-parameter that is not a template parameter pack.
543 if (DefaultArg && Ellipsis) {
544 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
545 DefaultArg = ParsedType();
546 }
547
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000548 // Handle the default argument, if provided.
549 if (DefaultArg) {
550 TypeSourceInfo *DefaultTInfo;
551 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000552
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000553 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000554
Douglas Gregor6f526752010-12-16 08:48:57 +0000555 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000556 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000557 UPPC_DefaultArgument))
558 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000559
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000560 // Check the template argument itself.
561 if (CheckTemplateArgument(Param, DefaultTInfo)) {
562 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000563 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000564 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000565
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000566 Param->setDefaultArgument(DefaultTInfo, false);
567 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000568
John McCalld226f652010-08-21 09:40:31 +0000569 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000570}
571
Douglas Gregor2943aed2009-03-03 04:44:36 +0000572/// \brief Check that the type of a non-type template parameter is
573/// well-formed.
574///
575/// \returns the (possibly-promoted) parameter type if valid;
576/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000577QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000578Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000579 // We don't allow variably-modified types as the type of non-type template
580 // parameters.
581 if (T->isVariablyModifiedType()) {
582 Diag(Loc, diag::err_variably_modified_nontype_template_param)
583 << T;
584 return QualType();
585 }
586
Douglas Gregor2943aed2009-03-03 04:44:36 +0000587 // C++ [temp.param]p4:
588 //
589 // A non-type template-parameter shall have one of the following
590 // (optionally cv-qualified) types:
591 //
592 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000593 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000594 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000595 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000596 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000597 T->isReferenceType() ||
598 // -- pointer to member.
599 T->isMemberPointerType() ||
600 // If T is a dependent type, we can't do the check now, so we
601 // assume that it is well-formed.
602 T->isDependentType())
603 return T;
604 // C++ [temp.param]p8:
605 //
606 // A non-type template-parameter of type "array of T" or
607 // "function returning T" is adjusted to be of type "pointer to
608 // T" or "pointer to function returning T", respectively.
609 else if (T->isArrayType())
610 // FIXME: Keep the type prior to promotion?
611 return Context.getArrayDecayedType(T);
612 else if (T->isFunctionType())
613 // FIXME: Keep the type prior to promotion?
614 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000615
Douglas Gregor2943aed2009-03-03 04:44:36 +0000616 Diag(Loc, diag::err_template_nontype_parm_bad_type)
617 << T;
618
619 return QualType();
620}
621
John McCalld226f652010-08-21 09:40:31 +0000622Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
623 unsigned Depth,
624 unsigned Position,
625 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000626 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000627 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
628 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000629
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000630 assert(S->isTemplateParamScope() &&
631 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000632 bool Invalid = false;
633
634 IdentifierInfo *ParamName = D.getIdentifier();
635 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000636 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000637 LookupOrdinaryName,
638 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000639 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000640 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000641 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000642 }
643
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000644 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
645 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000646 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000647 Invalid = true;
648 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000649
Douglas Gregor10738d32010-12-23 23:51:58 +0000650 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000651 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000652 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000653 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000654 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000655 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000656 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000657 Param->setAccess(AS_public);
658
Douglas Gregor72c3f312008-12-05 18:15:24 +0000659 if (Invalid)
660 Param->setInvalidDecl();
661
662 if (D.getIdentifier()) {
663 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000664 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000665 IdResolver.AddDecl(Param);
666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000667
Douglas Gregor61c4d282011-01-05 15:48:55 +0000668 // C++0x [temp.param]p9:
669 // A default template-argument may be specified for any kind of
670 // template-parameter that is not a template parameter pack.
671 if (Default && IsParameterPack) {
672 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
673 Default = 0;
674 }
675
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000676 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000677 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000678 // Check for unexpanded parameter packs.
679 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
680 return Param;
681
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000682 TemplateArgument Converted;
683 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
684 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000685 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000686 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000687
John McCall9ae2f072010-08-23 23:25:46 +0000688 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000689 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000690
John McCalld226f652010-08-21 09:40:31 +0000691 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000692}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000693
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000694/// ActOnTemplateTemplateParameter - Called when a C++ template template
695/// parameter (e.g. T in template <template <typename> class T> class array)
696/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000697Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
698 SourceLocation TmpLoc,
699 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000700 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000701 IdentifierInfo *Name,
702 SourceLocation NameLoc,
703 unsigned Depth,
704 unsigned Position,
705 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000706 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000707 assert(S->isTemplateParamScope() &&
708 "Template template parameter not in template parameter scope!");
709
710 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000711 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000712 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000713 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000714 NameLoc.isInvalid()? TmpLoc : NameLoc,
715 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000716 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000717 Param->setAccess(AS_public);
718
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000719 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000720 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000721 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000722 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000723 IdResolver.AddDecl(Param);
724 }
725
Douglas Gregor6f526752010-12-16 08:48:57 +0000726 if (Params->size() == 0) {
727 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
728 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
729 Param->setInvalidDecl();
730 }
731
Douglas Gregor61c4d282011-01-05 15:48:55 +0000732 // C++0x [temp.param]p9:
733 // A default template-argument may be specified for any kind of
734 // template-parameter that is not a template parameter pack.
735 if (IsParameterPack && !Default.isInvalid()) {
736 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
737 Default = ParsedTemplateArgument();
738 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000739
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000740 if (!Default.isInvalid()) {
741 // Check only that we have a template template argument. We don't want to
742 // try to check well-formedness now, because our template template parameter
743 // might have dependent types in its template parameters, which we wouldn't
744 // be able to match now.
745 //
746 // If none of the template template parameter's template arguments mention
747 // other template parameters, we could actually perform more checking here.
748 // However, it isn't worth doing.
749 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
750 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
751 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
752 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000753 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000754 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000755
Douglas Gregor6f526752010-12-16 08:48:57 +0000756 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000758 DefaultArg.getArgument().getAsTemplate(),
759 UPPC_DefaultArgument))
760 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000761
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000762 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000763 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000764
John McCalld226f652010-08-21 09:40:31 +0000765 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000766}
767
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000768/// ActOnTemplateParameterList - Builds a TemplateParameterList that
769/// contains the template parameters in Params/NumParams.
770Sema::TemplateParamsTy *
771Sema::ActOnTemplateParameterList(unsigned Depth,
772 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000773 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000774 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000775 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000776 SourceLocation RAngleLoc) {
777 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000778 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000779
Douglas Gregorddc29e12009-02-06 22:42:48 +0000780 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000781 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000782 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000783}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000784
John McCallb6217662010-03-15 10:12:16 +0000785static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
786 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000787 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000788}
789
John McCallf312b1e2010-08-26 23:41:50 +0000790DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000791Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000792 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000793 IdentifierInfo *Name, SourceLocation NameLoc,
794 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000795 TemplateParameterList *TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000796 AccessSpecifier AS,
797 unsigned NumOuterTemplateParamLists,
798 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000799 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000800 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000801 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000802 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000803
804 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000805 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000806 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000807
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000808 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
809 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000810
811 // There is no such thing as an unnamed class template.
812 if (!Name) {
813 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000814 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000815 }
816
817 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000818 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000819 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000820 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000821 if (SS.isNotEmpty() && !SS.isInvalid()) {
822 SemanticContext = computeDeclContext(SS, true);
823 if (!SemanticContext) {
824 // FIXME: Produce a reasonable diagnostic here
825 return true;
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
John McCall77bb1aa2010-05-01 00:40:08 +0000828 if (RequireCompleteDeclContext(SS, SemanticContext))
829 return true;
830
John McCalla24dc2e2009-11-17 02:14:36 +0000831 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000832 } else {
833 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000834 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000835 }
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Douglas Gregor57265e32010-04-12 16:00:01 +0000837 if (Previous.isAmbiguous())
838 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000839
Douglas Gregorddc29e12009-02-06 22:42:48 +0000840 NamedDecl *PrevDecl = 0;
841 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000842 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000843
Douglas Gregorddc29e12009-02-06 22:42:48 +0000844 // If there is a previous declaration with the same name, check
845 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000846 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000847 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000848
849 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000850 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000851 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000852 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000853 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
854 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000855 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000856 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
857 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
858 PrevClassTemplate
859 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
860 ->getSpecializedTemplate();
861 }
862 }
863
John McCall65c49462009-12-18 11:25:59 +0000864 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000865 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000866 // [...] When looking for a prior declaration of a class or a function
867 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000868 // function is neither a qualified name nor a template-id, scopes outside
869 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000870 if (!SS.isSet()) {
871 DeclContext *OutermostContext = CurContext;
872 while (!OutermostContext->isFileContext())
873 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000874
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000875 if (PrevDecl &&
876 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
877 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
878 SemanticContext = PrevDecl->getDeclContext();
879 } else {
880 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000881 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000882 // declaration.
883 PrevDecl = PrevClassTemplate = 0;
884 SemanticContext = OutermostContext;
885 }
John McCalle129d442009-12-17 23:21:11 +0000886 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000887
John McCalle129d442009-12-17 23:21:11 +0000888 if (CurContext->isDependentContext()) {
889 // If this is a dependent context, we don't want to link the friend
890 // class template to the template in scope, because that would perform
891 // checking of the template parameter lists that can't be performed
892 // until the outer context is instantiated.
893 PrevDecl = PrevClassTemplate = 0;
894 }
895 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
896 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000897
Douglas Gregorddc29e12009-02-06 22:42:48 +0000898 if (PrevClassTemplate) {
899 // Ensure that the template parameter lists are compatible.
900 if (!TemplateParameterListsAreEqual(TemplateParams,
901 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000902 /*Complain=*/true,
903 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000904 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000905
906 // C++ [temp.class]p4:
907 // In a redeclaration, partial specialization, explicit
908 // specialization or explicit instantiation of a class template,
909 // the class-key shall agree in kind with the original class
910 // template declaration (7.1.5.3).
911 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000912 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000913 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000914 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000915 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000917 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000918 }
919
Douglas Gregorddc29e12009-02-06 22:42:48 +0000920 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000921 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000922 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000923 Diag(NameLoc, diag::err_redefinition) << Name;
924 Diag(Def->getLocation(), diag::note_previous_definition);
925 // FIXME: Would it make sense to try to "forget" the previous
926 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000927 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000928 }
929 }
930 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
931 // Maybe we will complain about the shadowed template parameter.
932 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
933 // Just pretend that we didn't see the previous declaration.
934 PrevDecl = 0;
935 } else if (PrevDecl) {
936 // C++ [temp]p5:
937 // A class template shall not have the same name as any other
938 // template, class, function, object, enumeration, enumerator,
939 // namespace, or type in the same scope (3.3), except as specified
940 // in (14.5.4).
941 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
942 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000943 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000944 }
945
Douglas Gregord684b002009-02-10 19:49:53 +0000946 // Check the template parameter list of this declaration, possibly
947 // merging in the template parameter list from the previous class
948 // template declaration.
949 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000950 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000951 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000952 SemanticContext->isRecord() &&
953 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000954 ? TPC_ClassTemplateMember
955 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000956 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor57265e32010-04-12 16:00:01 +0000958 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000959 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000960 // template out-of-line.
961 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
962 !(TUK == TUK_Friend && CurContext->isDependentContext()))
963 Diag(NameLoc, diag::err_member_def_does_not_match)
964 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000965 }
966
Mike Stump1eb44332009-09-09 15:08:12 +0000967 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000968 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000969 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000970 PrevClassTemplate->getTemplatedDecl() : 0,
971 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000972 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000973 if (NumOuterTemplateParamLists > 0)
974 NewClass->setTemplateParameterListsInfo(Context,
975 NumOuterTemplateParamLists,
976 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000977
978 ClassTemplateDecl *NewTemplate
979 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
980 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000981 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000982 NewClass->setDescribedClassTemplate(NewTemplate);
983
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000984 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000985 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000986 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000987 assert(T->isDependentType() && "Class template type is not dependent?");
988 (void)T;
989
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000990 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000991 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000992 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000993 PrevClassTemplate->getInstantiatedFromMemberTemplate())
994 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000995
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000996 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000997 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000998 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Douglas Gregorddc29e12009-02-06 22:42:48 +00001000 // Set the lexical context of these templates
1001 NewClass->setLexicalDeclContext(CurContext);
1002 NewTemplate->setLexicalDeclContext(CurContext);
1003
John McCall0f434ec2009-07-31 02:45:11 +00001004 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001005 NewClass->startDefinition();
1006
1007 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001008 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001009
John McCall05b23ea2009-09-14 21:59:20 +00001010 if (TUK != TUK_Friend)
1011 PushOnScopeChains(NewTemplate, S);
1012 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001013 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001014 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001015 NewClass->setAccess(PrevClassTemplate->getAccess());
1016 }
John McCall05b23ea2009-09-14 21:59:20 +00001017
Douglas Gregord85bea22009-09-26 06:47:28 +00001018 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1019 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001020
John McCall05b23ea2009-09-14 21:59:20 +00001021 // Friend templates are visible in fairly strange ways.
1022 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001023 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001024 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1025 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1026 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001027 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001028 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001029
Douglas Gregord85bea22009-09-26 06:47:28 +00001030 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1031 NewClass->getLocation(),
1032 NewTemplate,
1033 /*FIXME:*/NewClass->getLocation());
1034 Friend->setAccess(AS_public);
1035 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001036 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001037
Douglas Gregord684b002009-02-10 19:49:53 +00001038 if (Invalid) {
1039 NewTemplate->setInvalidDecl();
1040 NewClass->setInvalidDecl();
1041 }
John McCalld226f652010-08-21 09:40:31 +00001042 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001043}
1044
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001045/// \brief Diagnose the presence of a default template argument on a
1046/// template parameter, which is ill-formed in certain contexts.
1047///
1048/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001049static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001050 Sema::TemplateParamListContext TPC,
1051 SourceLocation ParamLoc,
1052 SourceRange DefArgRange) {
1053 switch (TPC) {
1054 case Sema::TPC_ClassTemplate:
1055 return false;
1056
1057 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001058 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001059 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001060 // A default template-argument shall not be specified in a
1061 // function template declaration or a function template
1062 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001063 // If a friend function template declaration specifies a default
1064 // template-argument, that declaration shall be a definition and shall be
1065 // the only declaration of the function template in the translation unit.
1066 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001067 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001068 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001069 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001070 << DefArgRange;
1071 return false;
1072
1073 case Sema::TPC_ClassTemplateMember:
1074 // C++0x [temp.param]p9:
1075 // A default template-argument shall not be specified in the
1076 // template-parameter-lists of the definition of a member of a
1077 // class template that appears outside of the member's class.
1078 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1079 << DefArgRange;
1080 return true;
1081
1082 case Sema::TPC_FriendFunctionTemplate:
1083 // C++ [temp.param]p9:
1084 // A default template-argument shall not be specified in a
1085 // friend template declaration.
1086 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1087 << DefArgRange;
1088 return true;
1089
1090 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1091 // for friend function templates if there is only a single
1092 // declaration (and it is a definition). Strange!
1093 }
1094
1095 return false;
1096}
1097
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001098/// \brief Check for unexpanded parameter packs within the template parameters
1099/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001100static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1101 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001102 TemplateParameterList *Params = TTP->getTemplateParameters();
1103 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1104 NamedDecl *P = Params->getParam(I);
1105 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001106 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001107 NTTP->getTypeSourceInfo(),
1108 Sema::UPPC_NonTypeTemplateParameterType))
1109 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001110
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001111 continue;
1112 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001113
1114 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001115 = dyn_cast<TemplateTemplateParmDecl>(P))
1116 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1117 return true;
1118 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001119
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001120 return false;
1121}
1122
Douglas Gregord684b002009-02-10 19:49:53 +00001123/// \brief Checks the validity of a template parameter list, possibly
1124/// considering the template parameter list from a previous
1125/// declaration.
1126///
1127/// If an "old" template parameter list is provided, it must be
1128/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1129/// template parameter list.
1130///
1131/// \param NewParams Template parameter list for a new template
1132/// declaration. This template parameter list will be updated with any
1133/// default arguments that are carried through from the previous
1134/// template parameter list.
1135///
1136/// \param OldParams If provided, template parameter list from a
1137/// previous declaration of the same template. Default template
1138/// arguments will be merged from the old template parameter list to
1139/// the new template parameter list.
1140///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001141/// \param TPC Describes the context in which we are checking the given
1142/// template parameter list.
1143///
Douglas Gregord684b002009-02-10 19:49:53 +00001144/// \returns true if an error occurred, false otherwise.
1145bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001146 TemplateParameterList *OldParams,
1147 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001148 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Douglas Gregord684b002009-02-10 19:49:53 +00001150 // C++ [temp.param]p10:
1151 // The set of default template-arguments available for use with a
1152 // template declaration or definition is obtained by merging the
1153 // default arguments from the definition (if in scope) and all
1154 // declarations in scope in the same way default function
1155 // arguments are (8.3.6).
1156 bool SawDefaultArgument = false;
1157 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001158
Anders Carlsson49d25572009-06-12 23:20:15 +00001159 bool SawParameterPack = false;
1160 SourceLocation ParameterPackLoc;
1161
Mike Stump1a35fde2009-02-11 23:03:27 +00001162 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001163 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001164 if (OldParams)
1165 OldParam = OldParams->begin();
1166
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001167 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001168 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1169 NewParamEnd = NewParams->end();
1170 NewParam != NewParamEnd; ++NewParam) {
1171 // Variables used to diagnose redundant default arguments
1172 bool RedundantDefaultArg = false;
1173 SourceLocation OldDefaultLoc;
1174 SourceLocation NewDefaultLoc;
1175
1176 // Variables used to diagnose missing default arguments
1177 bool MissingDefaultArg = false;
1178
Anders Carlsson49d25572009-06-12 23:20:15 +00001179 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001180 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001181 // parameter pack, it shall be the last template parameter.
1182 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001183 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001184 diag::err_template_param_pack_must_be_last_template_parameter);
1185 Invalid = true;
1186 }
1187
Douglas Gregord684b002009-02-10 19:49:53 +00001188 if (TemplateTypeParmDecl *NewTypeParm
1189 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001190 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001191 if (NewTypeParm->hasDefaultArgument() &&
1192 DiagnoseDefaultTemplateArgument(*this, TPC,
1193 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001194 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001195 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001196 NewTypeParm->removeDefaultArgument();
1197
1198 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001199 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001200 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Anders Carlsson49d25572009-06-12 23:20:15 +00001202 if (NewTypeParm->isParameterPack()) {
1203 assert(!NewTypeParm->hasDefaultArgument() &&
1204 "Parameter packs can't have a default argument!");
1205 SawParameterPack = true;
1206 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001207 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001208 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001209 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1210 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1211 SawDefaultArgument = true;
1212 RedundantDefaultArg = true;
1213 PreviousDefaultArgLoc = NewDefaultLoc;
1214 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1215 // Merge the default argument from the old declaration to the
1216 // new declaration.
1217 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001218 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001219 true);
1220 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1221 } else if (NewTypeParm->hasDefaultArgument()) {
1222 SawDefaultArgument = true;
1223 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1224 } else if (SawDefaultArgument)
1225 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001226 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001227 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001228 // Check for unexpanded parameter packs.
1229 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001230 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001231 UPPC_NonTypeTemplateParameterType)) {
1232 Invalid = true;
1233 continue;
1234 }
1235
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001236 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001237 if (NewNonTypeParm->hasDefaultArgument() &&
1238 DiagnoseDefaultTemplateArgument(*this, TPC,
1239 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001240 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001241 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001242 }
1243
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001244 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001245 NonTypeTemplateParmDecl *OldNonTypeParm
1246 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001247 if (NewNonTypeParm->isParameterPack()) {
1248 assert(!NewNonTypeParm->hasDefaultArgument() &&
1249 "Parameter packs can't have a default argument!");
1250 SawParameterPack = true;
1251 ParameterPackLoc = NewNonTypeParm->getLocation();
1252 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001253 NewNonTypeParm->hasDefaultArgument()) {
1254 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1255 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1256 SawDefaultArgument = true;
1257 RedundantDefaultArg = true;
1258 PreviousDefaultArgLoc = NewDefaultLoc;
1259 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1260 // Merge the default argument from the old declaration to the
1261 // new declaration.
1262 SawDefaultArgument = true;
1263 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001264 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001265 // parameter.
1266 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001267 OldNonTypeParm->getDefaultArgument(),
1268 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001269 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1270 } else if (NewNonTypeParm->hasDefaultArgument()) {
1271 SawDefaultArgument = true;
1272 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1273 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001274 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001275 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001276 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001277 TemplateTemplateParmDecl *NewTemplateParm
1278 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001279
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001280 // Check for unexpanded parameter packs, recursively.
1281 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1282 Invalid = true;
1283 continue;
1284 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001285
1286 if (NewTemplateParm->hasDefaultArgument() &&
1287 DiagnoseDefaultTemplateArgument(*this, TPC,
1288 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001289 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001290 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001291
1292 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001293 TemplateTemplateParmDecl *OldTemplateParm
1294 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001295 if (NewTemplateParm->isParameterPack()) {
1296 assert(!NewTemplateParm->hasDefaultArgument() &&
1297 "Parameter packs can't have a default argument!");
1298 SawParameterPack = true;
1299 ParameterPackLoc = NewTemplateParm->getLocation();
1300 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001301 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001302 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1303 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001304 SawDefaultArgument = true;
1305 RedundantDefaultArg = true;
1306 PreviousDefaultArgLoc = NewDefaultLoc;
1307 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1308 // Merge the default argument from the old declaration to the
1309 // new declaration.
1310 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001311 // FIXME: We need to create a new kind of "default argument" expression
1312 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001313 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001314 OldTemplateParm->getDefaultArgument(),
1315 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001316 PreviousDefaultArgLoc
1317 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001318 } else if (NewTemplateParm->hasDefaultArgument()) {
1319 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001320 PreviousDefaultArgLoc
1321 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001322 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001323 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001324 }
1325
1326 if (RedundantDefaultArg) {
1327 // C++ [temp.param]p12:
1328 // A template-parameter shall not be given default arguments
1329 // by two different declarations in the same scope.
1330 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1331 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1332 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001333 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001334 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001335 // If a template-parameter of a class template has a default
1336 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001337 // have a default template-argument supplied or be a template parameter
1338 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001339 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001340 diag::err_template_param_default_arg_missing);
1341 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1342 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001343 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001344 }
1345
1346 // If we have an old template parameter list that we're merging
1347 // in, move on to the next parameter.
1348 if (OldParams)
1349 ++OldParam;
1350 }
1351
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001352 // We were missing some default arguments at the end of the list, so remove
1353 // all of the default arguments.
1354 if (RemoveDefaultArguments) {
1355 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1356 NewParamEnd = NewParams->end();
1357 NewParam != NewParamEnd; ++NewParam) {
1358 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1359 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001360 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001361 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1362 NTTP->removeDefaultArgument();
1363 else
1364 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1365 }
1366 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001367
Douglas Gregord684b002009-02-10 19:49:53 +00001368 return Invalid;
1369}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001370
John McCall4e2cbb22010-10-20 05:44:58 +00001371namespace {
1372
1373/// A class which looks for a use of a certain level of template
1374/// parameter.
1375struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1376 typedef RecursiveASTVisitor<DependencyChecker> super;
1377
1378 unsigned Depth;
1379 bool Match;
1380
1381 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1382 NamedDecl *ND = Params->getParam(0);
1383 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1384 Depth = PD->getDepth();
1385 } else if (NonTypeTemplateParmDecl *PD =
1386 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1387 Depth = PD->getDepth();
1388 } else {
1389 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1390 }
1391 }
1392
1393 bool Matches(unsigned ParmDepth) {
1394 if (ParmDepth >= Depth) {
1395 Match = true;
1396 return true;
1397 }
1398 return false;
1399 }
1400
1401 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1402 return !Matches(T->getDepth());
1403 }
1404
1405 bool TraverseTemplateName(TemplateName N) {
1406 if (TemplateTemplateParmDecl *PD =
1407 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1408 if (Matches(PD->getDepth())) return false;
1409 return super::TraverseTemplateName(N);
1410 }
1411
1412 bool VisitDeclRefExpr(DeclRefExpr *E) {
1413 if (NonTypeTemplateParmDecl *PD =
1414 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1415 if (PD->getDepth() == Depth) {
1416 Match = true;
1417 return false;
1418 }
1419 }
1420 return super::VisitDeclRefExpr(E);
1421 }
1422};
1423}
1424
1425/// Determines whether a template-id depends on the given parameter
1426/// list.
1427static bool
1428DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1429 TemplateParameterList *Params) {
1430 DependencyChecker Checker(Params);
1431 Checker.TraverseType(QualType(TemplateId, 0));
1432 return Checker.Match;
1433}
1434
Mike Stump1eb44332009-09-09 15:08:12 +00001435/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001436/// specifier, returning the template parameter list that applies to the
1437/// name.
1438///
1439/// \param DeclStartLoc the start of the declaration that has a scope
1440/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001441///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001442/// \param SS the scope specifier that will be matched to the given template
1443/// parameter lists. This scope specifier precedes a qualified name that is
1444/// being declared.
1445///
1446/// \param ParamLists the template parameter lists, from the outermost to the
1447/// innermost template parameter lists.
1448///
1449/// \param NumParamLists the number of template parameter lists in ParamLists.
1450///
John McCall77e8b112010-04-13 20:37:33 +00001451/// \param IsFriend Whether to apply the slightly different rules for
1452/// matching template parameters to scope specifiers in friend
1453/// declarations.
1454///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001455/// \param IsExplicitSpecialization will be set true if the entity being
1456/// declared is an explicit specialization, false otherwise.
1457///
Mike Stump1eb44332009-09-09 15:08:12 +00001458/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001459/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001460/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001461/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001462/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001463/// itself a template).
1464TemplateParameterList *
1465Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1466 const CXXScopeSpec &SS,
1467 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001468 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001469 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001470 bool &IsExplicitSpecialization,
1471 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001472 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001473
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001474 // Find the template-ids that occur within the nested-name-specifier. These
1475 // template-ids will match up with the template parameter lists.
1476 llvm::SmallVector<const TemplateSpecializationType *, 4>
1477 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001478 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1479 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001480 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1481 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001482 const Type *T = NNS->getAsType();
1483 if (!T) break;
1484
1485 // C++0x [temp.expl.spec]p17:
1486 // A member or a member template may be nested within many
1487 // enclosing class templates. In an explicit specialization for
1488 // such a member, the member declaration shall be preceded by a
1489 // template<> for each enclosing class template that is
1490 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001491 //
1492 // Following the existing practice of GNU and EDG, we allow a typedef of a
1493 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001494 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1495 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001496
Mike Stump1eb44332009-09-09 15:08:12 +00001497 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001498 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001499 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1500 if (!Template)
1501 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Ted Kremenek6217b802009-07-29 21:53:49 +00001503 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001504 ClassTemplateSpecializationDecl *SpecDecl
1505 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1506 // If the nested name specifier refers to an explicit specialization,
1507 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001508 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1509 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001510 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001511 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001512 }
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001514 TemplateIdsInSpecifier.push_back(SpecType);
1515 }
1516 }
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001518 // Reverse the list of template-ids in the scope specifier, so that we can
1519 // more easily match up the template-ids and the template parameter lists.
1520 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001522 SourceLocation FirstTemplateLoc = DeclStartLoc;
1523 if (NumParamLists)
1524 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001526 // Match the template-ids found in the specifier to the template parameter
1527 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001528 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001529 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001530 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1531 const TemplateSpecializationType *TemplateId
1532 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001533 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001534
1535 // In friend declarations we can have template-ids which don't
1536 // depend on the corresponding template parameter lists. But
1537 // assume that empty parameter lists are supposed to match this
1538 // template-id.
1539 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1540 if (!DependentTemplateId ||
1541 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1542 continue;
1543 }
1544
1545 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001546 // We have a template-id without a corresponding template parameter
1547 // list.
John McCall77e8b112010-04-13 20:37:33 +00001548
1549 // ...which is fine if this is a friend declaration.
1550 if (IsFriend) {
1551 IsExplicitSpecialization = true;
1552 break;
1553 }
1554
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001555 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001556 // FIXME: the location information here isn't great.
1557 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001558 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001559 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001560 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001561 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001562 } else {
1563 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1564 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001565 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001566 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001567 }
1568 return 0;
1569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001571 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001572 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001573 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001574
John McCall31f17ec2010-04-27 00:57:59 +00001575 // Are there cases in (e.g.) friends where this won't match?
1576 if (const InjectedClassNameType *Injected
1577 = TemplateId->getAs<InjectedClassNameType>()) {
1578 CXXRecordDecl *Record = Injected->getDecl();
1579 if (ClassTemplatePartialSpecializationDecl *Partial =
1580 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1581 ExpectedTemplateParams = Partial->getTemplateParameters();
1582 else
1583 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1584 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001585 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001586
John McCall31f17ec2010-04-27 00:57:59 +00001587 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001588 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001589 ExpectedTemplateParams,
1590 true, TPL_TemplateMatch);
1591
John McCall4e2cbb22010-10-20 05:44:58 +00001592 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1593 TPC_ClassTemplateMember);
1594 } else if (ParamLists[ParamIdx]->size() > 0)
1595 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001596 diag::err_template_param_list_matches_nontemplate)
1597 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001598 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001599 else
1600 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001601
1602 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001603 }
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001605 // If there were at least as many template-ids as there were template
1606 // parameter lists, then there are no template parameter lists remaining for
1607 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001608 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001609 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001611 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001612 if (ParamIdx != NumParamLists - 1) {
1613 while (ParamIdx < NumParamLists - 1) {
1614 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1615 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001616 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1617 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001618 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1619 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001620
1621 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1622 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1623 diag::note_explicit_template_spec_does_not_need_header)
1624 << ExplicitSpecializationsInSpecifier.back();
1625 ExplicitSpecializationsInSpecifier.pop_back();
1626 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001627
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001628 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001629 // means that the resulting template declaration can't be instantiated
1630 // properly (we'll end up with dependent nodes when we shouldn't).
1631 if (!isExplicitSpecHeader)
1632 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001633
John McCall4e2cbb22010-10-20 05:44:58 +00001634 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001635 }
1636 }
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001638 // Return the last template parameter list, which corresponds to the
1639 // entity being declared.
1640 return ParamLists[NumParamLists - 1];
1641}
1642
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001643void Sema::NoteAllFoundTemplates(TemplateName Name) {
1644 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1645 Diag(Template->getLocation(), diag::note_template_declared_here)
1646 << (isa<FunctionTemplateDecl>(Template)? 0
1647 : isa<ClassTemplateDecl>(Template)? 1
1648 : 2)
1649 << Template->getDeclName();
1650 return;
1651 }
1652
1653 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1654 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1655 IEnd = OST->end();
1656 I != IEnd; ++I)
1657 Diag((*I)->getLocation(), diag::note_template_declared_here)
1658 << 0 << (*I)->getDeclName();
1659
1660 return;
1661 }
1662}
1663
1664
Douglas Gregor7532dc62009-03-30 22:58:21 +00001665QualType Sema::CheckTemplateIdType(TemplateName Name,
1666 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001667 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001668 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001669 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1670 // We might have a substituted template template parameter pack. If so,
1671 // build a template specialization type for it.
1672 if (Name.getAsSubstTemplateTemplateParmPack())
1673 return Context.getTemplateSpecializationType(Name, TemplateArgs);
1674
1675 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1676 << Name;
1677 NoteAllFoundTemplates(Name);
1678 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001679 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001680
Douglas Gregor40808ce2009-03-09 23:48:35 +00001681 // Check that the template argument list is well-formed for this
1682 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001683 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001684 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001685 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001686 return QualType();
1687
Douglas Gregor910f8002010-11-07 23:05:16 +00001688 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001689 "Converted template argument list is too short!");
1690
1691 QualType CanonType;
1692
Douglas Gregorcaddba02009-11-12 18:38:13 +00001693 if (Name.isDependent() ||
1694 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001695 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001696 // This class template specialization is a dependent
1697 // type. Therefore, its canonical type is another class template
1698 // specialization type that contains all of the converted
1699 // arguments in canonical form. This ensures that, e.g., A<T> and
1700 // A<T, T> have identical types when A is declared as:
1701 //
1702 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001703 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001704 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001705 Converted.data(),
1706 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Douglas Gregor1275ae02009-07-28 23:00:59 +00001708 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001709 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001710 // In the future, we need to teach getTemplateSpecializationType to only
1711 // build the canonical type and return that to us.
1712 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001713
1714 // This might work out to be a current instantiation, in which
1715 // case the canonical type needs to be the InjectedClassNameType.
1716 //
1717 // TODO: in theory this could be a simple hashtable lookup; most
1718 // changes to CurContext don't change the set of current
1719 // instantiations.
1720 if (isa<ClassTemplateDecl>(Template)) {
1721 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1722 // If we get out to a namespace, we're done.
1723 if (Ctx->isFileContext()) break;
1724
1725 // If this isn't a record, keep looking.
1726 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1727 if (!Record) continue;
1728
1729 // Look for one of the two cases with InjectedClassNameTypes
1730 // and check whether it's the same template.
1731 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1732 !Record->getDescribedClassTemplate())
1733 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001734
John McCall31f17ec2010-04-27 00:57:59 +00001735 // Fetch the injected class name type and check whether its
1736 // injected type is equal to the type we just built.
1737 QualType ICNT = Context.getTypeDeclType(Record);
1738 QualType Injected = cast<InjectedClassNameType>(ICNT)
1739 ->getInjectedSpecializationType();
1740
1741 if (CanonType != Injected->getCanonicalTypeInternal())
1742 continue;
1743
1744 // If so, the canonical type of this TST is the injected
1745 // class name type of the record we just found.
1746 assert(ICNT.isCanonical());
1747 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001748 break;
1749 }
1750 }
Mike Stump1eb44332009-09-09 15:08:12 +00001751 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001752 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001753 // Find the class template specialization declaration that
1754 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001755 void *InsertPos = 0;
1756 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001757 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001758 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001759 if (!Decl) {
1760 // This is the first time we have referenced this class template
1761 // specialization. Create the canonical declaration and add it to
1762 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001763 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001764 ClassTemplate->getTemplatedDecl()->getTagKind(),
1765 ClassTemplate->getDeclContext(),
1766 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001767 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001768 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001769 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001770 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001771 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001772 Decl->setLexicalDeclContext(CurContext);
1773 }
1774
1775 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001776 assert(isa<RecordType>(CanonType) &&
1777 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001778 }
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Douglas Gregor40808ce2009-03-09 23:48:35 +00001780 // Build the fully-sugared type for this class template
1781 // specialization, which refers back to the class template
1782 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001783 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001784}
1785
John McCallf312b1e2010-08-26 23:41:50 +00001786TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001787Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1788 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001789 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001790 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001791 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001792 if (SS.isInvalid())
1793 return true;
1794
Douglas Gregor7532dc62009-03-30 22:58:21 +00001795 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001796
Douglas Gregor40808ce2009-03-09 23:48:35 +00001797 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001798 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001799 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001800
Douglas Gregora88f09f2011-02-28 17:23:35 +00001801 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1802 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1803 DTN->getQualifier(),
1804 DTN->getIdentifier(),
1805 TemplateArgs);
1806
1807 // Build type-source information.
1808 TypeLocBuilder TLB;
1809 DependentTemplateSpecializationTypeLoc SpecTL
1810 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001811 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001812 SpecTL.setNameLoc(TemplateLoc);
1813 SpecTL.setLAngleLoc(LAngleLoc);
1814 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001815 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001816 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1817 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1818 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1819 }
1820
John McCalld5532b62009-11-23 01:53:49 +00001821 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001822 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001823
1824 if (Result.isNull())
1825 return true;
1826
Douglas Gregor059101f2011-03-02 00:47:37 +00001827 // Build type-source information.
1828 TypeLocBuilder TLB;
1829 TemplateSpecializationTypeLoc SpecTL
1830 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1831 SpecTL.setTemplateNameLoc(TemplateLoc);
1832 SpecTL.setLAngleLoc(LAngleLoc);
1833 SpecTL.setRAngleLoc(RAngleLoc);
1834 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1835 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001836
Douglas Gregor059101f2011-03-02 00:47:37 +00001837 if (SS.isNotEmpty()) {
1838 // Create an elaborated-type-specifier containing the nested-name-specifier.
1839 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1840 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1841 ElabTL.setKeywordLoc(SourceLocation());
1842 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1843 }
1844
1845 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001846}
John McCallf1bbbb42009-09-04 01:14:41 +00001847
Douglas Gregor059101f2011-03-02 00:47:37 +00001848TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001849 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001850 SourceLocation TagLoc,
1851 CXXScopeSpec &SS,
1852 TemplateTy TemplateD,
1853 SourceLocation TemplateLoc,
1854 SourceLocation LAngleLoc,
1855 ASTTemplateArgsPtr TemplateArgsIn,
1856 SourceLocation RAngleLoc) {
1857 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1858
1859 // Translate the parser's template argument list in our AST format.
1860 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1861 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1862
1863 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001864 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001865 ElaboratedTypeKeyword Keyword
1866 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Douglas Gregor059101f2011-03-02 00:47:37 +00001868 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1869 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1870 DTN->getQualifier(),
1871 DTN->getIdentifier(),
1872 TemplateArgs);
1873
1874 // Build type-source information.
1875 TypeLocBuilder TLB;
1876 DependentTemplateSpecializationTypeLoc SpecTL
1877 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1878 SpecTL.setKeywordLoc(TagLoc);
1879 SpecTL.setNameLoc(TemplateLoc);
1880 SpecTL.setLAngleLoc(LAngleLoc);
1881 SpecTL.setRAngleLoc(RAngleLoc);
1882 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1883 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1884 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1885 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1886 }
1887
1888 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1889 if (Result.isNull())
1890 return TypeResult();
1891
1892 // Check the tag kind
1893 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001894 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001895
John McCall6b2becf2009-09-08 17:47:29 +00001896 IdentifierInfo *Id = D->getIdentifier();
1897 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001898
John McCall6b2becf2009-09-08 17:47:29 +00001899 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1900 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001901 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001902 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001903 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001904 }
1905 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001906
1907 // Provide source-location information for the template specialization.
1908 TypeLocBuilder TLB;
1909 TemplateSpecializationTypeLoc SpecTL
1910 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1911 SpecTL.setTemplateNameLoc(TemplateLoc);
1912 SpecTL.setLAngleLoc(LAngleLoc);
1913 SpecTL.setRAngleLoc(RAngleLoc);
1914 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1915 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001916
Douglas Gregor059101f2011-03-02 00:47:37 +00001917 // Construct an elaborated type containing the nested-name-specifier (if any)
1918 // and keyword.
1919 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1920 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1921 ElabTL.setKeywordLoc(TagLoc);
1922 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1923 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001924}
1925
John McCall60d7b3a2010-08-24 06:29:42 +00001926ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001927 LookupResult &R,
1928 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001929 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001930 // FIXME: Can we do any checking at this point? I guess we could check the
1931 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001932 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001933 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001934 // foo<int> could identify a single function unambiguously
1935 // This approach does NOT work, since f<int>(1);
1936 // gets resolved prior to resorting to overload resolution
1937 // i.e., template<class T> void f(double);
1938 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001939
1940 // These should be filtered out by our callers.
1941 assert(!R.empty() && "empty lookup results when building templateid");
1942 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1943
John McCallc373d482010-01-27 01:50:18 +00001944 // We don't want lookup warnings at this point.
1945 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001946
John McCallf7a1a742009-11-24 19:00:30 +00001947 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001948 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001949 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001950 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001951 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001952 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001953
1954 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001955}
1956
John McCallf7a1a742009-11-24 19:00:30 +00001957// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001958ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001959Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001960 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001961 const TemplateArgumentListInfo &TemplateArgs) {
1962 DeclContext *DC;
1963 if (!(DC = computeDeclContext(SS, false)) ||
1964 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001965 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001966 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001968 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001969 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001970 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1971 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001972
John McCallf7a1a742009-11-24 19:00:30 +00001973 if (R.isAmbiguous())
1974 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001975
John McCallf7a1a742009-11-24 19:00:30 +00001976 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001977 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1978 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001979 return ExprError();
1980 }
1981
1982 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001983 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1984 << (NestedNameSpecifier*) SS.getScopeRep()
1985 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001986 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1987 return ExprError();
1988 }
1989
1990 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001991}
1992
Douglas Gregorc45c2322009-03-31 00:43:58 +00001993/// \brief Form a dependent template name.
1994///
1995/// This action forms a dependent template name given the template
1996/// name and its (presumably dependent) scope specifier. For
1997/// example, given "MetaFun::template apply", the scope specifier \p
1998/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1999/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002000TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002001 SourceLocation TemplateKWLoc,
2002 CXXScopeSpec &SS,
2003 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002004 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002005 bool EnteringContext,
2006 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002007 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2008 !getLangOptions().CPlusPlus0x)
2009 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002010 << FixItHint::CreateRemoval(TemplateKWLoc);
2011
Douglas Gregor0707bc52010-01-19 16:01:07 +00002012 DeclContext *LookupCtx = 0;
2013 if (SS.isSet())
2014 LookupCtx = computeDeclContext(SS, EnteringContext);
2015 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002016 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002017 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002018 // C++0x [temp.names]p5:
2019 // If a name prefixed by the keyword template is not the name of
2020 // a template, the program is ill-formed. [Note: the keyword
2021 // template may not be applied to non-template members of class
2022 // templates. -end note ] [ Note: as is the case with the
2023 // typename prefix, the template prefix is allowed in cases
2024 // where it is not strictly necessary; i.e., when the
2025 // nested-name-specifier or the expression on the left of the ->
2026 // or . is not dependent on a template-parameter, or the use
2027 // does not appear in the scope of a template. -end note]
2028 //
2029 // Note: C++03 was more strict here, because it banned the use of
2030 // the "template" keyword prior to a template-name that was not a
2031 // dependent name. C++ DR468 relaxed this requirement (the
2032 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002033 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002034 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002035 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2036 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002037 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002038 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2039 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002040 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2041 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002042 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002043 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002044 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002045 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002046 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002047 << Name.getSourceRange()
2048 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002049 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002050 } else {
2051 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002052 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002053 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002054 }
2055
Mike Stump1eb44332009-09-09 15:08:12 +00002056 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002057 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002058
Douglas Gregor014e88d2009-11-03 23:16:33 +00002059 switch (Name.getKind()) {
2060 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002061 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002062 Name.Identifier));
2063 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002064
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002065 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002066 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002067 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002068 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002069
2070 case UnqualifiedId::IK_LiteralOperatorId:
2071 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2072
Douglas Gregor014e88d2009-11-03 23:16:33 +00002073 default:
2074 break;
2075 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002076
2077 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002078 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002079 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002080 << Name.getSourceRange()
2081 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002082 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002083}
2084
Mike Stump1eb44332009-09-09 15:08:12 +00002085bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002086 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002087 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002088 const TemplateArgument &Arg = AL.getArgument();
2089
Anders Carlsson436b1562009-06-13 00:33:33 +00002090 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002091 switch(Arg.getKind()) {
2092 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002093 // C++ [temp.arg.type]p1:
2094 // A template-argument for a template-parameter which is a
2095 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002096 break;
2097 case TemplateArgument::Template: {
2098 // We have a template type parameter but the template argument
2099 // is a template without any arguments.
2100 SourceRange SR = AL.getSourceRange();
2101 TemplateName Name = Arg.getAsTemplate();
2102 Diag(SR.getBegin(), diag::err_template_missing_args)
2103 << Name << SR;
2104 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2105 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002106
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002107 return true;
2108 }
2109 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002110 // We have a template type parameter but the template argument
2111 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002112 SourceRange SR = AL.getSourceRange();
2113 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002114 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Anders Carlsson436b1562009-06-13 00:33:33 +00002116 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002117 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002118 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002119
John McCalla93c9342009-12-07 02:54:59 +00002120 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002121 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002122
Anders Carlsson436b1562009-06-13 00:33:33 +00002123 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002124 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002125 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002126 return false;
2127}
2128
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002129/// \brief Substitute template arguments into the default template argument for
2130/// the given template type parameter.
2131///
2132/// \param SemaRef the semantic analysis object for which we are performing
2133/// the substitution.
2134///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002135/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002136/// for.
2137///
2138/// \param TemplateLoc the location of the template name that started the
2139/// template-id we are checking.
2140///
2141/// \param RAngleLoc the location of the right angle bracket ('>') that
2142/// terminates the template-id.
2143///
2144/// \param Param the template template parameter whose default we are
2145/// substituting into.
2146///
2147/// \param Converted the list of template arguments provided for template
2148/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002149/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002150static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002151SubstDefaultTemplateArgument(Sema &SemaRef,
2152 TemplateDecl *Template,
2153 SourceLocation TemplateLoc,
2154 SourceLocation RAngleLoc,
2155 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002156 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002157 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002158
2159 // If the argument type is dependent, instantiate it now based
2160 // on the previously-computed template arguments.
2161 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002162 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002163 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002164
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002165 MultiLevelTemplateArgumentList AllTemplateArgs
2166 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2167
2168 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002169 Template, Converted.data(),
2170 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002171 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002172
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002173 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2174 Param->getDefaultArgumentLoc(),
2175 Param->getDeclName());
2176 }
2177
2178 return ArgType;
2179}
2180
2181/// \brief Substitute template arguments into the default template argument for
2182/// the given non-type template parameter.
2183///
2184/// \param SemaRef the semantic analysis object for which we are performing
2185/// the substitution.
2186///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002187/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002188/// for.
2189///
2190/// \param TemplateLoc the location of the template name that started the
2191/// template-id we are checking.
2192///
2193/// \param RAngleLoc the location of the right angle bracket ('>') that
2194/// terminates the template-id.
2195///
Douglas Gregor788cd062009-11-11 01:00:40 +00002196/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002197/// substituting into.
2198///
2199/// \param Converted the list of template arguments provided for template
2200/// parameters that precede \p Param in the template parameter list.
2201///
2202/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002203static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002204SubstDefaultTemplateArgument(Sema &SemaRef,
2205 TemplateDecl *Template,
2206 SourceLocation TemplateLoc,
2207 SourceLocation RAngleLoc,
2208 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002209 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002210 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002211 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002212
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002213 MultiLevelTemplateArgumentList AllTemplateArgs
2214 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002215
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002216 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002217 Template, Converted.data(),
2218 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002219 SourceRange(TemplateLoc, RAngleLoc));
2220
2221 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2222}
2223
Douglas Gregor788cd062009-11-11 01:00:40 +00002224/// \brief Substitute template arguments into the default template argument for
2225/// the given template template parameter.
2226///
2227/// \param SemaRef the semantic analysis object for which we are performing
2228/// the substitution.
2229///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002230/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002231/// for.
2232///
2233/// \param TemplateLoc the location of the template name that started the
2234/// template-id we are checking.
2235///
2236/// \param RAngleLoc the location of the right angle bracket ('>') that
2237/// terminates the template-id.
2238///
2239/// \param Param the template template parameter whose default we are
2240/// substituting into.
2241///
2242/// \param Converted the list of template arguments provided for template
2243/// parameters that precede \p Param in the template parameter list.
2244///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002245/// \param QualifierLoc Will be set to the nested-name-specifier (with
2246/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002247///
Douglas Gregor788cd062009-11-11 01:00:40 +00002248/// \returns the substituted template argument, or NULL if an error occurred.
2249static TemplateName
2250SubstDefaultTemplateArgument(Sema &SemaRef,
2251 TemplateDecl *Template,
2252 SourceLocation TemplateLoc,
2253 SourceLocation RAngleLoc,
2254 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002255 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2256 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002257 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002258 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002259
Douglas Gregor788cd062009-11-11 01:00:40 +00002260 MultiLevelTemplateArgumentList AllTemplateArgs
2261 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002262
Douglas Gregor788cd062009-11-11 01:00:40 +00002263 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002264 Template, Converted.data(),
2265 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002266 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002267
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002268 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002269 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002270 if (QualifierLoc) {
2271 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2272 AllTemplateArgs);
2273 if (!QualifierLoc)
2274 return TemplateName();
2275 }
2276
Douglas Gregor1d752d72011-03-02 18:46:51 +00002277 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002278 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002279 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002280 AllTemplateArgs);
2281}
2282
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002283/// \brief If the given template parameter has a default template
2284/// argument, substitute into that default template argument and
2285/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002286TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002287Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2288 SourceLocation TemplateLoc,
2289 SourceLocation RAngleLoc,
2290 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002291 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2292 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002293 if (!TypeParm->hasDefaultArgument())
2294 return TemplateArgumentLoc();
2295
John McCalla93c9342009-12-07 02:54:59 +00002296 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002297 TemplateLoc,
2298 RAngleLoc,
2299 TypeParm,
2300 Converted);
2301 if (DI)
2302 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2303
2304 return TemplateArgumentLoc();
2305 }
2306
2307 if (NonTypeTemplateParmDecl *NonTypeParm
2308 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2309 if (!NonTypeParm->hasDefaultArgument())
2310 return TemplateArgumentLoc();
2311
John McCall60d7b3a2010-08-24 06:29:42 +00002312 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002313 TemplateLoc,
2314 RAngleLoc,
2315 NonTypeParm,
2316 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002317 if (Arg.isInvalid())
2318 return TemplateArgumentLoc();
2319
2320 Expr *ArgE = Arg.takeAs<Expr>();
2321 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2322 }
2323
2324 TemplateTemplateParmDecl *TempTempParm
2325 = cast<TemplateTemplateParmDecl>(Param);
2326 if (!TempTempParm->hasDefaultArgument())
2327 return TemplateArgumentLoc();
2328
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002329
Douglas Gregor1d752d72011-03-02 18:46:51 +00002330 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002331 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002332 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002333 RAngleLoc,
2334 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002335 Converted,
2336 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002337 if (TName.isNull())
2338 return TemplateArgumentLoc();
2339
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002340 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002341 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002342 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2343}
2344
Douglas Gregore7526412009-11-11 19:31:23 +00002345/// \brief Check that the given template argument corresponds to the given
2346/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002347///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002348/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002349/// checked.
2350///
2351/// \param Arg The template argument.
2352///
2353/// \param Template The template in which the template argument resides.
2354///
2355/// \param TemplateLoc The location of the template name for the template
2356/// whose argument list we're matching.
2357///
2358/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2359/// the template argument list.
2360///
2361/// \param ArgumentPackIndex The index into the argument pack where this
2362/// argument will be placed. Only valid if the parameter is a parameter pack.
2363///
2364/// \param Converted The checked, converted argument will be added to the
2365/// end of this small vector.
2366///
2367/// \param CTAK Describes how we arrived at this particular template argument:
2368/// explicitly written, deduced, etc.
2369///
2370/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002371bool Sema::CheckTemplateArgument(NamedDecl *Param,
2372 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002373 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002374 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002375 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002376 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002377 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002378 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002379 // Check template type parameters.
2380 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002381 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002382
Douglas Gregord9e15302009-11-11 19:41:09 +00002383 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002384 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002385 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002386 // with the template arguments we've seen thus far. But if the
2387 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002388 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002389 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2390 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002391
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002392 if (NTTPType->isDependentType() &&
2393 !isa<TemplateTemplateParmDecl>(Template) &&
2394 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002395 // Do substitution on the type of the non-type template parameter.
2396 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002397 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002398 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002399
2400 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002401 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002402 NTTPType = SubstType(NTTPType,
2403 MultiLevelTemplateArgumentList(TemplateArgs),
2404 NTTP->getLocation(),
2405 NTTP->getDeclName());
2406 // If that worked, check the non-type template parameter type
2407 // for validity.
2408 if (!NTTPType.isNull())
2409 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2410 NTTP->getLocation());
2411 if (NTTPType.isNull())
2412 return true;
2413 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002414
Douglas Gregore7526412009-11-11 19:31:23 +00002415 switch (Arg.getArgument().getKind()) {
2416 case TemplateArgument::Null:
2417 assert(false && "Should never see a NULL template argument here");
2418 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002419
Douglas Gregore7526412009-11-11 19:31:23 +00002420 case TemplateArgument::Expression: {
2421 Expr *E = Arg.getArgument().getAsExpr();
2422 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002423 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002424 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002425
Douglas Gregor910f8002010-11-07 23:05:16 +00002426 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002427 break;
2428 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002429
Douglas Gregore7526412009-11-11 19:31:23 +00002430 case TemplateArgument::Declaration:
2431 case TemplateArgument::Integral:
2432 // We've already checked this template argument, so just copy
2433 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002434 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002435 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002436
Douglas Gregore7526412009-11-11 19:31:23 +00002437 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002438 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002439 // We were given a template template argument. It may not be ill-formed;
2440 // see below.
2441 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002442 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2443 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002444 // We have a template argument such as \c T::template X, which we
2445 // parsed as a template template argument. However, since we now
2446 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002447 // template name into an expression.
2448
2449 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2450 Arg.getTemplateNameLoc());
2451
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002452 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002453 SS.Adopt(Arg.getTemplateQualifierLoc());
John McCallf7a1a742009-11-24 19:00:30 +00002454 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002455 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002456 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002457
Douglas Gregora7fc9012011-01-05 18:58:31 +00002458 // If we parsed the template argument as a pack expansion, create a
2459 // pack expansion expression.
2460 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002461 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002462 Arg.getTemplateEllipsisLoc());
2463 if (Expansion.isInvalid())
2464 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002465
Douglas Gregora7fc9012011-01-05 18:58:31 +00002466 E = Expansion.get();
2467 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002468
Douglas Gregore7526412009-11-11 19:31:23 +00002469 TemplateArgument Result;
2470 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2471 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002472
Douglas Gregor910f8002010-11-07 23:05:16 +00002473 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002474 break;
2475 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002476
Douglas Gregore7526412009-11-11 19:31:23 +00002477 // We have a template argument that actually does refer to a class
2478 // template, template alias, or template template parameter, and
2479 // therefore cannot be a non-type template argument.
2480 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2481 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002482
Douglas Gregore7526412009-11-11 19:31:23 +00002483 Diag(Param->getLocation(), diag::note_template_param_here);
2484 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002485
Douglas Gregore7526412009-11-11 19:31:23 +00002486 case TemplateArgument::Type: {
2487 // We have a non-type template parameter but the template
2488 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002489
Douglas Gregore7526412009-11-11 19:31:23 +00002490 // C++ [temp.arg]p2:
2491 // In a template-argument, an ambiguity between a type-id and
2492 // an expression is resolved to a type-id, regardless of the
2493 // form of the corresponding template-parameter.
2494 //
2495 // We warn specifically about this case, since it can be rather
2496 // confusing for users.
2497 QualType T = Arg.getArgument().getAsType();
2498 SourceRange SR = Arg.getSourceRange();
2499 if (T->isFunctionType())
2500 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2501 else
2502 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2503 Diag(Param->getLocation(), diag::note_template_param_here);
2504 return true;
2505 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002506
Douglas Gregore7526412009-11-11 19:31:23 +00002507 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002508 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002509 break;
2510 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002511
Douglas Gregore7526412009-11-11 19:31:23 +00002512 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002513 }
2514
2515
Douglas Gregore7526412009-11-11 19:31:23 +00002516 // Check template template parameters.
2517 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002518
Douglas Gregore7526412009-11-11 19:31:23 +00002519 // Substitute into the template parameter list of the template
2520 // template parameter, since previously-supplied template arguments
2521 // may appear within the template template parameter.
2522 {
2523 // Set up a template instantiation context.
2524 LocalInstantiationScope Scope(*this);
2525 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002526 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002527 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002528
2529 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002530 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002531 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002532 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002533 MultiLevelTemplateArgumentList(TemplateArgs)));
2534 if (!TempParm)
2535 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002536 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002537
Douglas Gregore7526412009-11-11 19:31:23 +00002538 switch (Arg.getArgument().getKind()) {
2539 case TemplateArgument::Null:
2540 assert(false && "Should never see a NULL template argument here");
2541 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002542
Douglas Gregore7526412009-11-11 19:31:23 +00002543 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002544 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002545 if (CheckTemplateArgument(TempParm, Arg))
2546 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002547
Douglas Gregor910f8002010-11-07 23:05:16 +00002548 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002549 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002550
Douglas Gregore7526412009-11-11 19:31:23 +00002551 case TemplateArgument::Expression:
2552 case TemplateArgument::Type:
2553 // We have a template template parameter but the template
2554 // argument does not refer to a template.
2555 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2556 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002557
Douglas Gregore7526412009-11-11 19:31:23 +00002558 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002559 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002560 "Declaration argument with template template parameter");
2561 break;
2562 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002563 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002564 "Integral argument with template template parameter");
2565 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002566
Douglas Gregore7526412009-11-11 19:31:23 +00002567 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002568 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002569 break;
2570 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002571
Douglas Gregore7526412009-11-11 19:31:23 +00002572 return false;
2573}
2574
Douglas Gregorc15cb382009-02-09 23:23:08 +00002575/// \brief Check that the given template argument list is well-formed
2576/// for specializing the given template.
2577bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2578 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002579 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002580 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002581 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002582 TemplateParameterList *Params = Template->getTemplateParameters();
2583 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002584 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002585 bool Invalid = false;
2586
John McCalld5532b62009-11-23 01:53:49 +00002587 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2588
Mike Stump1eb44332009-09-09 15:08:12 +00002589 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002590 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002591
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002592 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002593 (NumArgs < Params->getMinRequiredArguments() &&
2594 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002595 // FIXME: point at either the first arg beyond what we can handle,
2596 // or the '>', depending on whether we have too many or too few
2597 // arguments.
2598 SourceRange Range;
2599 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002600 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002601 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2602 << (NumArgs > NumParams)
2603 << (isa<ClassTemplateDecl>(Template)? 0 :
2604 isa<FunctionTemplateDecl>(Template)? 1 :
2605 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2606 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002607 Diag(Template->getLocation(), diag::note_template_decl_here)
2608 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002609 Invalid = true;
2610 }
Mike Stump1eb44332009-09-09 15:08:12 +00002611
2612 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002613 // [...] The type and form of each template-argument specified in
2614 // a template-id shall match the type and form specified for the
2615 // corresponding parameter declared by the template in its
2616 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002617 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002618 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2619 TemplateParameterList::iterator Param = Params->begin(),
2620 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002621 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002622 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002623 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002624 if (ArgIdx > NumArgs && PartialTemplateArgs)
2625 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002626
Douglas Gregorf35f8282009-11-11 21:54:23 +00002627 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002628 // If we have an expanded parameter pack, make sure we don't have too
2629 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002630 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002631 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002632 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002633 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2634 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2635 << true
2636 << (isa<ClassTemplateDecl>(Template)? 0 :
2637 isa<FunctionTemplateDecl>(Template)? 1 :
2638 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2639 << Template;
2640 Diag(Template->getLocation(), diag::note_template_decl_here)
2641 << Params->getSourceRange();
2642 return true;
2643 }
2644 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002645
Douglas Gregorf35f8282009-11-11 21:54:23 +00002646 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002647 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2648 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002649 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002650 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002651
Douglas Gregor14be16b2010-12-20 16:57:52 +00002652 if ((*Param)->isTemplateParameterPack()) {
2653 // The template parameter was a template parameter pack, so take the
2654 // deduced argument and place it on the argument pack. Note that we
2655 // stay on the same template parameter so that we can deduce more
2656 // arguments.
2657 ArgumentPack.push_back(Converted.back());
2658 Converted.pop_back();
2659 } else {
2660 // Move to the next template parameter.
2661 ++Param;
2662 }
2663 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002664 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002665 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002666
2667 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002668 // arguments, just break out now and we'll fill in the argument pack below.
2669 if ((*Param)->isTemplateParameterPack())
2670 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002671
Douglas Gregorf35f8282009-11-11 21:54:23 +00002672 // We have a default template argument that we will use.
2673 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002674
Douglas Gregorf35f8282009-11-11 21:54:23 +00002675 // Retrieve the default template argument from the template
2676 // parameter. For each kind of template parameter, we substitute the
2677 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002678 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002679 // the default argument.
2680 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2681 if (!TTP->hasDefaultArgument()) {
2682 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2683 break;
2684 }
2685
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002686 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002687 Template,
2688 TemplateLoc,
2689 RAngleLoc,
2690 TTP,
2691 Converted);
2692 if (!ArgType)
2693 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002694
Douglas Gregorf35f8282009-11-11 21:54:23 +00002695 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2696 ArgType);
2697 } else if (NonTypeTemplateParmDecl *NTTP
2698 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2699 if (!NTTP->hasDefaultArgument()) {
2700 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2701 break;
2702 }
2703
John McCall60d7b3a2010-08-24 06:29:42 +00002704 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002705 TemplateLoc,
2706 RAngleLoc,
2707 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002708 Converted);
2709 if (E.isInvalid())
2710 return true;
2711
2712 Expr *Ex = E.takeAs<Expr>();
2713 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2714 } else {
2715 TemplateTemplateParmDecl *TempParm
2716 = cast<TemplateTemplateParmDecl>(*Param);
2717
2718 if (!TempParm->hasDefaultArgument()) {
2719 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2720 break;
2721 }
2722
Douglas Gregor1d752d72011-03-02 18:46:51 +00002723 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002724 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002725 TemplateLoc,
2726 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002727 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002728 Converted,
2729 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002730 if (Name.isNull())
2731 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002733 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2734 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002735 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002736
Douglas Gregorf35f8282009-11-11 21:54:23 +00002737 // Introduce an instantiation record that describes where we are using
2738 // the default template argument.
2739 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002740 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002741 SourceRange(TemplateLoc, RAngleLoc));
2742
Douglas Gregorf35f8282009-11-11 21:54:23 +00002743 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002744 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002745 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002746 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002747
Douglas Gregor67714232011-03-03 02:41:12 +00002748 // Core issue 150 (assumed resolution): if this is a template template
2749 // parameter, keep track of the default template arguments from the
2750 // template definition.
2751 if (isTemplateTemplateParameter)
2752 TemplateArgs.addArgument(Arg);
2753
Douglas Gregor14be16b2010-12-20 16:57:52 +00002754 // Move to the next template parameter and argument.
2755 ++Param;
2756 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002757 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002758
Douglas Gregor14be16b2010-12-20 16:57:52 +00002759 // Form argument packs for each of the parameter packs remaining.
2760 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002761 // If we're checking a partial list of template arguments, don't fill
2762 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002763
2764 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002765 if (PartialTemplateArgs && ArgumentPack.empty()) {
2766 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002767 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002768 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002769 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002770 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2771 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002772 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002773 ArgumentPack.clear();
2774 }
2775 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002776
Douglas Gregor14be16b2010-12-20 16:57:52 +00002777 ++Param;
2778 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002779
Douglas Gregorc15cb382009-02-09 23:23:08 +00002780 return Invalid;
2781}
2782
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002783namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002784 class UnnamedLocalNoLinkageFinder
2785 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002786 {
2787 Sema &S;
2788 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002790 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002791
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002792 public:
2793 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2794
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002795 bool Visit(QualType T) {
2796 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002797 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002798
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002799#define TYPE(Class, Parent) \
2800 bool Visit##Class##Type(const Class##Type *);
2801#define ABSTRACT_TYPE(Class, Parent) \
2802 bool Visit##Class##Type(const Class##Type *) { return false; }
2803#define NON_CANONICAL_TYPE(Class, Parent) \
2804 bool Visit##Class##Type(const Class##Type *) { return false; }
2805#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002806
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002807 bool VisitTagDecl(const TagDecl *Tag);
2808 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2809 };
2810}
2811
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002812bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002813 return false;
2814}
2815
2816bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2817 return Visit(T->getElementType());
2818}
2819
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002820bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002821 return Visit(T->getPointeeType());
2822}
2823
2824bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002825 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002826 return Visit(T->getPointeeType());
2827}
2828
2829bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002830 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002831 return Visit(T->getPointeeType());
2832}
2833
2834bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002835 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002836 return Visit(T->getPointeeType());
2837}
2838
2839bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002840 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002841 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2842}
2843
2844bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002845 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002846 return Visit(T->getElementType());
2847}
2848
2849bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002850 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002851 return Visit(T->getElementType());
2852}
2853
2854bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002855 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002856 return Visit(T->getElementType());
2857}
2858
2859bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002860 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002861 return Visit(T->getElementType());
2862}
2863
2864bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002865 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002866 return Visit(T->getElementType());
2867}
2868
2869bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2870 return Visit(T->getElementType());
2871}
2872
2873bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2874 return Visit(T->getElementType());
2875}
2876
2877bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2878 const FunctionProtoType* T) {
2879 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002880 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002881 A != AEnd; ++A) {
2882 if (Visit(*A))
2883 return true;
2884 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002885
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002886 return Visit(T->getResultType());
2887}
2888
2889bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2890 const FunctionNoProtoType* T) {
2891 return Visit(T->getResultType());
2892}
2893
2894bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2895 const UnresolvedUsingType*) {
2896 return false;
2897}
2898
2899bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2900 return false;
2901}
2902
2903bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2904 return Visit(T->getUnderlyingType());
2905}
2906
2907bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2908 return false;
2909}
2910
Richard Smith34b41d92011-02-20 03:19:35 +00002911bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2912 return Visit(T->getDeducedType());
2913}
2914
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002915bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2916 return VisitTagDecl(T->getDecl());
2917}
2918
2919bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2920 return VisitTagDecl(T->getDecl());
2921}
2922
2923bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2924 const TemplateTypeParmType*) {
2925 return false;
2926}
2927
Douglas Gregorc3069d62011-01-14 02:55:32 +00002928bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2929 const SubstTemplateTypeParmPackType *) {
2930 return false;
2931}
2932
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002933bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2934 const TemplateSpecializationType*) {
2935 return false;
2936}
2937
2938bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2939 const InjectedClassNameType* T) {
2940 return VisitTagDecl(T->getDecl());
2941}
2942
2943bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2944 const DependentNameType* T) {
2945 return VisitNestedNameSpecifier(T->getQualifier());
2946}
2947
2948bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2949 const DependentTemplateSpecializationType* T) {
2950 return VisitNestedNameSpecifier(T->getQualifier());
2951}
2952
Douglas Gregor7536dd52010-12-20 02:24:11 +00002953bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2954 const PackExpansionType* T) {
2955 return Visit(T->getPattern());
2956}
2957
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002958bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2959 return false;
2960}
2961
2962bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2963 const ObjCInterfaceType *) {
2964 return false;
2965}
2966
2967bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2968 const ObjCObjectPointerType *) {
2969 return false;
2970}
2971
2972bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2973 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2974 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2975 << S.Context.getTypeDeclType(Tag) << SR;
2976 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002977 }
2978
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002979 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2980 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2981 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2982 return true;
2983 }
2984
2985 return false;
2986}
2987
2988bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2989 NestedNameSpecifier *NNS) {
2990 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2991 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002992
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002993 switch (NNS->getKind()) {
2994 case NestedNameSpecifier::Identifier:
2995 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002996 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002997 case NestedNameSpecifier::Global:
2998 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002999
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003000 case NestedNameSpecifier::TypeSpec:
3001 case NestedNameSpecifier::TypeSpecWithTemplate:
3002 return Visit(QualType(NNS->getAsType(), 0));
3003 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003004 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003005}
3006
3007
Douglas Gregorc15cb382009-02-09 23:23:08 +00003008/// \brief Check a template argument against its corresponding
3009/// template type parameter.
3010///
3011/// This routine implements the semantics of C++ [temp.arg.type]. It
3012/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003013bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003014 TypeSourceInfo *ArgInfo) {
3015 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003016 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003017 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003018
3019 if (Arg->isVariablyModifiedType()) {
3020 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003021 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003022 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003023 }
3024
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003025 // C++03 [temp.arg.type]p2:
3026 // A local type, a type with no linkage, an unnamed type or a type
3027 // compounded from any of these types shall not be used as a
3028 // template-argument for a template type-parameter.
3029 //
3030 // C++0x allows these, and even in C++03 we allow them as an extension with
3031 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003032 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003033 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3034 (void)Finder.Visit(Context.getCanonicalType(Arg));
3035 }
3036
Douglas Gregorc15cb382009-02-09 23:23:08 +00003037 return false;
3038}
3039
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003040/// \brief Checks whether the given template argument is the address
3041/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003042static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003043CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3044 NonTypeTemplateParmDecl *Param,
3045 QualType ParamType,
3046 Expr *ArgIn,
3047 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003048 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003049 Expr *Arg = ArgIn;
3050 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003051
3052 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003053 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003054 Arg = Cast->getSubExpr();
3055
3056 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003057 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003058 // A template-argument for a non-type, non-template
3059 // template-parameter shall be one of: [...]
3060 //
3061 // -- the address of an object or function with external
3062 // linkage, including function templates and function
3063 // template-ids but excluding non-static class members,
3064 // expressed as & id-expression where the & is optional if
3065 // the name refers to a function or array, or if the
3066 // corresponding template-parameter is a reference; or
3067 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003068
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003069 // In C++98/03 mode, give an extension warning on any extra parentheses.
3070 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3071 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003072 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003073 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003074 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003075 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003076 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003077 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003078 }
3079
3080 Arg = Parens->getSubExpr();
3081 }
3082
Douglas Gregorb7a09262010-04-01 18:32:35 +00003083 bool AddressTaken = false;
3084 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003085 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003086 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003087 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003088 AddressTaken = true;
3089 AddrOpLoc = UnOp->getOperatorLoc();
3090 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003091 } else
3092 DRE = dyn_cast<DeclRefExpr>(Arg);
3093
Douglas Gregorb7a09262010-04-01 18:32:35 +00003094 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003095 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3096 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003097 S.Diag(Param->getLocation(), diag::note_template_param_here);
3098 return true;
3099 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003100
3101 // Stop checking the precise nature of the argument if it is value dependent,
3102 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003103 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003104 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003105 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003106 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003107
Douglas Gregorb7a09262010-04-01 18:32:35 +00003108 if (!isa<ValueDecl>(DRE->getDecl())) {
3109 S.Diag(Arg->getSourceRange().getBegin(),
3110 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003111 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003112 S.Diag(Param->getLocation(), diag::note_template_param_here);
3113 return true;
3114 }
3115
3116 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003117
3118 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003119 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3120 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003121 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003122 S.Diag(Param->getLocation(), diag::note_template_param_here);
3123 return true;
3124 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003125
3126 // Cannot refer to non-static member functions
3127 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003128 if (!Method->isStatic()) {
3129 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003130 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003131 S.Diag(Param->getLocation(), diag::note_template_param_here);
3132 return true;
3133 }
Mike Stump1eb44332009-09-09 15:08:12 +00003134
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003135 // Functions must have external linkage.
3136 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003137 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003138 S.Diag(Arg->getSourceRange().getBegin(),
3139 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003140 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003141 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003142 << true;
3143 return true;
3144 }
3145
3146 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003147 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003148
Douglas Gregorb7a09262010-04-01 18:32:35 +00003149 // If the template parameter has pointer type, the function decays.
3150 if (ParamType->isPointerType() && !AddressTaken)
3151 ArgType = S.Context.getPointerType(Func->getType());
3152 else if (AddressTaken && ParamType->isReferenceType()) {
3153 // If we originally had an address-of operator, but the
3154 // parameter has reference type, complain and (if things look
3155 // like they will work) drop the address-of operator.
3156 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3157 ParamType.getNonReferenceType())) {
3158 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3159 << ParamType;
3160 S.Diag(Param->getLocation(), diag::note_template_param_here);
3161 return true;
3162 }
3163
3164 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3165 << ParamType
3166 << FixItHint::CreateRemoval(AddrOpLoc);
3167 S.Diag(Param->getLocation(), diag::note_template_param_here);
3168
3169 ArgType = Func->getType();
3170 }
3171 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003172 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003173 S.Diag(Arg->getSourceRange().getBegin(),
3174 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003175 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003176 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003177 << true;
3178 return true;
3179 }
3180
Douglas Gregorb7a09262010-04-01 18:32:35 +00003181 // A value of reference type is not an object.
3182 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003183 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003184 diag::err_template_arg_reference_var)
3185 << Var->getType() << Arg->getSourceRange();
3186 S.Diag(Param->getLocation(), diag::note_template_param_here);
3187 return true;
3188 }
3189
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003190 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003191 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003192
3193 // If the template parameter has pointer type, we must have taken
3194 // the address of this object.
3195 if (ParamType->isReferenceType()) {
3196 if (AddressTaken) {
3197 // If we originally had an address-of operator, but the
3198 // parameter has reference type, complain and (if things look
3199 // like they will work) drop the address-of operator.
3200 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3201 ParamType.getNonReferenceType())) {
3202 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3203 << ParamType;
3204 S.Diag(Param->getLocation(), diag::note_template_param_here);
3205 return true;
3206 }
3207
3208 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3209 << ParamType
3210 << FixItHint::CreateRemoval(AddrOpLoc);
3211 S.Diag(Param->getLocation(), diag::note_template_param_here);
3212
3213 ArgType = Var->getType();
3214 }
3215 } else if (!AddressTaken && ParamType->isPointerType()) {
3216 if (Var->getType()->isArrayType()) {
3217 // Array-to-pointer decay.
3218 ArgType = S.Context.getArrayDecayedType(Var->getType());
3219 } else {
3220 // If the template parameter has pointer type but the address of
3221 // this object was not taken, complain and (possibly) recover by
3222 // taking the address of the entity.
3223 ArgType = S.Context.getPointerType(Var->getType());
3224 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3225 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3226 << ParamType;
3227 S.Diag(Param->getLocation(), diag::note_template_param_here);
3228 return true;
3229 }
3230
3231 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3232 << ParamType
3233 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3234
3235 S.Diag(Param->getLocation(), diag::note_template_param_here);
3236 }
3237 }
3238 } else {
3239 // We found something else, but we don't know specifically what it is.
3240 S.Diag(Arg->getSourceRange().getBegin(),
3241 diag::err_template_arg_not_object_or_func)
3242 << Arg->getSourceRange();
3243 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3244 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003245 }
Mike Stump1eb44332009-09-09 15:08:12 +00003246
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003247 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003248 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003249 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003250 // For pointer-to-object types, qualification conversions are
3251 // permitted.
3252 } else {
3253 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3254 if (!ParamRef->getPointeeType()->isFunctionType()) {
3255 // C++ [temp.arg.nontype]p5b3:
3256 // For a non-type template-parameter of type reference to
3257 // object, no conversions apply. The type referred to by the
3258 // reference may be more cv-qualified than the (otherwise
3259 // identical) type of the template- argument. The
3260 // template-parameter is bound directly to the
3261 // template-argument, which shall be an lvalue.
3262
3263 // FIXME: Other qualifiers?
3264 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3265 unsigned ArgQuals = ArgType.getCVRQualifiers();
3266
3267 if ((ParamQuals | ArgQuals) != ParamQuals) {
3268 S.Diag(Arg->getSourceRange().getBegin(),
3269 diag::err_template_arg_ref_bind_ignores_quals)
3270 << ParamType << Arg->getType()
3271 << Arg->getSourceRange();
3272 S.Diag(Param->getLocation(), diag::note_template_param_here);
3273 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003274 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003275 }
3276 }
3277
3278 // At this point, the template argument refers to an object or
3279 // function with external linkage. We now need to check whether the
3280 // argument and parameter types are compatible.
3281 if (!S.Context.hasSameUnqualifiedType(ArgType,
3282 ParamType.getNonReferenceType())) {
3283 // We can't perform this conversion or binding.
3284 if (ParamType->isReferenceType())
3285 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3286 << ParamType << Arg->getType() << Arg->getSourceRange();
3287 else
3288 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3289 << Arg->getType() << ParamType << Arg->getSourceRange();
3290 S.Diag(Param->getLocation(), diag::note_template_param_here);
3291 return true;
3292 }
3293 }
3294
3295 // Create the template argument.
3296 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003297 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003298 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003299}
3300
3301/// \brief Checks whether the given template argument is a pointer to
3302/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003303bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003304 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003305 bool Invalid = false;
3306
3307 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003308 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003309 Arg = Cast->getSubExpr();
3310
3311 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003312 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003313 // A template-argument for a non-type, non-template
3314 // template-parameter shall be one of: [...]
3315 //
3316 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003317 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003318
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003319 // In C++98/03 mode, give an extension warning on any extra parentheses.
3320 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3321 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003322 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003323 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003324 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003325 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003326 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003327 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003328 }
3329
3330 Arg = Parens->getSubExpr();
3331 }
3332
Douglas Gregorcaddba02009-11-12 18:38:13 +00003333 // A pointer-to-member constant written &Class::member.
3334 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003335 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003336 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3337 if (DRE && !DRE->getQualifier())
3338 DRE = 0;
3339 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003340 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003341 // A constant of pointer-to-member type.
3342 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3343 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3344 if (VD->getType()->isMemberPointerType()) {
3345 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003346 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003347 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3348 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003349 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003350 else
3351 Converted = TemplateArgument(VD->getCanonicalDecl());
3352 return Invalid;
3353 }
3354 }
3355 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003356
Douglas Gregorcaddba02009-11-12 18:38:13 +00003357 DRE = 0;
3358 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003359
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003360 if (!DRE)
3361 return Diag(Arg->getSourceRange().getBegin(),
3362 diag::err_template_arg_not_pointer_to_member_form)
3363 << Arg->getSourceRange();
3364
3365 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3366 assert((isa<FieldDecl>(DRE->getDecl()) ||
3367 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3368 "Only non-static member pointers can make it here");
3369
3370 // Okay: this is the address of a non-static member, and therefore
3371 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003372 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003373 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003374 else
3375 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003376 return Invalid;
3377 }
3378
3379 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003380 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003381 diag::err_template_arg_not_pointer_to_member_form)
3382 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003383 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003384 diag::note_template_arg_refers_here);
3385 return true;
3386}
3387
Douglas Gregorc15cb382009-02-09 23:23:08 +00003388/// \brief Check a template argument against its corresponding
3389/// non-type template parameter.
3390///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003391/// This routine implements the semantics of C++ [temp.arg.nontype].
3392/// It returns true if an error occurred, and false otherwise. \p
3393/// InstantiatedParamType is the type of the non-type template
3394/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003395///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003396/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003397bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003398 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003399 TemplateArgument &Converted,
3400 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003401 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3402
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003403 // If either the parameter has a dependent type or the argument is
3404 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003405 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3406 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003407 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003408 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003409 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003410
3411 // C++ [temp.arg.nontype]p5:
3412 // The following conversions are performed on each expression used
3413 // as a non-type template-argument. If a non-type
3414 // template-argument cannot be converted to the type of the
3415 // corresponding template-parameter then the program is
3416 // ill-formed.
3417 //
3418 // -- for a non-type template-parameter of integral or
3419 // enumeration type, integral promotions (4.5) and integral
3420 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003421 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003422 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003423 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003424 // C++ [temp.arg.nontype]p1:
3425 // A template-argument for a non-type, non-template
3426 // template-parameter shall be one of:
3427 //
3428 // -- an integral constant-expression of integral or enumeration
3429 // type; or
3430 // -- the name of a non-type template-parameter; or
3431 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003432 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003433 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003434 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003435 diag::err_template_arg_not_integral_or_enumeral)
3436 << ArgType << Arg->getSourceRange();
3437 Diag(Param->getLocation(), diag::note_template_param_here);
3438 return true;
3439 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003440 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003441 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3442 << ArgType << Arg->getSourceRange();
3443 return true;
3444 }
3445
Douglas Gregor02024a92010-03-28 02:42:43 +00003446 // From here on out, all we care about are the unqualified forms
3447 // of the parameter and argument types.
3448 ParamType = ParamType.getUnqualifiedType();
3449 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003450
3451 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003452 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003453 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003454 } else if (CTAK == CTAK_Deduced) {
3455 // C++ [temp.deduct.type]p17:
3456 // If, in the declaration of a function template with a non-type
3457 // template-parameter, the non-type template- parameter is used
3458 // in an expression in the function parameter-list and, if the
3459 // corresponding template-argument is deduced, the
3460 // template-argument type shall match the type of the
3461 // template-parameter exactly, except that a template-argument
3462 // deduced from an array bound may be of any integral type.
3463 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3464 << ArgType << ParamType;
3465 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003466 return true;
3467 } else if (ParamType->isBooleanType()) {
3468 // This is an integral-to-boolean conversion.
3469 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003470 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3471 !ParamType->isEnumeralType()) {
3472 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003473 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003474 } else {
3475 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003476 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003477 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003478 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003479 Diag(Param->getLocation(), diag::note_template_param_here);
3480 return true;
3481 }
3482
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003483 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003484 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003485 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003486
3487 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003488 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003489
3490 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003491 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003492 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003493 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003494 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003495 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003496
3497 // Complain if an unsigned parameter received a negative value.
3498 if (IntegerType->isUnsignedIntegerType()
3499 && (OldValue.isSigned() && OldValue.isNegative())) {
3500 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3501 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3502 << Arg->getSourceRange();
3503 Diag(Param->getLocation(), diag::note_template_param_here);
3504 }
3505
3506 // Complain if we overflowed the template parameter's type.
3507 unsigned RequiredBits;
3508 if (IntegerType->isUnsignedIntegerType())
3509 RequiredBits = OldValue.getActiveBits();
3510 else if (OldValue.isUnsigned())
3511 RequiredBits = OldValue.getActiveBits() + 1;
3512 else
3513 RequiredBits = OldValue.getMinSignedBits();
3514 if (RequiredBits > AllowedBits) {
3515 Diag(Arg->getSourceRange().getBegin(),
3516 diag::warn_template_arg_too_large)
3517 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3518 << Arg->getSourceRange();
3519 Diag(Param->getLocation(), diag::note_template_param_here);
3520 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003521 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003522
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003523 // Add the value of this argument to the list of converted
3524 // arguments. We use the bitwidth and signedness of the template
3525 // parameter.
3526 if (Arg->isValueDependent()) {
3527 // The argument is value-dependent. Create a new
3528 // TemplateArgument with the converted expression.
3529 Converted = TemplateArgument(Arg);
3530 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003531 }
3532
John McCall833ca992009-10-29 08:12:44 +00003533 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003534 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003535 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003536 return false;
3537 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003538
John McCall6bb80172010-03-30 21:47:33 +00003539 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3540
Douglas Gregorb7a09262010-04-01 18:32:35 +00003541 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3542 // from a template argument of type std::nullptr_t to a non-type
3543 // template parameter of type pointer to object, pointer to
3544 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003545 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003546 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3547 Converted = TemplateArgument((NamedDecl *)0);
3548 return false;
3549 }
3550
Douglas Gregorb86b0572009-02-11 01:18:59 +00003551 // Handle pointer-to-function, reference-to-function, and
3552 // pointer-to-member-function all in (roughly) the same way.
3553 if (// -- For a non-type template-parameter of type pointer to
3554 // function, only the function-to-pointer conversion (4.3) is
3555 // applied. If the template-argument represents a set of
3556 // overloaded functions (or a pointer to such), the matching
3557 // function is selected from the set (13.4).
3558 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003559 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003560 // -- For a non-type template-parameter of type reference to
3561 // function, no conversions apply. If the template-argument
3562 // represents a set of overloaded functions, the matching
3563 // function is selected from the set (13.4).
3564 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003565 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003566 // -- For a non-type template-parameter of type pointer to
3567 // member function, no conversions apply. If the
3568 // template-argument represents a set of overloaded member
3569 // functions, the matching member function is selected from
3570 // the set (13.4).
3571 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003572 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003573 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003574
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003575 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003576 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003577 true,
3578 FoundResult)) {
3579 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3580 return true;
3581
3582 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3583 ArgType = Arg->getType();
3584 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003585 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003586 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003587
Douglas Gregorb7a09262010-04-01 18:32:35 +00003588 if (!ParamType->isMemberPointerType())
3589 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003590 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003591 Arg, Converted);
3592
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003593 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3594 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003595 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003596 } else if (!Context.hasSameUnqualifiedType(ArgType,
3597 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003598 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003599 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003600 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003601 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003602 Diag(Param->getLocation(), diag::note_template_param_here);
3603 return true;
3604 }
Mike Stump1eb44332009-09-09 15:08:12 +00003605
Douglas Gregorb7a09262010-04-01 18:32:35 +00003606 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003607 }
3608
Chris Lattnerfe90de72009-02-20 21:37:53 +00003609 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003610 // -- for a non-type template-parameter of type pointer to
3611 // object, qualification conversions (4.4) and the
3612 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003613 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003614 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003615 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003616
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003617 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003618 ParamType,
3619 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003620 }
Mike Stump1eb44332009-09-09 15:08:12 +00003621
Ted Kremenek6217b802009-07-29 21:53:49 +00003622 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003623 // -- For a non-type template-parameter of type reference to
3624 // object, no conversions apply. The type referred to by the
3625 // reference may be more cv-qualified than the (otherwise
3626 // identical) type of the template-argument. The
3627 // template-parameter is bound directly to the
3628 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003629 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003630 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003631
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003632 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003633 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3634 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003635 true,
3636 FoundResult)) {
3637 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3638 return true;
3639
3640 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3641 ArgType = Arg->getType();
3642 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003643 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003644 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003645
3646 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003647 ParamType,
3648 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003649 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003650
3651 // -- For a non-type template-parameter of type pointer to data
3652 // member, qualification conversions (4.4) are applied.
3653 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3654
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003655 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003656 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003657 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003658 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003659 } else {
3660 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003661 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003662 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003663 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003664 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003665 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003666 }
3667
Douglas Gregorcaddba02009-11-12 18:38:13 +00003668 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003669}
3670
3671/// \brief Check a template argument against its corresponding
3672/// template template parameter.
3673///
3674/// This routine implements the semantics of C++ [temp.arg.template].
3675/// It returns true if an error occurred, and false otherwise.
3676bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003677 const TemplateArgumentLoc &Arg) {
3678 TemplateName Name = Arg.getArgument().getAsTemplate();
3679 TemplateDecl *Template = Name.getAsTemplateDecl();
3680 if (!Template) {
3681 // Any dependent template name is fine.
3682 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3683 return false;
3684 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003685
3686 // C++ [temp.arg.template]p1:
3687 // A template-argument for a template template-parameter shall be
3688 // the name of a class template, expressed as id-expression. Only
3689 // primary class templates are considered when matching the
3690 // template template argument with the corresponding parameter;
3691 // partial specializations are not considered even if their
3692 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003693 //
3694 // Note that we also allow template template parameters here, which
3695 // will happen when we are dealing with, e.g., class template
3696 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003697 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003698 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003699 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003700 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003701 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003702 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003703 << Template;
3704 }
3705
3706 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3707 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003708 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003709 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003710 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003711}
3712
Douglas Gregor02024a92010-03-28 02:42:43 +00003713/// \brief Given a non-type template argument that refers to a
3714/// declaration and the type of its corresponding non-type template
3715/// parameter, produce an expression that properly refers to that
3716/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003717ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003718Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3719 QualType ParamType,
3720 SourceLocation Loc) {
3721 assert(Arg.getKind() == TemplateArgument::Declaration &&
3722 "Only declaration template arguments permitted here");
3723 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3724
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003725 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003726 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3727 // If the value is a class member, we might have a pointer-to-member.
3728 // Determine whether the non-type template template parameter is of
3729 // pointer-to-member type. If so, we need to build an appropriate
3730 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3731 // would refer to the member itself.
3732 if (ParamType->isMemberPointerType()) {
3733 QualType ClassType
3734 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3735 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003736 = NestedNameSpecifier::Create(Context, 0, false,
3737 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003738 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003739 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003740
3741 // The actual value-ness of this is unimportant, but for
3742 // internal consistency's sake, references to instance methods
3743 // are r-values.
3744 ExprValueKind VK = VK_LValue;
3745 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3746 VK = VK_RValue;
3747
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003748 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003749 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003750 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003751 Loc,
3752 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003753 if (RefExpr.isInvalid())
3754 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003755
John McCall2de56d12010-08-25 11:45:40 +00003756 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003757
Douglas Gregorc0c83002010-04-30 21:46:38 +00003758 // We might need to perform a trailing qualification conversion, since
3759 // the element type on the parameter could be more qualified than the
3760 // element type in the expression we constructed.
3761 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003762 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003763 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003764 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003765 RefExpr = Owned(RefE);
3766 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003767
Douglas Gregor02024a92010-03-28 02:42:43 +00003768 assert(!RefExpr.isInvalid() &&
3769 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003770 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003771 return move(RefExpr);
3772 }
3773 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003774
Douglas Gregor02024a92010-03-28 02:42:43 +00003775 QualType T = VD->getType().getNonReferenceType();
3776 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003777 // When the non-type template parameter is a pointer, take the
3778 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003779 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003780 if (RefExpr.isInvalid())
3781 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003782
3783 if (T->isFunctionType() || T->isArrayType()) {
3784 // Decay functions and arrays.
3785 Expr *RefE = (Expr *)RefExpr.get();
3786 DefaultFunctionArrayConversion(RefE);
3787 if (RefE != RefExpr.get()) {
3788 RefExpr.release();
3789 RefExpr = Owned(RefE);
3790 }
3791
3792 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003793 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003794
Douglas Gregorb7a09262010-04-01 18:32:35 +00003795 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003796 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003797 }
3798
John McCallf89e55a2010-11-18 06:31:45 +00003799 ExprValueKind VK = VK_RValue;
3800
Douglas Gregor02024a92010-03-28 02:42:43 +00003801 // If the non-type template parameter has reference type, qualify the
3802 // resulting declaration reference with the extra qualifiers on the
3803 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003804 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3805 VK = VK_LValue;
3806 T = Context.getQualifiedType(T,
3807 TargetRef->getPointeeType().getQualifiers());
3808 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003809
John McCallf89e55a2010-11-18 06:31:45 +00003810 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003811}
3812
3813/// \brief Construct a new expression that refers to the given
3814/// integral template argument with the given source-location
3815/// information.
3816///
3817/// This routine takes care of the mapping from an integral template
3818/// argument (which may have any integral type) to the appropriate
3819/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003820ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003821Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3822 SourceLocation Loc) {
3823 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003824 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003825 QualType T = Arg.getIntegralType();
3826 if (T->isCharType() || T->isWideCharType())
3827 return Owned(new (Context) CharacterLiteral(
3828 Arg.getAsIntegral()->getZExtValue(),
3829 T->isWideCharType(),
3830 T,
3831 Loc));
3832 if (T->isBooleanType())
3833 return Owned(new (Context) CXXBoolLiteralExpr(
3834 Arg.getAsIntegral()->getBoolValue(),
3835 T,
3836 Loc));
3837
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003838 QualType BT;
3839 if (const EnumType *ET = T->getAs<EnumType>())
3840 BT = ET->getDecl()->getPromotionType();
3841 else
3842 BT = T;
3843
3844 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003845 if (T->isEnumeralType()) {
3846 // FIXME: This is a hack. We need a better way to handle substituted
3847 // non-type template parameters.
3848 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3849 E, 0,
3850 Context.getTrivialTypeSourceInfo(T, Loc),
3851 Loc, Loc);
3852 }
3853
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003854 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003855}
3856
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003857/// \brief Match two template parameters within template parameter lists.
3858static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3859 bool Complain,
3860 Sema::TemplateParameterListEqualKind Kind,
3861 SourceLocation TemplateArgLoc) {
3862 // Check the actual kind (type, non-type, template).
3863 if (Old->getKind() != New->getKind()) {
3864 if (Complain) {
3865 unsigned NextDiag = diag::err_template_param_different_kind;
3866 if (TemplateArgLoc.isValid()) {
3867 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3868 NextDiag = diag::note_template_param_different_kind;
3869 }
3870 S.Diag(New->getLocation(), NextDiag)
3871 << (Kind != Sema::TPL_TemplateMatch);
3872 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3873 << (Kind != Sema::TPL_TemplateMatch);
3874 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003875
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003876 return false;
3877 }
3878
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003879 // Check that both are parameter packs are neither are parameter packs.
3880 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003881 // template template parameter, the template template parameter can have
3882 // a parameter pack where the template template argument does not.
3883 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3884 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3885 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003886 if (Complain) {
3887 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3888 if (TemplateArgLoc.isValid()) {
3889 S.Diag(TemplateArgLoc,
3890 diag::err_template_arg_template_params_mismatch);
3891 NextDiag = diag::note_template_parameter_pack_non_pack;
3892 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003893
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003894 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3895 : isa<NonTypeTemplateParmDecl>(New)? 1
3896 : 2;
3897 S.Diag(New->getLocation(), NextDiag)
3898 << ParamKind << New->isParameterPack();
3899 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3900 << ParamKind << Old->isParameterPack();
3901 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003902
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003903 return false;
3904 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003905
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003906 // For non-type template parameters, check the type of the parameter.
3907 if (NonTypeTemplateParmDecl *OldNTTP
3908 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3909 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003910
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003911 // If we are matching a template template argument to a template
3912 // template parameter and one of the non-type template parameter types
3913 // is dependent, then we must wait until template instantiation time
3914 // to actually compare the arguments.
3915 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3916 (OldNTTP->getType()->isDependentType() ||
3917 NewNTTP->getType()->isDependentType()))
3918 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003919
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003920 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3921 if (Complain) {
3922 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3923 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003924 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003925 diag::err_template_arg_template_params_mismatch);
3926 NextDiag = diag::note_template_nontype_parm_different_type;
3927 }
3928 S.Diag(NewNTTP->getLocation(), NextDiag)
3929 << NewNTTP->getType()
3930 << (Kind != Sema::TPL_TemplateMatch);
3931 S.Diag(OldNTTP->getLocation(),
3932 diag::note_template_nontype_parm_prev_declaration)
3933 << OldNTTP->getType();
3934 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003935
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003936 return false;
3937 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003938
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003939 return true;
3940 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003941
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003942 // For template template parameters, check the template parameter types.
3943 // The template parameter lists of template template
3944 // parameters must agree.
3945 if (TemplateTemplateParmDecl *OldTTP
3946 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003947 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003948 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3949 OldTTP->getTemplateParameters(),
3950 Complain,
3951 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003952 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003953 : Kind),
3954 TemplateArgLoc);
3955 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003956
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003957 return true;
3958}
Douglas Gregor02024a92010-03-28 02:42:43 +00003959
Douglas Gregora0347822011-01-13 00:08:50 +00003960/// \brief Diagnose a known arity mismatch when comparing template argument
3961/// lists.
3962static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003963void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003964 TemplateParameterList *New,
3965 TemplateParameterList *Old,
3966 Sema::TemplateParameterListEqualKind Kind,
3967 SourceLocation TemplateArgLoc) {
3968 unsigned NextDiag = diag::err_template_param_list_different_arity;
3969 if (TemplateArgLoc.isValid()) {
3970 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3971 NextDiag = diag::note_template_param_list_different_arity;
3972 }
3973 S.Diag(New->getTemplateLoc(), NextDiag)
3974 << (New->size() > Old->size())
3975 << (Kind != Sema::TPL_TemplateMatch)
3976 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3977 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3978 << (Kind != Sema::TPL_TemplateMatch)
3979 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3980}
3981
Douglas Gregorddc29e12009-02-06 22:42:48 +00003982/// \brief Determine whether the given template parameter lists are
3983/// equivalent.
3984///
Mike Stump1eb44332009-09-09 15:08:12 +00003985/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003986/// source code as part of a new template declaration.
3987///
3988/// \param Old The old template parameter list, typically found via
3989/// name lookup of the template declared with this template parameter
3990/// list.
3991///
3992/// \param Complain If true, this routine will produce a diagnostic if
3993/// the template parameter lists are not equivalent.
3994///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003995/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003996///
3997/// \param TemplateArgLoc If this source location is valid, then we
3998/// are actually checking the template parameter list of a template
3999/// argument (New) against the template parameter list of its
4000/// corresponding template template parameter (Old). We produce
4001/// slightly different diagnostics in this scenario.
4002///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004003/// \returns True if the template parameter lists are equal, false
4004/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004005bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004006Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4007 TemplateParameterList *Old,
4008 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004009 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004010 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004011 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4012 if (Complain)
4013 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4014 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004015
4016 return false;
4017 }
4018
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004019 // C++0x [temp.arg.template]p3:
4020 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004021 // when each of the template parameters in the template-parameter-list of
4022 // the template-argument's corresponding class template or template alias
4023 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004024 // template-parameter-list of P. [...]
4025 TemplateParameterList::iterator NewParm = New->begin();
4026 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004027 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004028 OldParmEnd = Old->end();
4029 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004030 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4031 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004032 if (NewParm == NewParmEnd) {
4033 if (Complain)
4034 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4035 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004036
Douglas Gregora0347822011-01-13 00:08:50 +00004037 return false;
4038 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004039
Douglas Gregora0347822011-01-13 00:08:50 +00004040 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4041 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004042 return false;
4043
Douglas Gregora0347822011-01-13 00:08:50 +00004044 ++NewParm;
4045 continue;
4046 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004047
Douglas Gregora0347822011-01-13 00:08:50 +00004048 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004049 // [...] When P's template- parameter-list contains a template parameter
4050 // pack (14.5.3), the template parameter pack will match zero or more
4051 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004052 // template-parameter-list of A with the same type and form as the
4053 // template parameter pack in P (ignoring whether those template
4054 // parameters are template parameter packs).
4055 for (; NewParm != NewParmEnd; ++NewParm) {
4056 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4057 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004058 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004059 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004060 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004061
Douglas Gregora0347822011-01-13 00:08:50 +00004062 // Make sure we exhausted all of the arguments.
4063 if (NewParm != NewParmEnd) {
4064 if (Complain)
4065 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4066 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004067
Douglas Gregora0347822011-01-13 00:08:50 +00004068 return false;
4069 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004070
Douglas Gregorddc29e12009-02-06 22:42:48 +00004071 return true;
4072}
4073
4074/// \brief Check whether a template can be declared within this scope.
4075///
4076/// If the template declaration is valid in this scope, returns
4077/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004078bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004079Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004080 // Find the nearest enclosing declaration scope.
4081 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4082 (S->getFlags() & Scope::TemplateParamScope) != 0)
4083 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004084
Douglas Gregorddc29e12009-02-06 22:42:48 +00004085 // C++ [temp]p2:
4086 // A template-declaration can appear only as a namespace scope or
4087 // class scope declaration.
4088 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004089 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4090 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004091 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004092 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004093
Eli Friedman1503f772009-07-31 01:43:05 +00004094 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004095 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004096
4097 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4098 return false;
4099
Mike Stump1eb44332009-09-09 15:08:12 +00004100 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004101 diag::err_template_outside_namespace_or_class_scope)
4102 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004103}
Douglas Gregorcc636682009-02-17 23:15:12 +00004104
Douglas Gregord5cb8762009-10-07 00:13:32 +00004105/// \brief Determine what kind of template specialization the given declaration
4106/// is.
4107static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4108 if (!D)
4109 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004110
Douglas Gregorf6b11852009-10-08 15:14:33 +00004111 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4112 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004113 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4114 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004115 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4116 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004117
Douglas Gregord5cb8762009-10-07 00:13:32 +00004118 return TSK_Undeclared;
4119}
4120
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004121/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004122/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004123///
Douglas Gregor9302da62009-10-14 23:50:59 +00004124/// This routine determines whether a template specialization can be declared
4125/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004126///
4127/// \param S the semantic analysis object for which this check is being
4128/// performed.
4129///
4130/// \param Specialized the entity being specialized or instantiated, which
4131/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004132/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004133/// member class).
4134///
4135/// \param PrevDecl the previous declaration of this entity, if any.
4136///
4137/// \param Loc the location of the explicit specialization or instantiation of
4138/// this entity.
4139///
4140/// \param IsPartialSpecialization whether this is a partial specialization of
4141/// a class template.
4142///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004143/// \returns true if there was an error that we cannot recover from, false
4144/// otherwise.
4145static bool CheckTemplateSpecializationScope(Sema &S,
4146 NamedDecl *Specialized,
4147 NamedDecl *PrevDecl,
4148 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004149 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004150 // Keep these "kind" numbers in sync with the %select statements in the
4151 // various diagnostics emitted by this routine.
4152 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004153 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004154 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004155 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004156 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004157 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004158 EntityKind = 3;
4159 else if (isa<VarDecl>(Specialized))
4160 EntityKind = 4;
4161 else if (isa<RecordDecl>(Specialized))
4162 EntityKind = 5;
4163 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004164 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4165 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004166 return true;
4167 }
4168
Douglas Gregor88b70942009-02-25 22:02:03 +00004169 // C++ [temp.expl.spec]p2:
4170 // An explicit specialization shall be declared in the namespace
4171 // of which the template is a member, or, for member templates, in
4172 // the namespace of which the enclosing class or enclosing class
4173 // template is a member. An explicit specialization of a member
4174 // function, member class or static data member of a class
4175 // template shall be declared in the namespace of which the class
4176 // template is a member. Such a declaration may also be a
4177 // definition. If the declaration is not a definition, the
4178 // specialization may be defined later in the name- space in which
4179 // the explicit specialization was declared, or in a namespace
4180 // that encloses the one in which the explicit specialization was
4181 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004182 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004183 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004184 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004185 return true;
4186 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004187
Douglas Gregor0a407472009-10-07 17:30:37 +00004188 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4189 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004190 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004191 return true;
4192 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004193
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004194 // C++ [temp.class.spec]p6:
4195 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004196 // in any namespace scope in which its definition may be defined (14.5.1
4197 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004198 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004199 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004200 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004201 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004202 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004203 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4204 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004205 // C++ [temp.exp.spec]p2:
4206 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004207 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004208 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004209 // An explicit specialization of a member function, member class or
4210 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004211 // namespace of which the class template is a member.
4212 //
4213 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004214 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004215 // the specialized template.
4216 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4217 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004218 bool IsCPlusPlus0xExtension
4219 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004220 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004221 S.Diag(Loc, IsCPlusPlus0xExtension
4222 ? diag::ext_template_spec_decl_out_of_scope_global
4223 : diag::err_template_spec_decl_out_of_scope_global)
4224 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004225 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004226 S.Diag(Loc, IsCPlusPlus0xExtension
4227 ? diag::ext_template_spec_decl_out_of_scope
4228 : diag::err_template_spec_decl_out_of_scope)
4229 << EntityKind << Specialized
4230 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004231
Douglas Gregor9302da62009-10-14 23:50:59 +00004232 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4233 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004234 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004235 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004236
4237 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004238 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004239 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004240 // specializations of function templates, static data members, and member
4241 // functions, so we skip the check here for those kinds of entities.
4242 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004243 // Should we refactor that check, so that it occurs later?
4244 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004245 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4246 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004247 if (isa<TranslationUnitDecl>(SpecializedContext))
4248 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4249 << EntityKind << Specialized;
4250 else if (isa<NamespaceDecl>(SpecializedContext))
4251 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4252 << EntityKind << Specialized
4253 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004254
Douglas Gregor9302da62009-10-14 23:50:59 +00004255 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004256 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004257
Douglas Gregord5cb8762009-10-07 00:13:32 +00004258 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004259
Douglas Gregor88b70942009-02-25 22:02:03 +00004260 return false;
4261}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004262
Douglas Gregorbacb9492011-01-03 21:13:47 +00004263/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4264/// that checks non-type template partial specialization arguments.
4265static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4266 NonTypeTemplateParmDecl *Param,
4267 const TemplateArgument *Args,
4268 unsigned NumArgs) {
4269 for (unsigned I = 0; I != NumArgs; ++I) {
4270 if (Args[I].getKind() == TemplateArgument::Pack) {
4271 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004272 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004273 Args[I].pack_size()))
4274 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004275
Douglas Gregore94866f2009-06-12 21:21:02 +00004276 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004278
Douglas Gregorbacb9492011-01-03 21:13:47 +00004279 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004280 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004281 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004282 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004283
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004284 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004285 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4286 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004287
4288 // Strip off any implicit casts we added as part of type checking.
4289 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4290 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004291
Douglas Gregore94866f2009-06-12 21:21:02 +00004292 // C++ [temp.class.spec]p8:
4293 // A non-type argument is non-specialized if it is the name of a
4294 // non-type parameter. All other non-type arguments are
4295 // specialized.
4296 //
4297 // Below, we check the two conditions that only apply to
4298 // specialized non-type arguments, so skip any non-specialized
4299 // arguments.
4300 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004301 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004302 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004303
Douglas Gregore94866f2009-06-12 21:21:02 +00004304 // C++ [temp.class.spec]p9:
4305 // Within the argument list of a class template partial
4306 // specialization, the following restrictions apply:
4307 // -- A partially specialized non-type argument expression
4308 // shall not involve a template parameter of the partial
4309 // specialization except when the argument expression is a
4310 // simple identifier.
4311 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004312 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004313 diag::err_dependent_non_type_arg_in_partial_spec)
4314 << ArgExpr->getSourceRange();
4315 return true;
4316 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004317
Douglas Gregore94866f2009-06-12 21:21:02 +00004318 // -- The type of a template parameter corresponding to a
4319 // specialized non-type argument shall not be dependent on a
4320 // parameter of the specialization.
4321 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004322 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004323 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4324 << Param->getType()
4325 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004326 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004327 return true;
4328 }
4329 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004330
Douglas Gregorbacb9492011-01-03 21:13:47 +00004331 return false;
4332}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004333
Douglas Gregorbacb9492011-01-03 21:13:47 +00004334/// \brief Check the non-type template arguments of a class template
4335/// partial specialization according to C++ [temp.class.spec]p9.
4336///
4337/// \param TemplateParams the template parameters of the primary class
4338/// template.
4339///
4340/// \param TemplateArg the template arguments of the class template
4341/// partial specialization.
4342///
4343/// \returns true if there was an error, false otherwise.
4344static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4345 TemplateParameterList *TemplateParams,
4346 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4347 const TemplateArgument *ArgList = TemplateArgs.data();
4348
4349 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4350 NonTypeTemplateParmDecl *Param
4351 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4352 if (!Param)
4353 continue;
4354
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004355 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004356 &ArgList[I], 1))
4357 return true;
4358 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004359
4360 return false;
4361}
4362
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004363/// \brief Retrieve the previous declaration of the given declaration.
4364static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4365 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4366 return VD->getPreviousDeclaration();
4367 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4368 return FD->getPreviousDeclaration();
4369 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4370 return TD->getPreviousDeclaration();
4371 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4372 return TD->getPreviousDeclaration();
4373 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4374 return FTD->getPreviousDeclaration();
4375 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4376 return CTD->getPreviousDeclaration();
4377 return 0;
4378}
4379
John McCalld226f652010-08-21 09:40:31 +00004380DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004381Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4382 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004383 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004384 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004385 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004386 SourceLocation TemplateNameLoc,
4387 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004388 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004389 SourceLocation RAngleLoc,
4390 AttributeList *Attr,
4391 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004392 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004393
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004394 // NOTE: KWLoc is the location of the tag keyword. This will instead
4395 // store the location of the outermost template keyword in the declaration.
4396 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4397 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4398
Douglas Gregorcc636682009-02-17 23:15:12 +00004399 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004400 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004401 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004402 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4403
4404 if (!ClassTemplate) {
4405 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004406 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004407 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4408 return true;
4409 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004410
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004411 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004412 bool isPartialSpecialization = false;
4413
Douglas Gregor88b70942009-02-25 22:02:03 +00004414 // Check the validity of the template headers that introduce this
4415 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004416 // FIXME: We probably shouldn't complain about these headers for
4417 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004418 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004419 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004420 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4421 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004422 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004423 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004424 isExplicitSpecialization,
4425 Invalid);
4426 if (Invalid)
4427 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004428
Douglas Gregor05396e22009-08-25 17:23:04 +00004429 if (TemplateParams && TemplateParams->size() > 0) {
4430 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004431
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004432 if (TUK == TUK_Friend) {
4433 Diag(KWLoc, diag::err_partial_specialization_friend)
4434 << SourceRange(LAngleLoc, RAngleLoc);
4435 return true;
4436 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004437
Douglas Gregor05396e22009-08-25 17:23:04 +00004438 // C++ [temp.class.spec]p10:
4439 // The template parameter list of a specialization shall not
4440 // contain default template argument values.
4441 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4442 Decl *Param = TemplateParams->getParam(I);
4443 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4444 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004445 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004446 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004447 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004448 }
4449 } else if (NonTypeTemplateParmDecl *NTTP
4450 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4451 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004452 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004453 diag::err_default_arg_in_partial_spec)
4454 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004455 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004456 }
4457 } else {
4458 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004459 if (TTP->hasDefaultArgument()) {
4460 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004461 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004462 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004463 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004464 }
4465 }
4466 }
Douglas Gregora735b202009-10-13 14:39:41 +00004467 } else if (TemplateParams) {
4468 if (TUK == TUK_Friend)
4469 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004470 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004471 SourceRange(TemplateParams->getTemplateLoc(),
4472 TemplateParams->getRAngleLoc()))
4473 << SourceRange(LAngleLoc, RAngleLoc);
4474 else
4475 isExplicitSpecialization = true;
4476 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004477 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004478 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004479 isExplicitSpecialization = true;
4480 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004481
Douglas Gregorcc636682009-02-17 23:15:12 +00004482 // Check that the specialization uses the same tag kind as the
4483 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004484 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4485 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004486 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004487 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004488 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004489 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004490 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004491 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004492 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004493 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004494 diag::note_previous_use);
4495 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4496 }
4497
Douglas Gregor40808ce2009-03-09 23:48:35 +00004498 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004499 TemplateArgumentListInfo TemplateArgs;
4500 TemplateArgs.setLAngleLoc(LAngleLoc);
4501 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004502 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004503
Douglas Gregor925910d2011-01-03 20:35:03 +00004504 // Check for unexpanded parameter packs in any of the template arguments.
4505 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004506 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004507 UPPC_PartialSpecialization))
4508 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004509
Douglas Gregorcc636682009-02-17 23:15:12 +00004510 // Check that the template argument list is well-formed for this
4511 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004512 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004513 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4514 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004515 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004516
Douglas Gregor910f8002010-11-07 23:05:16 +00004517 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004518 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004519
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004520 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004521 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004522 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004523 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004524 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004525 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004526 return true;
4527
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004528 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004529 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004530 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004531 TemplateArgs.size())) {
4532 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4533 << ClassTemplate->getDeclName();
4534 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004535 }
4536 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004537
Douglas Gregorcc636682009-02-17 23:15:12 +00004538 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004539 ClassTemplateSpecializationDecl *PrevDecl = 0;
4540
4541 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004542 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004543 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004544 = ClassTemplate->findPartialSpecialization(Converted.data(),
4545 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004546 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004547 else
4548 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004549 = ClassTemplate->findSpecialization(Converted.data(),
4550 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004551
4552 ClassTemplateSpecializationDecl *Specialization = 0;
4553
Douglas Gregor88b70942009-02-25 22:02:03 +00004554 // Check whether we can declare a class template specialization in
4555 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004556 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004557 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4558 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004559 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004560 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004561
Douglas Gregorb88e8882009-07-30 17:40:51 +00004562 // The canonical type
4563 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004564 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004565 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004566 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004567 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004568 // arguments was referenced but not declared, or we're only
4569 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004570 // declaration node as our own, updating its source location and
4571 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004572 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004573 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004574 if (TemplateParameterLists.size() > 0) {
4575 Specialization->setTemplateParameterListsInfo(Context,
4576 TemplateParameterLists.size(),
4577 (TemplateParameterList**) TemplateParameterLists.release());
4578 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004579 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004580 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004581 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004582 // Build the canonical type that describes the converted template
4583 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004584 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4585 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004586 Converted.data(),
4587 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004588
4589 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004590 ClassTemplate->getInjectedClassNameSpecialization())) {
4591 // C++ [temp.class.spec]p9b3:
4592 //
4593 // -- The argument list of the specialization shall not be identical
4594 // to the implicit argument list of the primary template.
4595 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4596 << (TUK == TUK_Definition)
4597 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4598 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4599 ClassTemplate->getIdentifier(),
4600 TemplateNameLoc,
4601 Attr,
4602 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004603 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004604 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004605 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004606 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004607
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004608 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004609 ClassTemplatePartialSpecializationDecl *PrevPartial
4610 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004611 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004612 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004613 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004614 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004615 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004616 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004617 TemplateParams,
4618 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004619 Converted.data(),
4620 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004621 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004622 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004623 PrevPartial,
4624 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004625 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004626 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004627 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004628 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004629 (TemplateParameterList**) TemplateParameterLists.release());
4630 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004631
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004632 if (!PrevPartial)
4633 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004634 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004635
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004636 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004637 // template specialization, make a note of that.
4638 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4639 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004640
Douglas Gregor031a5882009-06-13 00:26:55 +00004641 // Check that all of the template parameters of the class template
4642 // partial specialization are deducible from the template
4643 // arguments. If not, this class template partial specialization
4644 // will never be used.
4645 llvm::SmallVector<bool, 8> DeducibleParams;
4646 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004647 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004648 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004649 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004650 unsigned NumNonDeducible = 0;
4651 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4652 if (!DeducibleParams[I])
4653 ++NumNonDeducible;
4654
4655 if (NumNonDeducible) {
4656 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4657 << (NumNonDeducible > 1)
4658 << SourceRange(TemplateNameLoc, RAngleLoc);
4659 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4660 if (!DeducibleParams[I]) {
4661 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4662 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004663 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004664 diag::note_partial_spec_unused_parameter)
4665 << Param->getDeclName();
4666 else
Mike Stump1eb44332009-09-09 15:08:12 +00004667 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004668 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004669 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004670 }
4671 }
4672 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004673 } else {
4674 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004675 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004676 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004677 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004678 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004679 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004680 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004681 Converted.data(),
4682 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004683 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004684 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004685 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004686 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004687 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00004688 (TemplateParameterList**) TemplateParameterLists.release());
4689 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004690
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004691 if (!PrevDecl)
4692 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004693
4694 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004695 }
4696
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004697 // C++ [temp.expl.spec]p6:
4698 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004699 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004700 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004701 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004702 // use occurs; no diagnostic is required.
4703 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004704 bool Okay = false;
4705 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4706 // Is there any previous explicit specialization declaration?
4707 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4708 Okay = true;
4709 break;
4710 }
4711 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004712
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004713 if (!Okay) {
4714 SourceRange Range(TemplateNameLoc, RAngleLoc);
4715 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4716 << Context.getTypeDeclType(Specialization) << Range;
4717
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004718 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004719 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004720 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004721 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004722 return true;
4723 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004724 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004725
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004726 // If this is not a friend, note that this is an explicit specialization.
4727 if (TUK != TUK_Friend)
4728 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004729
4730 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004731 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004732 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004733 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004734 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004735 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004736 Diag(Def->getLocation(), diag::note_previous_definition);
4737 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004738 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004739 }
4740 }
4741
John McCall7f1b9872010-12-18 03:30:47 +00004742 if (Attr)
4743 ProcessDeclAttributeList(S, Specialization, Attr);
4744
Douglas Gregorfc705b82009-02-26 22:19:44 +00004745 // Build the fully-sugared type for this class template
4746 // specialization as the user wrote in the specialization
4747 // itself. This means that we'll pretty-print the type retrieved
4748 // from the specialization's declaration the way that the user
4749 // actually wrote the specialization, rather than formatting the
4750 // name based on the "canonical" representation used to store the
4751 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004752 TypeSourceInfo *WrittenTy
4753 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4754 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004755 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004756 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004757 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004758 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004759 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004760
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004761 // C++ [temp.expl.spec]p9:
4762 // A template explicit specialization is in the scope of the
4763 // namespace in which the template was defined.
4764 //
4765 // We actually implement this paragraph where we set the semantic
4766 // context (in the creation of the ClassTemplateSpecializationDecl),
4767 // but we also maintain the lexical context where the actual
4768 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004769 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004770
Douglas Gregorcc636682009-02-17 23:15:12 +00004771 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004772 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004773 Specialization->startDefinition();
4774
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004775 if (TUK == TUK_Friend) {
4776 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4777 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004778 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004779 /*FIXME:*/KWLoc);
4780 Friend->setAccess(AS_public);
4781 CurContext->addDecl(Friend);
4782 } else {
4783 // Add the specialization into its lexical context, so that it can
4784 // be seen when iterating through the list of declarations in that
4785 // context. However, specializations are not found by name lookup.
4786 CurContext->addDecl(Specialization);
4787 }
John McCalld226f652010-08-21 09:40:31 +00004788 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004789}
Douglas Gregord57959a2009-03-27 23:10:48 +00004790
John McCalld226f652010-08-21 09:40:31 +00004791Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004792 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004793 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004794 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4795}
4796
John McCalld226f652010-08-21 09:40:31 +00004797Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004798 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004799 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004800 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004801 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004802
Douglas Gregor52591bf2009-06-24 00:54:41 +00004803 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004804 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004805 }
Mike Stump1eb44332009-09-09 15:08:12 +00004806
Douglas Gregor52591bf2009-06-24 00:54:41 +00004807 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004808
John McCalld226f652010-08-21 09:40:31 +00004809 Decl *DP = HandleDeclarator(ParentScope, D,
4810 move(TemplateParameterLists),
4811 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004812 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004813 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004814 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004815 FunctionTemplate->getTemplatedDecl());
4816 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4817 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4818 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004819}
4820
John McCall75042392010-02-11 01:33:53 +00004821/// \brief Strips various properties off an implicit instantiation
4822/// that has just been explicitly specialized.
4823static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004824 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004825
4826 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4827 FD->setInlineSpecified(false);
4828 }
4829}
4830
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004831/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004832/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004833/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004834/// new specialization/instantiation will have any effect.
4835///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004836/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004837/// instantiation.
4838///
4839/// \param NewTSK the kind of the new explicit specialization or instantiation.
4840///
4841/// \param PrevDecl the previous declaration of the entity.
4842///
4843/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4844///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004845/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004846/// declaration was instantiated (either implicitly or explicitly).
4847///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004848/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004849/// specialization or instantiation has no effect and should be ignored.
4850///
4851/// \returns true if there was an error that should prevent the introduction of
4852/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004853bool
4854Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4855 TemplateSpecializationKind NewTSK,
4856 NamedDecl *PrevDecl,
4857 TemplateSpecializationKind PrevTSK,
4858 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004859 bool &HasNoEffect) {
4860 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004861
Douglas Gregor454885e2009-10-15 15:54:05 +00004862 switch (NewTSK) {
4863 case TSK_Undeclared:
4864 case TSK_ImplicitInstantiation:
4865 assert(false && "Don't check implicit instantiations here");
4866 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004867
Douglas Gregor454885e2009-10-15 15:54:05 +00004868 case TSK_ExplicitSpecialization:
4869 switch (PrevTSK) {
4870 case TSK_Undeclared:
4871 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004872 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004873 // explicitly specialized or has merely been mentioned without any
4874 // instantiation.
4875 return false;
4876
4877 case TSK_ImplicitInstantiation:
4878 if (PrevPointOfInstantiation.isInvalid()) {
4879 // The declaration itself has not actually been instantiated, so it is
4880 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004881 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004882 return false;
4883 }
4884 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004885
Douglas Gregor454885e2009-10-15 15:54:05 +00004886 case TSK_ExplicitInstantiationDeclaration:
4887 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004888 assert((PrevTSK == TSK_ImplicitInstantiation ||
4889 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004890 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004891
Douglas Gregor454885e2009-10-15 15:54:05 +00004892 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004893 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004894 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004895 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004896 // implicit instantiation to take place, in every translation unit in
4897 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004898 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4899 // Is there any previous explicit specialization declaration?
4900 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4901 return false;
4902 }
4903
Douglas Gregor0d035142009-10-27 18:42:08 +00004904 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004905 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004906 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004907 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004908
Douglas Gregor454885e2009-10-15 15:54:05 +00004909 return true;
4910 }
4911 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004912
Douglas Gregor454885e2009-10-15 15:54:05 +00004913 case TSK_ExplicitInstantiationDeclaration:
4914 switch (PrevTSK) {
4915 case TSK_ExplicitInstantiationDeclaration:
4916 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004917 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004918 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004919
Douglas Gregor454885e2009-10-15 15:54:05 +00004920 case TSK_Undeclared:
4921 case TSK_ImplicitInstantiation:
4922 // We're explicitly instantiating something that may have already been
4923 // implicitly instantiated; that's fine.
4924 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004925
Douglas Gregor454885e2009-10-15 15:54:05 +00004926 case TSK_ExplicitSpecialization:
4927 // C++0x [temp.explicit]p4:
4928 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004929 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004930 // specialization for that template, the explicit instantiation has no
4931 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004932 HasNoEffect = true;
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.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004937 // If an entity is the subject of both an explicit instantiation
4938 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004939 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004940 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004941 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004942 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004943 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004944 assert(PrevPointOfInstantiation.isValid() &&
4945 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004946 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004947 return false;
4948 }
4949 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004950
Douglas Gregor454885e2009-10-15 15:54:05 +00004951 case TSK_ExplicitInstantiationDefinition:
4952 switch (PrevTSK) {
4953 case TSK_Undeclared:
4954 case TSK_ImplicitInstantiation:
4955 // We're explicitly instantiating something that may have already been
4956 // implicitly instantiated; that's fine.
4957 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004958
Douglas Gregor454885e2009-10-15 15:54:05 +00004959 case TSK_ExplicitSpecialization:
4960 // C++ DR 259, C++0x [temp.explicit]p4:
4961 // For a given set of template parameters, if an explicit
4962 // instantiation of a template appears after a declaration of
4963 // an explicit specialization for that template, the explicit
4964 // instantiation has no effect.
4965 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004966 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004967 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004968 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004969 if (!getLangOptions().CPlusPlus0x) {
4970 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004971 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004972 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004973 diag::note_previous_template_specialization);
4974 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004975 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004976 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004977
Douglas Gregor454885e2009-10-15 15:54:05 +00004978 case TSK_ExplicitInstantiationDeclaration:
4979 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004980 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004981 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004982
Douglas Gregor454885e2009-10-15 15:54:05 +00004983 case TSK_ExplicitInstantiationDefinition:
4984 // C++0x [temp.spec]p5:
4985 // For a given template and a given set of template-arguments,
4986 // - an explicit instantiation definition shall appear at most once
4987 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004988 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004989 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004990 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004991 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004992 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004993 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004994 }
4995 break;
4996 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004997
Douglas Gregor454885e2009-10-15 15:54:05 +00004998 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004999
Douglas Gregor454885e2009-10-15 15:54:05 +00005000 return false;
5001}
5002
John McCallaf2094e2010-04-08 09:05:18 +00005003/// \brief Perform semantic analysis for the given dependent function
5004/// template specialization. The only possible way to get a dependent
5005/// function template specialization is with a friend declaration,
5006/// like so:
5007///
5008/// template <class T> void foo(T);
5009/// template <class T> class A {
5010/// friend void foo<>(T);
5011/// };
5012///
5013/// There really isn't any useful analysis we can do here, so we
5014/// just store the information.
5015bool
5016Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5017 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5018 LookupResult &Previous) {
5019 // Remove anything from Previous that isn't a function template in
5020 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005021 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005022 LookupResult::Filter F = Previous.makeFilter();
5023 while (F.hasNext()) {
5024 NamedDecl *D = F.next()->getUnderlyingDecl();
5025 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005026 !FDLookupContext->InEnclosingNamespaceSetOf(
5027 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005028 F.erase();
5029 }
5030 F.done();
5031
5032 // Should this be diagnosed here?
5033 if (Previous.empty()) return true;
5034
5035 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5036 ExplicitTemplateArgs);
5037 return false;
5038}
5039
Abramo Bagnarae03db982010-05-20 15:32:11 +00005040/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005041/// specialization.
5042///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005043/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005044/// explicit function template specialization. On successful completion,
5045/// the function declaration \p FD will become a function template
5046/// specialization.
5047///
5048/// \param FD the function declaration, which will be updated to become a
5049/// function template specialization.
5050///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005051/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5052/// if any. Note that this may be valid info even when 0 arguments are
5053/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5054/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005055///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005056/// \param PrevDecl the set of declarations that may be specialized by
5057/// this function specialization.
5058bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005059Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005060 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005061 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005062 // The set of function template specializations that could match this
5063 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005064 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005065
Sebastian Redl7a126a42010-08-31 00:36:30 +00005066 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005067 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5068 I != E; ++I) {
5069 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5070 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005071 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005072 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005073 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5074 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005075 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005076
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005077 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005078 // A trailing template-argument can be left unspecified in the
5079 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005080 // provided it can be deduced from the function argument type.
5081 // Perform template argument deduction to determine whether we may be
5082 // specializing this template.
5083 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005084 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005085 FunctionDecl *Specialization = 0;
5086 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005087 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005088 FD->getType(),
5089 Specialization,
5090 Info)) {
5091 // FIXME: Template argument deduction failed; record why it failed, so
5092 // that we can provide nifty diagnostics.
5093 (void)TDK;
5094 continue;
5095 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005096
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005097 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005098 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005099 }
5100 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005101
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005102 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005103 UnresolvedSetIterator Result
5104 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005105 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005106 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005107 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005108 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005109 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005110 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005111 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005112 return true;
John McCallc373d482010-01-27 01:50:18 +00005113
5114 // Ignore access information; it doesn't figure into redeclaration checking.
5115 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005116
5117 FunctionTemplateSpecializationInfo *SpecInfo
5118 = Specialization->getTemplateSpecializationInfo();
5119 assert(SpecInfo && "Function template specialization info missing?");
5120 {
5121 // Note: do not overwrite location info if previous template
5122 // specialization kind was explicit.
5123 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5124 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5125 Specialization->setLocation(FD->getLocation());
5126 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005127
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005128 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005129 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005130
5131 // If this is a friend declaration, then we're not really declaring
5132 // an explicit specialization.
5133 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005134
Douglas Gregord5cb8762009-10-07 00:13:32 +00005135 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005136 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005137 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005138 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005139 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005140 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005141 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005142
5143 // C++ [temp.expl.spec]p6:
5144 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005145 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005146 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005147 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005148 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005149 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005150 if (!isFriend &&
5151 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005152 TSK_ExplicitSpecialization,
5153 Specialization,
5154 SpecInfo->getTemplateSpecializationKind(),
5155 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005156 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005157 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005158
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005159 // Mark the prior declaration as an explicit specialization, so that later
5160 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005161 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005162 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005163 MarkUnusedFileScopedDecl(Specialization);
5164 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005165
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005166 // Turn the given function declaration into a function template
5167 // specialization, with the template arguments from the previous
5168 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005169 // Take copies of (semantic and syntactic) template argument lists.
5170 const TemplateArgumentList* TemplArgs = new (Context)
5171 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5172 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5173 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005174 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005175 TemplArgs, /*InsertPos=*/0,
5176 SpecInfo->getTemplateSpecializationKind(),
5177 TemplArgsAsWritten);
5178
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005179 // The "previous declaration" for this function template specialization is
5180 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005181 Previous.clear();
5182 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005183 return false;
5184}
5185
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005186/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005187/// specialization.
5188///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005189/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005190/// explicit member function specialization. On successful completion,
5191/// the function declaration \p FD will become a member function
5192/// specialization.
5193///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005194/// \param Member the member declaration, which will be updated to become a
5195/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005196///
John McCall68263142009-11-18 22:49:29 +00005197/// \param Previous the set of declarations, one of which may be specialized
5198/// by this function specialization; the set will be modified to contain the
5199/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005200bool
John McCall68263142009-11-18 22:49:29 +00005201Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005202 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005203
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005204 // Try to find the member we are instantiating.
5205 NamedDecl *Instantiation = 0;
5206 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005207 MemberSpecializationInfo *MSInfo = 0;
5208
John McCall68263142009-11-18 22:49:29 +00005209 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005210 // Nowhere to look anyway.
5211 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005212 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5213 I != E; ++I) {
5214 NamedDecl *D = (*I)->getUnderlyingDecl();
5215 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005216 if (Context.hasSameType(Function->getType(), Method->getType())) {
5217 Instantiation = Method;
5218 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005219 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005220 break;
5221 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005222 }
5223 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005224 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005225 VarDecl *PrevVar;
5226 if (Previous.isSingleResult() &&
5227 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005228 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005229 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005230 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005231 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005232 }
5233 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005234 CXXRecordDecl *PrevRecord;
5235 if (Previous.isSingleResult() &&
5236 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5237 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005238 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005239 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005240 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005241 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005242
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005243 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005244 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005245 // specializations are always out-of-line, the caller will complain about
5246 // this mismatch later.
5247 return false;
5248 }
John McCall77e8b112010-04-13 20:37:33 +00005249
5250 // If this is a friend, just bail out here before we start turning
5251 // things into explicit specializations.
5252 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5253 // Preserve instantiation information.
5254 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5255 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5256 cast<CXXMethodDecl>(InstantiatedFrom),
5257 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5258 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5259 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5260 cast<CXXRecordDecl>(InstantiatedFrom),
5261 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5262 }
5263
5264 Previous.clear();
5265 Previous.addDecl(Instantiation);
5266 return false;
5267 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005268
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005269 // Make sure that this is a specialization of a member.
5270 if (!InstantiatedFrom) {
5271 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5272 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005273 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5274 return true;
5275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005276
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005277 // C++ [temp.expl.spec]p6:
5278 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005279 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005280 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005281 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005282 // use occurs; no diagnostic is required.
5283 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005284
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005285 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005286 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5287 TSK_ExplicitSpecialization,
5288 Instantiation,
5289 MSInfo->getTemplateSpecializationKind(),
5290 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005291 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005292 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005293
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005294 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005295 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005296 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005297 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005298 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005299 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005300
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005301 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005302 // the original declaration to note that it is an explicit specialization
5303 // (if it was previously an implicit instantiation). This latter step
5304 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005305 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005306 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5307 if (InstantiationFunction->getTemplateSpecializationKind() ==
5308 TSK_ImplicitInstantiation) {
5309 InstantiationFunction->setTemplateSpecializationKind(
5310 TSK_ExplicitSpecialization);
5311 InstantiationFunction->setLocation(Member->getLocation());
5312 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005313
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005314 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5315 cast<CXXMethodDecl>(InstantiatedFrom),
5316 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005317 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005318 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005319 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5320 if (InstantiationVar->getTemplateSpecializationKind() ==
5321 TSK_ImplicitInstantiation) {
5322 InstantiationVar->setTemplateSpecializationKind(
5323 TSK_ExplicitSpecialization);
5324 InstantiationVar->setLocation(Member->getLocation());
5325 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005326
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005327 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5328 cast<VarDecl>(InstantiatedFrom),
5329 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005330 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005331 } else {
5332 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005333 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5334 if (InstantiationClass->getTemplateSpecializationKind() ==
5335 TSK_ImplicitInstantiation) {
5336 InstantiationClass->setTemplateSpecializationKind(
5337 TSK_ExplicitSpecialization);
5338 InstantiationClass->setLocation(Member->getLocation());
5339 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005340
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005341 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005342 cast<CXXRecordDecl>(InstantiatedFrom),
5343 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005344 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005345
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005346 // Save the caller the trouble of having to figure out which declaration
5347 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005348 Previous.clear();
5349 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005350 return false;
5351}
5352
Douglas Gregor558c0322009-10-14 23:41:34 +00005353/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005354///
5355/// \returns true if a serious error occurs, false otherwise.
5356static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005357 SourceLocation InstLoc,
5358 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005359 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5360 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005361
Douglas Gregor669eed82010-07-13 00:10:04 +00005362 if (CurContext->isRecord()) {
5363 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5364 << D;
5365 return true;
5366 }
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 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005370 // template.
5371 //
5372 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005373 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005374 !CurContext->Encloses(OrigContext)) {
5375 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005376 S.Diag(InstLoc,
5377 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005378 diag::err_explicit_instantiation_out_of_scope
5379 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005380 << D << NS;
5381 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005382 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005383 S.getLangOptions().CPlusPlus0x?
5384 diag::err_explicit_instantiation_must_be_global
5385 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005386 << D;
5387 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005388 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005389 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005390
Douglas Gregor558c0322009-10-14 23:41:34 +00005391 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005392 // If the name declared in the explicit instantiation is an unqualified
5393 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005394 // its template is declared or, if that namespace is inline (7.3.1), any
5395 // namespace from its enclosing namespace set.
5396 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005397 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005398
5399 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005400 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005401
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005402 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005403 S.getLangOptions().CPlusPlus0x?
5404 diag::err_explicit_instantiation_unqualified_wrong_namespace
5405 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005406 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005407 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005408 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005409}
5410
5411/// \brief Determine whether the given scope specifier has a template-id in it.
5412static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5413 if (!SS.isSet())
5414 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005415
Douglas Gregor558c0322009-10-14 23:41:34 +00005416 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005417 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005418 // or a static data member of a class template specialization, the name of
5419 // the class template specialization in the qualified-id for the member
5420 // name shall be a simple-template-id.
5421 //
5422 // C++98 has the same restriction, just worded differently.
5423 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5424 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005425 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005426 if (isa<TemplateSpecializationType>(T))
5427 return true;
5428
5429 return false;
5430}
5431
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005432// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005433DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005434Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005435 SourceLocation ExternLoc,
5436 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005437 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005438 SourceLocation KWLoc,
5439 const CXXScopeSpec &SS,
5440 TemplateTy TemplateD,
5441 SourceLocation TemplateNameLoc,
5442 SourceLocation LAngleLoc,
5443 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005444 SourceLocation RAngleLoc,
5445 AttributeList *Attr) {
5446 // Find the class template we're specializing
5447 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005448 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005449 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5450
5451 // Check that the specialization uses the same tag kind as the
5452 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005453 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5454 assert(Kind != TTK_Enum &&
5455 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005456 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005457 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005458 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005459 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005460 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005461 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005462 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005463 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005464 diag::note_previous_use);
5465 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5466 }
5467
Douglas Gregor558c0322009-10-14 23:41:34 +00005468 // C++0x [temp.explicit]p2:
5469 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005470 // definition and an explicit instantiation declaration. An explicit
5471 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005472 TemplateSpecializationKind TSK
5473 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5474 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005475
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005476 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005477 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005478 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005479
5480 // Check that the template argument list is well-formed for this
5481 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005482 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005483 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5484 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005485 return true;
5486
Douglas Gregor910f8002010-11-07 23:05:16 +00005487 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005488 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005489
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005490 // Find the class template specialization declaration that
5491 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005492 void *InsertPos = 0;
5493 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005494 = ClassTemplate->findSpecialization(Converted.data(),
5495 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005496
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005497 TemplateSpecializationKind PrevDecl_TSK
5498 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5499
Douglas Gregord5cb8762009-10-07 00:13:32 +00005500 // C++0x [temp.explicit]p2:
5501 // [...] An explicit instantiation shall appear in an enclosing
5502 // namespace of its template. [...]
5503 //
5504 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005505 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5506 SS.isSet()))
5507 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005508
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005509 ClassTemplateSpecializationDecl *Specialization = 0;
5510
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005511 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005512 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005513 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005514 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005515 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005516 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005517 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005518
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005519 // Even though HasNoEffect == true means that this explicit instantiation
5520 // has no effect on semantics, we go on to put its syntax in the AST.
5521
5522 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5523 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005524 // Since the only prior class template specialization with these
5525 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005526 // declaration node as our own, updating the source location
5527 // for the template name to reflect our new declaration.
5528 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005529 Specialization = PrevDecl;
5530 Specialization->setLocation(TemplateNameLoc);
5531 PrevDecl = 0;
5532 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005533 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005534
Douglas Gregor52604ab2009-09-11 21:19:12 +00005535 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005536 // Create a new class template specialization declaration node for
5537 // this explicit specialization.
5538 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005539 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005540 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005541 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005542 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005543 Converted.data(),
5544 Converted.size(),
5545 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005546 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005547
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005548 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005549 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005550 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005551 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005552 }
5553
5554 // Build the fully-sugared type for this explicit instantiation as
5555 // the user wrote in the explicit instantiation itself. This means
5556 // that we'll pretty-print the type retrieved from the
5557 // specialization's declaration the way that the user actually wrote
5558 // the explicit instantiation, rather than formatting the name based
5559 // on the "canonical" representation used to store the template
5560 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005561 TypeSourceInfo *WrittenTy
5562 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5563 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005564 Context.getTypeDeclType(Specialization));
5565 Specialization->setTypeAsWritten(WrittenTy);
5566 TemplateArgsIn.release();
5567
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005568 // Set source locations for keywords.
5569 Specialization->setExternLoc(ExternLoc);
5570 Specialization->setTemplateKeywordLoc(TemplateLoc);
5571
5572 // Add the explicit instantiation into its lexical context. However,
5573 // since explicit instantiations are never found by name lookup, we
5574 // just put it into the declaration context directly.
5575 Specialization->setLexicalDeclContext(CurContext);
5576 CurContext->addDecl(Specialization);
5577
5578 // Syntax is now OK, so return if it has no other effect on semantics.
5579 if (HasNoEffect) {
5580 // Set the template specialization kind.
5581 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005582 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005583 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005584
5585 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005586 // A definition of a class template or class member template
5587 // shall be in scope at the point of the explicit instantiation of
5588 // the class template or class member template.
5589 //
5590 // This check comes when we actually try to perform the
5591 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005592 ClassTemplateSpecializationDecl *Def
5593 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005594 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005595 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005596 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005597 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005598 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005599 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5600 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005601
Douglas Gregor0d035142009-10-27 18:42:08 +00005602 // Instantiate the members of this class template specialization.
5603 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005604 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005605 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005606 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5607
5608 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5609 // TSK_ExplicitInstantiationDefinition
5610 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5611 TSK == TSK_ExplicitInstantiationDefinition)
5612 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005613
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005614 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005615 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005616
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005617 // Set the template specialization kind.
5618 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005619 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005620}
5621
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005622// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005623DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005624Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005625 SourceLocation ExternLoc,
5626 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005627 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005628 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005629 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005630 IdentifierInfo *Name,
5631 SourceLocation NameLoc,
5632 AttributeList *Attr) {
5633
Douglas Gregor402abb52009-05-28 23:31:59 +00005634 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005635 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005636 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005637 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5638 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005639 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005640 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005641 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5642
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005643 if (!TagD)
5644 return true;
5645
John McCalld226f652010-08-21 09:40:31 +00005646 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005647 if (Tag->isEnum()) {
5648 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5649 << Context.getTypeDeclType(Tag);
5650 return true;
5651 }
5652
Douglas Gregord0c87372009-05-27 17:30:49 +00005653 if (Tag->isInvalidDecl())
5654 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005655
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005656 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5657 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5658 if (!Pattern) {
5659 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5660 << Context.getTypeDeclType(Record);
5661 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5662 return true;
5663 }
5664
Douglas Gregor558c0322009-10-14 23:41:34 +00005665 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005666 // If the explicit instantiation is for a class or member class, the
5667 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005668 // simple-template-id.
5669 //
5670 // C++98 has the same restriction, just worded differently.
5671 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005672 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005673 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005674
Douglas Gregor558c0322009-10-14 23:41:34 +00005675 // C++0x [temp.explicit]p2:
5676 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005677 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005678 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005679 TemplateSpecializationKind TSK
5680 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5681 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005682
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005683 // C++0x [temp.explicit]p2:
5684 // [...] An explicit instantiation shall appear in an enclosing
5685 // namespace of its template. [...]
5686 //
5687 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005688 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005689
Douglas Gregor454885e2009-10-15 15:54:05 +00005690 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005691 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005692 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005693 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005694 PrevDecl = Record;
5695 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005696 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005697 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005698 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005699 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005700 PrevDecl,
5701 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005702 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005703 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005704 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005705 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005706 return TagD;
5707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005708
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005709 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005710 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005711 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005712 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005713 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005714 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005715 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005716 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005717 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005718 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5719 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005720 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5721 << Pattern;
5722 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005723 } else {
5724 if (InstantiateClass(NameLoc, Record, Def,
5725 getTemplateInstantiationArgs(Record),
5726 TSK))
5727 return true;
5728
Douglas Gregor952b0172010-02-11 01:04:33 +00005729 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005730 if (!RecordDef)
5731 return true;
5732 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005733 }
5734
Douglas Gregor0d035142009-10-27 18:42:08 +00005735 // Instantiate all of the members of the class.
5736 InstantiateClassMembers(NameLoc, RecordDef,
5737 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005738
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005739 if (TSK == TSK_ExplicitInstantiationDefinition)
5740 MarkVTableUsed(NameLoc, RecordDef, true);
5741
Mike Stump390b4cc2009-05-16 07:39:55 +00005742 // FIXME: We don't have any representation for explicit instantiations of
5743 // member classes. Such a representation is not needed for compilation, but it
5744 // should be available for clients that want to see all of the declarations in
5745 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005746 return TagD;
5747}
5748
John McCallf312b1e2010-08-26 23:41:50 +00005749DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5750 SourceLocation ExternLoc,
5751 SourceLocation TemplateLoc,
5752 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005753 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005754 // TODO: check if/when DNInfo should replace Name.
5755 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5756 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005757 if (!Name) {
5758 if (!D.isInvalidType())
5759 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5760 diag::err_explicit_instantiation_requires_name)
5761 << D.getDeclSpec().getSourceRange()
5762 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005763
Douglas Gregord5a423b2009-09-25 18:43:00 +00005764 return true;
5765 }
5766
5767 // The scope passed in may not be a decl scope. Zip up the scope tree until
5768 // we find one that is.
5769 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5770 (S->getFlags() & Scope::TemplateParamScope) != 0)
5771 S = S->getParent();
5772
5773 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005774 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5775 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005776 if (R.isNull())
5777 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005778
Douglas Gregord5a423b2009-09-25 18:43:00 +00005779 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5780 // Cannot explicitly instantiate a typedef.
5781 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5782 << Name;
5783 return true;
5784 }
5785
Douglas Gregor663b5a02009-10-14 20:14:33 +00005786 // C++0x [temp.explicit]p1:
5787 // [...] An explicit instantiation of a function template shall not use the
5788 // inline or constexpr specifiers.
5789 // Presumably, this also applies to member functions of class templates as
5790 // well.
5791 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005792 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005793 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005794 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005795
Douglas Gregor663b5a02009-10-14 20:14:33 +00005796 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005797
Douglas Gregor558c0322009-10-14 23:41:34 +00005798 // C++0x [temp.explicit]p2:
5799 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005800 // definition and an explicit instantiation declaration. An explicit
5801 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005802 TemplateSpecializationKind TSK
5803 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5804 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005805
Abramo Bagnara25777432010-08-11 22:01:17 +00005806 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005807 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005808
5809 if (!R->isFunctionType()) {
5810 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005811 // A [...] static data member of a class template can be explicitly
5812 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005813 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005814 if (Previous.isAmbiguous())
5815 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005816
John McCall1bcee0a2009-12-02 08:25:40 +00005817 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005818 if (!Prev || !Prev->isStaticDataMember()) {
5819 // We expect to see a data data member here.
5820 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5821 << Name;
5822 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5823 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005824 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005825 return true;
5826 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005827
Douglas Gregord5a423b2009-09-25 18:43:00 +00005828 if (!Prev->getInstantiatedFromStaticDataMember()) {
5829 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005830 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005831 diag::err_explicit_instantiation_data_member_not_instantiated)
5832 << Prev;
5833 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5834 // FIXME: Can we provide a note showing where this was declared?
5835 return true;
5836 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005837
Douglas Gregor558c0322009-10-14 23:41:34 +00005838 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005839 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005840 // or a static data member of a class template specialization, the name of
5841 // the class template specialization in the qualified-id for the member
5842 // name shall be a simple-template-id.
5843 //
5844 // C++98 has the same restriction, just worded differently.
5845 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005846 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005847 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005848 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005849
Douglas Gregor558c0322009-10-14 23:41:34 +00005850 // Check the scope of this explicit instantiation.
5851 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005852
Douglas Gregor454885e2009-10-15 15:54:05 +00005853 // Verify that it is okay to explicitly instantiate here.
5854 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5855 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005856 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005857 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005858 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005859 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005860 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005861 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005862 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005863 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005864
Douglas Gregord5a423b2009-09-25 18:43:00 +00005865 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005866 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005867 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005868 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005869
Douglas Gregord5a423b2009-09-25 18:43:00 +00005870 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005871 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005872 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005873
5874 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005875 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005876 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005877 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005878 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5879 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005880 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5881 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005882 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5883 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005884 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005885 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005886 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005887 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005888 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005889
Douglas Gregord5a423b2009-09-25 18:43:00 +00005890 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005891 // A [...] function [...] can be explicitly instantiated from its template.
5892 // A member function [...] of a class template can be explicitly
5893 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005894 // template.
John McCallc373d482010-01-27 01:50:18 +00005895 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005896 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5897 P != PEnd; ++P) {
5898 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005899 if (!HasExplicitTemplateArgs) {
5900 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5901 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5902 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005903
John McCallc373d482010-01-27 01:50:18 +00005904 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005905 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5906 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005907 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005908 }
5909 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005910
Douglas Gregord5a423b2009-09-25 18:43:00 +00005911 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5912 if (!FunTmpl)
5913 continue;
5914
John McCall5769d612010-02-08 23:07:23 +00005915 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005916 FunctionDecl *Specialization = 0;
5917 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005918 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005919 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005920 R, Specialization, Info)) {
5921 // FIXME: Keep track of almost-matches?
5922 (void)TDK;
5923 continue;
5924 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005925
John McCallc373d482010-01-27 01:50:18 +00005926 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005927 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005928
Douglas Gregord5a423b2009-09-25 18:43:00 +00005929 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005930 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005931 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005932 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005933 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5934 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5935 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005936
John McCallc373d482010-01-27 01:50:18 +00005937 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005938 return true;
John McCallc373d482010-01-27 01:50:18 +00005939
5940 // Ignore access control bits, we don't need them for redeclaration checking.
5941 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005942
Douglas Gregor0a897e32009-10-15 17:21:20 +00005943 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005944 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005945 diag::err_explicit_instantiation_member_function_not_instantiated)
5946 << Specialization
5947 << (Specialization->getTemplateSpecializationKind() ==
5948 TSK_ExplicitSpecialization);
5949 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5950 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005951 }
5952
Douglas Gregor0a897e32009-10-15 17:21:20 +00005953 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005954 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5955 PrevDecl = Specialization;
5956
Douglas Gregor0a897e32009-10-15 17:21:20 +00005957 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005958 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005959 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005960 PrevDecl,
5961 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005962 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005963 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005964 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005965
Douglas Gregor0a897e32009-10-15 17:21:20 +00005966 // FIXME: We may still want to build some representation of this
5967 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005968 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005969 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005970 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005971
5972 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005973
Douglas Gregor0a897e32009-10-15 17:21:20 +00005974 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005975 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005976
Douglas Gregor558c0322009-10-14 23:41:34 +00005977 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005978 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005979 // or a static data member of a class template specialization, the name of
5980 // the class template specialization in the qualified-id for the member
5981 // name shall be a simple-template-id.
5982 //
5983 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005984 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005985 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005986 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005987 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005988 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005989 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005990 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005991
Douglas Gregor558c0322009-10-14 23:41:34 +00005992 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005993 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005994 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005995 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005996 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005997
Douglas Gregord5a423b2009-09-25 18:43:00 +00005998 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005999 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006000}
6001
John McCallf312b1e2010-08-26 23:41:50 +00006002TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006003Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6004 const CXXScopeSpec &SS, IdentifierInfo *Name,
6005 SourceLocation TagLoc, SourceLocation NameLoc) {
6006 // This has to hold, because SS is expected to be defined.
6007 assert(Name && "Expected a name in a dependent tag");
6008
6009 NestedNameSpecifier *NNS
6010 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6011 if (!NNS)
6012 return true;
6013
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006014 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006015
Douglas Gregor48c89f42010-04-24 16:38:41 +00006016 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6017 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006018 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006019 return true;
6020 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006021
Douglas Gregor059101f2011-03-02 00:47:37 +00006022 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006023 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006024 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6025
6026 // Create type-source location information for this type.
6027 TypeLocBuilder TLB;
6028 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6029 TL.setKeywordLoc(TagLoc);
6030 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6031 TL.setNameLoc(NameLoc);
6032 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006033}
6034
John McCallf312b1e2010-08-26 23:41:50 +00006035TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006036Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6037 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006038 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006039 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006040 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006041
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006042 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6043 !getLangOptions().CPlusPlus0x)
6044 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006045 << FixItHint::CreateRemoval(TypenameLoc);
6046
Douglas Gregor2494dd02011-03-01 01:34:45 +00006047 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006048 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6049 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006050 if (T.isNull())
6051 return true;
John McCall63b43852010-04-29 23:50:39 +00006052
6053 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6054 if (isa<DependentNameType>(T)) {
6055 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006056 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006057 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006058 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006059 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006060 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006061 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006062 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006063 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006064 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006065
John McCallb3d87482010-08-24 05:47:05 +00006066 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006067}
6068
John McCallf312b1e2010-08-26 23:41:50 +00006069TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006070Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6071 const CXXScopeSpec &SS,
6072 SourceLocation TemplateLoc,
6073 TemplateTy TemplateIn,
6074 SourceLocation TemplateNameLoc,
6075 SourceLocation LAngleLoc,
6076 ASTTemplateArgsPtr TemplateArgsIn,
6077 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006078 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6079 !getLangOptions().CPlusPlus0x)
6080 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006081 << FixItHint::CreateRemoval(TypenameLoc);
6082
6083 // Translate the parser's template argument list in our AST format.
6084 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6085 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6086
6087 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006088 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6089 // Construct a dependent template specialization type.
6090 assert(DTN && "dependent template has non-dependent name?");
6091 assert(DTN->getQualifier()
6092 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6093 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6094 DTN->getQualifier(),
6095 DTN->getIdentifier(),
6096 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006097
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006098 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006099 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006100 DependentTemplateSpecializationTypeLoc SpecTL
6101 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006102 SpecTL.setLAngleLoc(LAngleLoc);
6103 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006104 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006105 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006106 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006107 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6108 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006109 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006110 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006111
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006112 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6113 if (T.isNull())
6114 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006115
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006116 // Provide source-location information for the template specialization
6117 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006118 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006119 TemplateSpecializationTypeLoc SpecTL
6120 = Builder.push<TemplateSpecializationTypeLoc>(T);
6121
6122 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006123 SpecTL.setLAngleLoc(LAngleLoc);
6124 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006125 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006126 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6127 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6128
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006129 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6130 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6131 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006132 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6133
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006134 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6135 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006136}
6137
Douglas Gregora02411e2011-02-27 22:46:49 +00006138
Douglas Gregord57959a2009-03-27 23:10:48 +00006139/// \brief Build the type that describes a C++ typename specifier,
6140/// e.g., "typename T::type".
6141QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006142Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6143 SourceLocation KeywordLoc,
6144 NestedNameSpecifierLoc QualifierLoc,
6145 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006146 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006147 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006148 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006149
John McCall77bb1aa2010-05-01 00:40:08 +00006150 DeclContext *Ctx = computeDeclContext(SS);
6151 if (!Ctx) {
6152 // If the nested-name-specifier is dependent and couldn't be
6153 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006154 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6155 return Context.getDependentNameType(Keyword,
6156 QualifierLoc.getNestedNameSpecifier(),
6157 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006158 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006159
John McCall77bb1aa2010-05-01 00:40:08 +00006160 // If the nested-name-specifier refers to the current instantiation,
6161 // the "typename" keyword itself is superfluous. In C++03, the
6162 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6163 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006164 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006165
John McCall77bb1aa2010-05-01 00:40:08 +00006166 if (RequireCompleteDeclContext(SS, Ctx))
6167 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006168
6169 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006170 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006171 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006172 unsigned DiagID = 0;
6173 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006174 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006175 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006176 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006177 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006178
6179 case LookupResult::FoundUnresolvedValue: {
6180 // We found a using declaration that is a value. Most likely, the using
6181 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006182 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006183 IILoc);
6184 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6185 << Name << Ctx << FullRange;
6186 if (UnresolvedUsingValueDecl *Using
6187 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006188 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006189 Diag(Loc, diag::note_using_value_decl_missing_typename)
6190 << FixItHint::CreateInsertion(Loc, "typename ");
6191 }
6192 }
6193 // Fall through to create a dependent typename type, from which we can recover
6194 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006195
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006196 case LookupResult::NotFoundInCurrentInstantiation:
6197 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006198 return Context.getDependentNameType(Keyword,
6199 QualifierLoc.getNestedNameSpecifier(),
6200 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006201
6202 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006203 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006204 // We found a type. Build an ElaboratedType, since the
6205 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006206 return Context.getElaboratedType(ETK_Typename,
6207 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006208 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006209 }
6210
6211 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006212 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006213 break;
6214
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006215
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006216 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006217 return QualType();
6218
Douglas Gregord57959a2009-03-27 23:10:48 +00006219 case LookupResult::FoundOverloaded:
6220 DiagID = diag::err_typename_nested_not_type;
6221 Referenced = *Result.begin();
6222 break;
6223
John McCall6e247262009-10-10 05:48:19 +00006224 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006225 return QualType();
6226 }
6227
6228 // If we get here, it's because name lookup did not find a
6229 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006230 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006231 IILoc);
6232 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006233 if (Referenced)
6234 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6235 << Name;
6236 return QualType();
6237}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006238
6239namespace {
6240 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006241 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006242 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006243 SourceLocation Loc;
6244 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006245
Douglas Gregor4a959d82009-08-06 16:20:37 +00006246 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006247 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006248
Mike Stump1eb44332009-09-09 15:08:12 +00006249 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006250 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006251 DeclarationName Entity)
6252 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006253 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006254
6255 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006256 /// transformed.
6257 ///
6258 /// For the purposes of type reconstruction, a type has already been
6259 /// transformed if it is NULL or if it is not dependent.
6260 bool AlreadyTransformed(QualType T) {
6261 return T.isNull() || !T->isDependentType();
6262 }
Mike Stump1eb44332009-09-09 15:08:12 +00006263
6264 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006265 /// rebuilt.
6266 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006267
Douglas Gregor4a959d82009-08-06 16:20:37 +00006268 /// \brief Returns the name of the entity whose type is being rebuilt.
6269 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006270
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006271 /// \brief Sets the "base" location and entity when that
6272 /// information is known based on another transformation.
6273 void setBase(SourceLocation Loc, DeclarationName Entity) {
6274 this->Loc = Loc;
6275 this->Entity = Entity;
6276 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006277 };
6278}
6279
Douglas Gregor4a959d82009-08-06 16:20:37 +00006280/// \brief Rebuilds a type within the context of the current instantiation.
6281///
Mike Stump1eb44332009-09-09 15:08:12 +00006282/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006283/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006284/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006285/// partial specialization thereof). This routine will rebuild that type now
6286/// that we have entered the declarator's scope, which may produce different
6287/// canonical types, e.g.,
6288///
6289/// \code
6290/// template<typename T>
6291/// struct X {
6292/// typedef T* pointer;
6293/// pointer data();
6294/// };
6295///
6296/// template<typename T>
6297/// typename X<T>::pointer X<T>::data() { ... }
6298/// \endcode
6299///
Douglas Gregor4714c122010-03-31 17:34:00 +00006300/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006301/// since we do not know that we can look into X<T> when we parsed the type.
6302/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006303/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006304/// as the canonical type of T*, allowing the return types of the out-of-line
6305/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006306TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6307 SourceLocation Loc,
6308 DeclarationName Name) {
6309 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006310 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006311
Douglas Gregor4a959d82009-08-06 16:20:37 +00006312 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6313 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006314}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006315
John McCall60d7b3a2010-08-24 06:29:42 +00006316ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006317 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6318 DeclarationName());
6319 return Rebuilder.TransformExpr(E);
6320}
6321
John McCall63b43852010-04-29 23:50:39 +00006322bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006323 if (SS.isInvalid())
6324 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006325
Douglas Gregor7e384942011-02-25 16:07:42 +00006326 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006327 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6328 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006329 NestedNameSpecifierLoc Rebuilt
6330 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6331 if (!Rebuilt)
6332 return true;
John McCall63b43852010-04-29 23:50:39 +00006333
Douglas Gregor7e384942011-02-25 16:07:42 +00006334 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006335 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006336}
6337
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006338/// \brief Produces a formatted string that describes the binding of
6339/// template parameters to template arguments.
6340std::string
6341Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6342 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006343 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006344}
6345
6346std::string
6347Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6348 const TemplateArgument *Args,
6349 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006350 llvm::SmallString<128> Str;
6351 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006352
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006353 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006354 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006355
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006356 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006357 if (I >= NumArgs)
6358 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006359
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006360 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006361 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006362 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006363 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006364
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006365 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006366 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006367 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006368 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006369 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006370
Douglas Gregor87dd6972010-12-20 16:52:59 +00006371 Out << " = ";
6372 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006373 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006374
6375 Out << ']';
6376 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006377}