blob: 49f9a1de1e92a0074e6f4821b83c2b914a81c7de [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
John McCallf7a1a742009-11-24 19:00:30 +000079static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
John McCallad00b772010-06-16 08:42:20 +000085 NamedDecl *Repl = isAcceptableTemplateName(C, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
95 // is followed by a template-argument-list, the reference refers to the
96 // class template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
98 //
99 // FIXME: Will we eventually have to do the same for alias templates?
100 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
101 if (!ClassTemplates.insert(ClassTmpl)) {
102 filter.erase();
103 continue;
104 }
John McCall8ba66912010-08-13 07:02:08 +0000105
106 // FIXME: we promote access to public here as a workaround to
107 // the fact that LookupResult doesn't let us remember that we
108 // found this template through a particular injected class name,
109 // which means we end up doing nasty things to the invariants.
110 // Pretending that access is public is *much* safer.
111 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000112 }
John McCallf7a1a742009-11-24 19:00:30 +0000113 }
114 filter.done();
115}
116
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000117TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000118 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000119 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000120 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000121 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000122 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000123 TemplateTy &TemplateResult,
124 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000125 assert(getLangOptions().CPlusPlus && "No template names in C!");
126
Douglas Gregor014e88d2009-11-03 23:16:33 +0000127 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000128 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000129
Douglas Gregor014e88d2009-11-03 23:16:33 +0000130 switch (Name.getKind()) {
131 case UnqualifiedId::IK_Identifier:
132 TName = DeclarationName(Name.Identifier);
133 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 case UnqualifiedId::IK_OperatorFunctionId:
136 TName = Context.DeclarationNames.getCXXOperatorName(
137 Name.OperatorFunctionId.Operator);
138 break;
139
Sean Hunte6252d12009-11-28 08:58:14 +0000140 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000141 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
142 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000143
Douglas Gregor014e88d2009-11-03 23:16:33 +0000144 default:
145 return TNK_Non_template;
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
John McCallb3d87482010-08-24 05:47:05 +0000148 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000150 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000151 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000152 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
153 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000154 if (R.empty()) return TNK_Non_template;
155 if (R.isAmbiguous()) {
156 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000157 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000158
159 // FIXME: we might have ambiguous templates, in which case we
160 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000161 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000162 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000163
John McCall0bd6feb2009-12-02 08:04:21 +0000164 TemplateName Template;
165 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
John McCall0bd6feb2009-12-02 08:04:21 +0000167 unsigned ResultCount = R.end() - R.begin();
168 if (ResultCount > 1) {
169 // We assume that we'll preserve the qualifier from a function
170 // template name in other ways.
171 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
172 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000173
174 // We'll do this lookup again later.
175 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000177 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
178
179 if (SS.isSet() && !SS.isInvalid()) {
180 NestedNameSpecifier *Qualifier
181 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000182 Template = Context.getQualifiedTemplateName(Qualifier,
183 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000184 } else {
185 Template = TemplateName(TD);
186 }
187
John McCallb8592062010-08-13 02:23:42 +0000188 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000189 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000190
191 // We'll do this lookup again later.
192 R.suppressDiagnostics();
193 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000194 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
195 TemplateKind = TNK_Type_template;
196 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
John McCall0bd6feb2009-12-02 08:04:21 +0000199 TemplateResult = TemplateTy::make(Template);
200 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000201}
202
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000203bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000204 SourceLocation IILoc,
205 Scope *S,
206 const CXXScopeSpec *SS,
207 TemplateTy &SuggestedTemplate,
208 TemplateNameKind &SuggestedKind) {
209 // We can't recover unless there's a dependent scope specifier preceding the
210 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000211 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000212 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
213 computeDeclContext(*SS))
214 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000215
Douglas Gregor84d0a192010-01-12 21:28:44 +0000216 // The code is missing a 'template' keyword prior to the dependent template
217 // name.
218 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
219 Diag(IILoc, diag::err_template_kw_missing)
220 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000221 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
224 SuggestedKind = TNK_Dependent_template_name;
225 return true;
226}
227
John McCallf7a1a742009-11-24 19:00:30 +0000228void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000229 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000230 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000231 bool EnteringContext,
232 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000233 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000234 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000235 DeclContext *LookupCtx = 0;
236 bool isDependent = false;
237 if (!ObjectType.isNull()) {
238 // This nested-name-specifier occurs in a member access expression, e.g.,
239 // x->B::f, and we are looking into the type of the object.
240 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
241 LookupCtx = computeDeclContext(ObjectType);
242 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000243 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000244 "Caller should have completed object type");
245 } else if (SS.isSet()) {
246 // This nested-name-specifier occurs after another nested-name-specifier,
247 // so long into the context associated with the prior nested-name-specifier.
248 LookupCtx = computeDeclContext(SS, EnteringContext);
249 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250
John McCallf7a1a742009-11-24 19:00:30 +0000251 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000252 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000253 return;
254 }
255
256 bool ObjectTypeSearchedInScope = false;
257 if (LookupCtx) {
258 // Perform "qualified" name lookup into the declaration context we
259 // computed, which is either the type of the base of a member access
260 // expression or the declaration context associated with a prior
261 // nested-name-specifier.
262 LookupQualifiedName(Found, LookupCtx);
263
264 if (!ObjectType.isNull() && Found.empty()) {
265 // C++ [basic.lookup.classref]p1:
266 // In a class member access expression (5.2.5), if the . or -> token is
267 // immediately followed by an identifier followed by a <, the
268 // identifier must be looked up to determine whether the < is the
269 // beginning of a template argument list (14.2) or a less-than operator.
270 // The identifier is first looked up in the class of the object
271 // expression. If the identifier is not found, it is then looked up in
272 // the context of the entire postfix-expression and shall name a class
273 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000274 if (S) LookupName(Found, S);
275 ObjectTypeSearchedInScope = true;
276 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000277 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000278 // We cannot look into a dependent object type or nested nme
279 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000280 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000281 return;
282 } else {
283 // Perform unqualified name lookup in the current scope.
284 LookupName(Found, S);
285 }
286
Douglas Gregor2e933882010-01-12 17:06:20 +0000287 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000288 // If we did not find any names, attempt to correct any typos.
289 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000290 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000291 false, CTC_CXXCasts)) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000292 FilterAcceptableTemplateNames(Context, Found);
John McCallad00b772010-06-16 08:42:20 +0000293 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000294 if (LookupCtx)
295 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
296 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000297 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000298 Found.getLookupName().getAsString());
299 else
300 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
301 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000302 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000303 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000304 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
305 Diag(Template->getLocation(), diag::note_previous_decl)
306 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000307 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000308 } else {
309 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000310 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000311 }
312 }
313
John McCallf7a1a742009-11-24 19:00:30 +0000314 FilterAcceptableTemplateNames(Context, Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000315 if (Found.empty()) {
316 if (isDependent)
317 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000318 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000319 }
John McCallf7a1a742009-11-24 19:00:30 +0000320
321 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
322 // C++ [basic.lookup.classref]p1:
323 // [...] If the lookup in the class of the object expression finds a
324 // template, the name is also looked up in the context of the entire
325 // postfix-expression and [...]
326 //
327 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
328 LookupOrdinaryName);
329 LookupName(FoundOuter, S);
330 FilterAcceptableTemplateNames(Context, FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000331
John McCallf7a1a742009-11-24 19:00:30 +0000332 if (FoundOuter.empty()) {
333 // - if the name is not found, the name found in the class of the
334 // object expression is used, otherwise
335 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
336 // - if the name is found in the context of the entire
337 // postfix-expression and does not name a class template, the name
338 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000339 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000340 // - if the name found is a class template, it must refer to the same
341 // entity as the one found in the class of the object expression,
342 // otherwise the program is ill-formed.
343 if (!Found.isSingleResult() ||
344 Found.getFoundDecl()->getCanonicalDecl()
345 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000346 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000347 diag::ext_nested_name_member_ref_lookup_ambiguous)
348 << Found.getLookupName()
349 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000350 Diag(Found.getRepresentativeDecl()->getLocation(),
351 diag::note_ambig_member_ref_object_type)
352 << ObjectType;
353 Diag(FoundOuter.getFoundDecl()->getLocation(),
354 diag::note_ambig_member_ref_scope);
355
356 // Recover by taking the template that we found in the object
357 // expression's type.
358 }
359 }
360 }
361}
362
John McCall2f841ba2009-12-02 03:53:29 +0000363/// ActOnDependentIdExpression - Handle a dependent id-expression that
364/// was just parsed. This is only possible with an explicit scope
365/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000366ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000367Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000369 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000371 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000372
John McCall2f841ba2009-12-02 03:53:29 +0000373 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000374 isa<CXXMethodDecl>(DC) &&
375 cast<CXXMethodDecl>(DC)->isInstance()) {
376 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000377
John McCallf7a1a742009-11-24 19:00:30 +0000378 // Since the 'this' expression is synthesized, we don't need to
379 // perform the double-lookup check.
380 NamedDecl *FirstQualifierInScope = 0;
381
John McCallaa81e162009-12-01 22:10:20 +0000382 return Owned(CXXDependentScopeMemberExpr::Create(Context,
383 /*This*/ 0, ThisType,
384 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000385 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000386 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000387 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000388 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000389 TemplateArgs));
390 }
391
Abramo Bagnara25777432010-08-11 22:01:17 +0000392 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000393}
394
John McCall60d7b3a2010-08-24 06:29:42 +0000395ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000396Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000397 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000398 const TemplateArgumentListInfo *TemplateArgs) {
399 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000400 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000401 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000402 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000403}
404
Douglas Gregor72c3f312008-12-05 18:15:24 +0000405/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
406/// that the template parameter 'PrevDecl' is being shadowed by a new
407/// declaration at location Loc. Returns true to indicate that this is
408/// an error, and false otherwise.
409bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000410 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000411
412 // Microsoft Visual C++ permits template parameters to be shadowed.
413 if (getLangOptions().Microsoft)
414 return false;
415
416 // C++ [temp.local]p4:
417 // A template-parameter shall not be redeclared within its
418 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000419 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000420 << cast<NamedDecl>(PrevDecl)->getDeclName();
421 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
422 return true;
423}
424
Douglas Gregor2943aed2009-03-03 04:44:36 +0000425/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000426/// the parameter D to reference the templated declaration and return a pointer
427/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000428TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
429 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
430 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000431 return Temp;
432 }
433 return 0;
434}
435
Douglas Gregorba68eca2011-01-05 17:40:24 +0000436ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
437 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000438 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000439 "Only template template arguments can be pack expansions here");
440 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
441 "Template template argument pack expansion without packs");
442 ParsedTemplateArgument Result(*this);
443 Result.EllipsisLoc = EllipsisLoc;
444 return Result;
445}
446
Douglas Gregor788cd062009-11-11 01:00:40 +0000447static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
448 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000449
Douglas Gregor788cd062009-11-11 01:00:40 +0000450 switch (Arg.getKind()) {
451 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000452 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000453 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000454 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000455 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000456 return TemplateArgumentLoc(TemplateArgument(T), DI);
457 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000458
Douglas Gregor788cd062009-11-11 01:00:40 +0000459 case ParsedTemplateArgument::NonType: {
460 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
461 return TemplateArgumentLoc(TemplateArgument(E), E);
462 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000465 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000466 TemplateArgument TArg;
467 if (Arg.getEllipsisLoc().isValid())
468 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
469 else
470 TArg = Template;
471 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000472 Arg.getScopeSpec().getWithLocInContext(
473 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000474 Arg.getLocation(),
475 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000476 }
477 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000478
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000479 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000480 return TemplateArgumentLoc();
481}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000482
Douglas Gregor788cd062009-11-11 01:00:40 +0000483/// \brief Translates template arguments as provided by the parser
484/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000485void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
486 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000487 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000488 TemplateArgs.addArgument(translateTemplateArgument(*this,
489 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000490}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000491
Douglas Gregor72c3f312008-12-05 18:15:24 +0000492/// ActOnTypeParameter - Called when a C++ template type parameter
493/// (e.g., "typename T") has been parsed. Typename specifies whether
494/// the keyword "typename" was used to declare the type parameter
495/// (otherwise, "class" was used), and KeyLoc is the location of the
496/// "class" or "typename" keyword. ParamName is the name of the
497/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000498/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000499/// If the type parameter has a default argument, it will be added
500/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000501Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
502 SourceLocation EllipsisLoc,
503 SourceLocation KeyLoc,
504 IdentifierInfo *ParamName,
505 SourceLocation ParamNameLoc,
506 unsigned Depth, unsigned Position,
507 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000508 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000509 assert(S->isTemplateParamScope() &&
510 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000511 bool Invalid = false;
512
513 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000514 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000515 LookupOrdinaryName,
516 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000517 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000518 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000519 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000520 }
521
Douglas Gregorddc29e12009-02-06 22:42:48 +0000522 SourceLocation Loc = ParamNameLoc;
523 if (!ParamName)
524 Loc = KeyLoc;
525
Douglas Gregor72c3f312008-12-05 18:15:24 +0000526 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000527 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
528 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000529 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000530 if (Invalid)
531 Param->setInvalidDecl();
532
533 if (ParamName) {
534 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000535 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000536 IdResolver.AddDecl(Param);
537 }
538
Douglas Gregor61c4d282011-01-05 15:48:55 +0000539 // C++0x [temp.param]p9:
540 // A default template-argument may be specified for any kind of
541 // template-parameter that is not a template parameter pack.
542 if (DefaultArg && Ellipsis) {
543 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
544 DefaultArg = ParsedType();
545 }
546
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000547 // Handle the default argument, if provided.
548 if (DefaultArg) {
549 TypeSourceInfo *DefaultTInfo;
550 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000551
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000552 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000553
Douglas Gregor6f526752010-12-16 08:48:57 +0000554 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000555 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000556 UPPC_DefaultArgument))
557 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000558
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000559 // Check the template argument itself.
560 if (CheckTemplateArgument(Param, DefaultTInfo)) {
561 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000562 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000563 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000564
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000565 Param->setDefaultArgument(DefaultTInfo, false);
566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567
John McCalld226f652010-08-21 09:40:31 +0000568 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000569}
570
Douglas Gregor2943aed2009-03-03 04:44:36 +0000571/// \brief Check that the type of a non-type template parameter is
572/// well-formed.
573///
574/// \returns the (possibly-promoted) parameter type if valid;
575/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000576QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000577Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000578 // We don't allow variably-modified types as the type of non-type template
579 // parameters.
580 if (T->isVariablyModifiedType()) {
581 Diag(Loc, diag::err_variably_modified_nontype_template_param)
582 << T;
583 return QualType();
584 }
585
Douglas Gregor2943aed2009-03-03 04:44:36 +0000586 // C++ [temp.param]p4:
587 //
588 // A non-type template-parameter shall have one of the following
589 // (optionally cv-qualified) types:
590 //
591 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000592 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000593 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000594 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000595 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000596 T->isReferenceType() ||
597 // -- pointer to member.
598 T->isMemberPointerType() ||
599 // If T is a dependent type, we can't do the check now, so we
600 // assume that it is well-formed.
601 T->isDependentType())
602 return T;
603 // C++ [temp.param]p8:
604 //
605 // A non-type template-parameter of type "array of T" or
606 // "function returning T" is adjusted to be of type "pointer to
607 // T" or "pointer to function returning T", respectively.
608 else if (T->isArrayType())
609 // FIXME: Keep the type prior to promotion?
610 return Context.getArrayDecayedType(T);
611 else if (T->isFunctionType())
612 // FIXME: Keep the type prior to promotion?
613 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000614
Douglas Gregor2943aed2009-03-03 04:44:36 +0000615 Diag(Loc, diag::err_template_nontype_parm_bad_type)
616 << T;
617
618 return QualType();
619}
620
John McCalld226f652010-08-21 09:40:31 +0000621Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
622 unsigned Depth,
623 unsigned Position,
624 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000625 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000626 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
627 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000628
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000629 assert(S->isTemplateParamScope() &&
630 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000631 bool Invalid = false;
632
633 IdentifierInfo *ParamName = D.getIdentifier();
634 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000635 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000636 LookupOrdinaryName,
637 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000638 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000639 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000640 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000641 }
642
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000643 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
644 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000645 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000646 Invalid = true;
647 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000648
Douglas Gregor10738d32010-12-23 23:51:58 +0000649 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000650 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000651 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
652 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000653 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000654 IsParameterPack, TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000655 if (Invalid)
656 Param->setInvalidDecl();
657
658 if (D.getIdentifier()) {
659 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000660 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000661 IdResolver.AddDecl(Param);
662 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000663
Douglas Gregor61c4d282011-01-05 15:48:55 +0000664 // C++0x [temp.param]p9:
665 // A default template-argument may be specified for any kind of
666 // template-parameter that is not a template parameter pack.
667 if (Default && IsParameterPack) {
668 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
669 Default = 0;
670 }
671
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000672 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000673 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000674 // Check for unexpanded parameter packs.
675 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
676 return Param;
677
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000678 TemplateArgument Converted;
679 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
680 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000681 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683
John McCall9ae2f072010-08-23 23:25:46 +0000684 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000686
John McCalld226f652010-08-21 09:40:31 +0000687 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000688}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000689
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000690/// ActOnTemplateTemplateParameter - Called when a C++ template template
691/// parameter (e.g. T in template <template <typename> class T> class array)
692/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000693Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
694 SourceLocation TmpLoc,
695 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000696 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000697 IdentifierInfo *Name,
698 SourceLocation NameLoc,
699 unsigned Depth,
700 unsigned Position,
701 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000702 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000703 assert(S->isTemplateParamScope() &&
704 "Template template parameter not in template parameter scope!");
705
706 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000707 bool IsParameterPack = EllipsisLoc.isValid();
708 // FIXME: Pack-ness is dropped
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000709 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000710 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000711 NameLoc.isInvalid()? TmpLoc : NameLoc,
712 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000713 Name, Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000714
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000715 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000716 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000717 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000718 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000719 IdResolver.AddDecl(Param);
720 }
721
Douglas Gregor6f526752010-12-16 08:48:57 +0000722 if (Params->size() == 0) {
723 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
724 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
725 Param->setInvalidDecl();
726 }
727
Douglas Gregor61c4d282011-01-05 15:48:55 +0000728 // C++0x [temp.param]p9:
729 // A default template-argument may be specified for any kind of
730 // template-parameter that is not a template parameter pack.
731 if (IsParameterPack && !Default.isInvalid()) {
732 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
733 Default = ParsedTemplateArgument();
734 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000735
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000736 if (!Default.isInvalid()) {
737 // Check only that we have a template template argument. We don't want to
738 // try to check well-formedness now, because our template template parameter
739 // might have dependent types in its template parameters, which we wouldn't
740 // be able to match now.
741 //
742 // If none of the template template parameter's template arguments mention
743 // other template parameters, we could actually perform more checking here.
744 // However, it isn't worth doing.
745 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
746 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
747 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
748 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000749 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000750 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000751
Douglas Gregor6f526752010-12-16 08:48:57 +0000752 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000753 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000754 DefaultArg.getArgument().getAsTemplate(),
755 UPPC_DefaultArgument))
756 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000758 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000759 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000760
John McCalld226f652010-08-21 09:40:31 +0000761 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000762}
763
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000764/// ActOnTemplateParameterList - Builds a TemplateParameterList that
765/// contains the template parameters in Params/NumParams.
766Sema::TemplateParamsTy *
767Sema::ActOnTemplateParameterList(unsigned Depth,
768 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000769 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000770 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000771 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000772 SourceLocation RAngleLoc) {
773 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000774 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000775
Douglas Gregorddc29e12009-02-06 22:42:48 +0000776 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000777 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000778 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000779}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000780
John McCallb6217662010-03-15 10:12:16 +0000781static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
782 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000783 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000784}
785
John McCallf312b1e2010-08-26 23:41:50 +0000786DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000787Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000788 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000789 IdentifierInfo *Name, SourceLocation NameLoc,
790 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000791 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000792 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000793 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000794 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000795 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000796 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000797
798 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000799 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000800 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000801
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000802 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
803 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000804
805 // There is no such thing as an unnamed class template.
806 if (!Name) {
807 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000808 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000809 }
810
811 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000812 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000813 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000814 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000815 if (SS.isNotEmpty() && !SS.isInvalid()) {
816 SemanticContext = computeDeclContext(SS, true);
817 if (!SemanticContext) {
818 // FIXME: Produce a reasonable diagnostic here
819 return true;
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
John McCall77bb1aa2010-05-01 00:40:08 +0000822 if (RequireCompleteDeclContext(SS, SemanticContext))
823 return true;
824
John McCalla24dc2e2009-11-17 02:14:36 +0000825 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000826 } else {
827 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000828 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Douglas Gregor57265e32010-04-12 16:00:01 +0000831 if (Previous.isAmbiguous())
832 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000833
Douglas Gregorddc29e12009-02-06 22:42:48 +0000834 NamedDecl *PrevDecl = 0;
835 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000836 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000837
Douglas Gregorddc29e12009-02-06 22:42:48 +0000838 // If there is a previous declaration with the same name, check
839 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000840 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000842
843 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000844 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000845 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000846 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000847 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
848 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000849 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000850 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
851 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
852 PrevClassTemplate
853 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
854 ->getSpecializedTemplate();
855 }
856 }
857
John McCall65c49462009-12-18 11:25:59 +0000858 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000859 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000860 // [...] When looking for a prior declaration of a class or a function
861 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000862 // function is neither a qualified name nor a template-id, scopes outside
863 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000864 if (!SS.isSet()) {
865 DeclContext *OutermostContext = CurContext;
866 while (!OutermostContext->isFileContext())
867 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000868
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000869 if (PrevDecl &&
870 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
871 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
872 SemanticContext = PrevDecl->getDeclContext();
873 } else {
874 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000875 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000876 // declaration.
877 PrevDecl = PrevClassTemplate = 0;
878 SemanticContext = OutermostContext;
879 }
John McCalle129d442009-12-17 23:21:11 +0000880 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000881
John McCalle129d442009-12-17 23:21:11 +0000882 if (CurContext->isDependentContext()) {
883 // If this is a dependent context, we don't want to link the friend
884 // class template to the template in scope, because that would perform
885 // checking of the template parameter lists that can't be performed
886 // until the outer context is instantiated.
887 PrevDecl = PrevClassTemplate = 0;
888 }
889 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
890 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000891
Douglas Gregorddc29e12009-02-06 22:42:48 +0000892 if (PrevClassTemplate) {
893 // Ensure that the template parameter lists are compatible.
894 if (!TemplateParameterListsAreEqual(TemplateParams,
895 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000896 /*Complain=*/true,
897 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000898 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000899
900 // C++ [temp.class]p4:
901 // In a redeclaration, partial specialization, explicit
902 // specialization or explicit instantiation of a class template,
903 // the class-key shall agree in kind with the original class
904 // template declaration (7.1.5.3).
905 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000906 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000907 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000908 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000909 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000910 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000911 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000912 }
913
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000915 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000916 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000917 Diag(NameLoc, diag::err_redefinition) << Name;
918 Diag(Def->getLocation(), diag::note_previous_definition);
919 // FIXME: Would it make sense to try to "forget" the previous
920 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000921 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000922 }
923 }
924 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
925 // Maybe we will complain about the shadowed template parameter.
926 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
927 // Just pretend that we didn't see the previous declaration.
928 PrevDecl = 0;
929 } else if (PrevDecl) {
930 // C++ [temp]p5:
931 // A class template shall not have the same name as any other
932 // template, class, function, object, enumeration, enumerator,
933 // namespace, or type in the same scope (3.3), except as specified
934 // in (14.5.4).
935 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
936 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000937 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000938 }
939
Douglas Gregord684b002009-02-10 19:49:53 +0000940 // Check the template parameter list of this declaration, possibly
941 // merging in the template parameter list from the previous class
942 // template declaration.
943 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000944 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000945 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000946 SemanticContext->isRecord() &&
947 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000948 ? TPC_ClassTemplateMember
949 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000950 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Douglas Gregor57265e32010-04-12 16:00:01 +0000952 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000953 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000954 // template out-of-line.
955 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
956 !(TUK == TUK_Friend && CurContext->isDependentContext()))
957 Diag(NameLoc, diag::err_member_def_does_not_match)
958 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000959 }
960
Mike Stump1eb44332009-09-09 15:08:12 +0000961 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000962 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000963 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000964 PrevClassTemplate->getTemplatedDecl() : 0,
965 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000966 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000967
968 ClassTemplateDecl *NewTemplate
969 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
970 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000971 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000972 NewClass->setDescribedClassTemplate(NewTemplate);
973
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000974 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000975 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000976 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000977 assert(T->isDependentType() && "Class template type is not dependent?");
978 (void)T;
979
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000980 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000981 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000982 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000983 PrevClassTemplate->getInstantiatedFromMemberTemplate())
984 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000985
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000986 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000987 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000988 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregorddc29e12009-02-06 22:42:48 +0000990 // Set the lexical context of these templates
991 NewClass->setLexicalDeclContext(CurContext);
992 NewTemplate->setLexicalDeclContext(CurContext);
993
John McCall0f434ec2009-07-31 02:45:11 +0000994 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000995 NewClass->startDefinition();
996
997 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000998 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000999
John McCall05b23ea2009-09-14 21:59:20 +00001000 if (TUK != TUK_Friend)
1001 PushOnScopeChains(NewTemplate, S);
1002 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001003 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001004 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001005 NewClass->setAccess(PrevClassTemplate->getAccess());
1006 }
John McCall05b23ea2009-09-14 21:59:20 +00001007
Douglas Gregord85bea22009-09-26 06:47:28 +00001008 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1009 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001010
John McCall05b23ea2009-09-14 21:59:20 +00001011 // Friend templates are visible in fairly strange ways.
1012 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001013 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001014 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1015 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1016 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001017 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001018 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019
Douglas Gregord85bea22009-09-26 06:47:28 +00001020 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1021 NewClass->getLocation(),
1022 NewTemplate,
1023 /*FIXME:*/NewClass->getLocation());
1024 Friend->setAccess(AS_public);
1025 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001026 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001027
Douglas Gregord684b002009-02-10 19:49:53 +00001028 if (Invalid) {
1029 NewTemplate->setInvalidDecl();
1030 NewClass->setInvalidDecl();
1031 }
John McCalld226f652010-08-21 09:40:31 +00001032 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001033}
1034
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001035/// \brief Diagnose the presence of a default template argument on a
1036/// template parameter, which is ill-formed in certain contexts.
1037///
1038/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001039static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001040 Sema::TemplateParamListContext TPC,
1041 SourceLocation ParamLoc,
1042 SourceRange DefArgRange) {
1043 switch (TPC) {
1044 case Sema::TPC_ClassTemplate:
1045 return false;
1046
1047 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001048 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001049 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001050 // A default template-argument shall not be specified in a
1051 // function template declaration or a function template
1052 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001053 // If a friend function template declaration specifies a default
1054 // template-argument, that declaration shall be a definition and shall be
1055 // the only declaration of the function template in the translation unit.
1056 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001057 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001058 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001059 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001060 << DefArgRange;
1061 return false;
1062
1063 case Sema::TPC_ClassTemplateMember:
1064 // C++0x [temp.param]p9:
1065 // A default template-argument shall not be specified in the
1066 // template-parameter-lists of the definition of a member of a
1067 // class template that appears outside of the member's class.
1068 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1069 << DefArgRange;
1070 return true;
1071
1072 case Sema::TPC_FriendFunctionTemplate:
1073 // C++ [temp.param]p9:
1074 // A default template-argument shall not be specified in a
1075 // friend template declaration.
1076 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1077 << DefArgRange;
1078 return true;
1079
1080 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1081 // for friend function templates if there is only a single
1082 // declaration (and it is a definition). Strange!
1083 }
1084
1085 return false;
1086}
1087
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001088/// \brief Check for unexpanded parameter packs within the template parameters
1089/// of a template template parameter, recursively.
1090bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1091 TemplateParameterList *Params = TTP->getTemplateParameters();
1092 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1093 NamedDecl *P = Params->getParam(I);
1094 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001095 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001096 NTTP->getTypeSourceInfo(),
1097 Sema::UPPC_NonTypeTemplateParameterType))
1098 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001099
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001100 continue;
1101 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001102
1103 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001104 = dyn_cast<TemplateTemplateParmDecl>(P))
1105 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1106 return true;
1107 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001108
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001109 return false;
1110}
1111
Douglas Gregord684b002009-02-10 19:49:53 +00001112/// \brief Checks the validity of a template parameter list, possibly
1113/// considering the template parameter list from a previous
1114/// declaration.
1115///
1116/// If an "old" template parameter list is provided, it must be
1117/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1118/// template parameter list.
1119///
1120/// \param NewParams Template parameter list for a new template
1121/// declaration. This template parameter list will be updated with any
1122/// default arguments that are carried through from the previous
1123/// template parameter list.
1124///
1125/// \param OldParams If provided, template parameter list from a
1126/// previous declaration of the same template. Default template
1127/// arguments will be merged from the old template parameter list to
1128/// the new template parameter list.
1129///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001130/// \param TPC Describes the context in which we are checking the given
1131/// template parameter list.
1132///
Douglas Gregord684b002009-02-10 19:49:53 +00001133/// \returns true if an error occurred, false otherwise.
1134bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001135 TemplateParameterList *OldParams,
1136 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001137 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Douglas Gregord684b002009-02-10 19:49:53 +00001139 // C++ [temp.param]p10:
1140 // The set of default template-arguments available for use with a
1141 // template declaration or definition is obtained by merging the
1142 // default arguments from the definition (if in scope) and all
1143 // declarations in scope in the same way default function
1144 // arguments are (8.3.6).
1145 bool SawDefaultArgument = false;
1146 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001147
Anders Carlsson49d25572009-06-12 23:20:15 +00001148 bool SawParameterPack = false;
1149 SourceLocation ParameterPackLoc;
1150
Mike Stump1a35fde2009-02-11 23:03:27 +00001151 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001152 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001153 if (OldParams)
1154 OldParam = OldParams->begin();
1155
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001156 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001157 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1158 NewParamEnd = NewParams->end();
1159 NewParam != NewParamEnd; ++NewParam) {
1160 // Variables used to diagnose redundant default arguments
1161 bool RedundantDefaultArg = false;
1162 SourceLocation OldDefaultLoc;
1163 SourceLocation NewDefaultLoc;
1164
1165 // Variables used to diagnose missing default arguments
1166 bool MissingDefaultArg = false;
1167
Anders Carlsson49d25572009-06-12 23:20:15 +00001168 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001169 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001170 // parameter pack, it shall be the last template parameter.
1171 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001172 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001173 diag::err_template_param_pack_must_be_last_template_parameter);
1174 Invalid = true;
1175 }
1176
Douglas Gregord684b002009-02-10 19:49:53 +00001177 if (TemplateTypeParmDecl *NewTypeParm
1178 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001179 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001180 if (NewTypeParm->hasDefaultArgument() &&
1181 DiagnoseDefaultTemplateArgument(*this, TPC,
1182 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001183 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001184 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001185 NewTypeParm->removeDefaultArgument();
1186
1187 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001188 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001189 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Anders Carlsson49d25572009-06-12 23:20:15 +00001191 if (NewTypeParm->isParameterPack()) {
1192 assert(!NewTypeParm->hasDefaultArgument() &&
1193 "Parameter packs can't have a default argument!");
1194 SawParameterPack = true;
1195 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001196 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001197 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001198 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1199 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1200 SawDefaultArgument = true;
1201 RedundantDefaultArg = true;
1202 PreviousDefaultArgLoc = NewDefaultLoc;
1203 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1204 // Merge the default argument from the old declaration to the
1205 // new declaration.
1206 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001207 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001208 true);
1209 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1210 } else if (NewTypeParm->hasDefaultArgument()) {
1211 SawDefaultArgument = true;
1212 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1213 } else if (SawDefaultArgument)
1214 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001215 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001216 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001217 // Check for unexpanded parameter packs.
1218 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001219 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001220 UPPC_NonTypeTemplateParameterType)) {
1221 Invalid = true;
1222 continue;
1223 }
1224
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001225 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001226 if (NewNonTypeParm->hasDefaultArgument() &&
1227 DiagnoseDefaultTemplateArgument(*this, TPC,
1228 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001229 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001230 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001231 }
1232
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001233 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001234 NonTypeTemplateParmDecl *OldNonTypeParm
1235 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001236 if (NewNonTypeParm->isParameterPack()) {
1237 assert(!NewNonTypeParm->hasDefaultArgument() &&
1238 "Parameter packs can't have a default argument!");
1239 SawParameterPack = true;
1240 ParameterPackLoc = NewNonTypeParm->getLocation();
1241 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001242 NewNonTypeParm->hasDefaultArgument()) {
1243 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1244 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1245 SawDefaultArgument = true;
1246 RedundantDefaultArg = true;
1247 PreviousDefaultArgLoc = NewDefaultLoc;
1248 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1249 // Merge the default argument from the old declaration to the
1250 // new declaration.
1251 SawDefaultArgument = true;
1252 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001253 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001254 // parameter.
1255 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001256 OldNonTypeParm->getDefaultArgument(),
1257 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001258 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1259 } else if (NewNonTypeParm->hasDefaultArgument()) {
1260 SawDefaultArgument = true;
1261 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1262 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001263 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001264 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001265 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001266 TemplateTemplateParmDecl *NewTemplateParm
1267 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001268
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001269 // Check for unexpanded parameter packs, recursively.
1270 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1271 Invalid = true;
1272 continue;
1273 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001274
1275 if (NewTemplateParm->hasDefaultArgument() &&
1276 DiagnoseDefaultTemplateArgument(*this, TPC,
1277 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001278 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001279 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001280
1281 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001282 TemplateTemplateParmDecl *OldTemplateParm
1283 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001284 if (NewTemplateParm->isParameterPack()) {
1285 assert(!NewTemplateParm->hasDefaultArgument() &&
1286 "Parameter packs can't have a default argument!");
1287 SawParameterPack = true;
1288 ParameterPackLoc = NewTemplateParm->getLocation();
1289 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001290 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001291 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1292 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001293 SawDefaultArgument = true;
1294 RedundantDefaultArg = true;
1295 PreviousDefaultArgLoc = NewDefaultLoc;
1296 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1297 // Merge the default argument from the old declaration to the
1298 // new declaration.
1299 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001300 // FIXME: We need to create a new kind of "default argument" expression
1301 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001302 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001303 OldTemplateParm->getDefaultArgument(),
1304 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001305 PreviousDefaultArgLoc
1306 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001307 } else if (NewTemplateParm->hasDefaultArgument()) {
1308 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001309 PreviousDefaultArgLoc
1310 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001311 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001312 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001313 }
1314
1315 if (RedundantDefaultArg) {
1316 // C++ [temp.param]p12:
1317 // A template-parameter shall not be given default arguments
1318 // by two different declarations in the same scope.
1319 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1320 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1321 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001322 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001323 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001324 // If a template-parameter of a class template has a default
1325 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001326 // have a default template-argument supplied or be a template parameter
1327 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001328 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001329 diag::err_template_param_default_arg_missing);
1330 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1331 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001332 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001333 }
1334
1335 // If we have an old template parameter list that we're merging
1336 // in, move on to the next parameter.
1337 if (OldParams)
1338 ++OldParam;
1339 }
1340
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001341 // We were missing some default arguments at the end of the list, so remove
1342 // all of the default arguments.
1343 if (RemoveDefaultArguments) {
1344 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1345 NewParamEnd = NewParams->end();
1346 NewParam != NewParamEnd; ++NewParam) {
1347 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1348 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001349 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001350 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1351 NTTP->removeDefaultArgument();
1352 else
1353 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1354 }
1355 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001356
Douglas Gregord684b002009-02-10 19:49:53 +00001357 return Invalid;
1358}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001359
John McCall4e2cbb22010-10-20 05:44:58 +00001360namespace {
1361
1362/// A class which looks for a use of a certain level of template
1363/// parameter.
1364struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1365 typedef RecursiveASTVisitor<DependencyChecker> super;
1366
1367 unsigned Depth;
1368 bool Match;
1369
1370 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1371 NamedDecl *ND = Params->getParam(0);
1372 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1373 Depth = PD->getDepth();
1374 } else if (NonTypeTemplateParmDecl *PD =
1375 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1376 Depth = PD->getDepth();
1377 } else {
1378 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1379 }
1380 }
1381
1382 bool Matches(unsigned ParmDepth) {
1383 if (ParmDepth >= Depth) {
1384 Match = true;
1385 return true;
1386 }
1387 return false;
1388 }
1389
1390 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1391 return !Matches(T->getDepth());
1392 }
1393
1394 bool TraverseTemplateName(TemplateName N) {
1395 if (TemplateTemplateParmDecl *PD =
1396 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1397 if (Matches(PD->getDepth())) return false;
1398 return super::TraverseTemplateName(N);
1399 }
1400
1401 bool VisitDeclRefExpr(DeclRefExpr *E) {
1402 if (NonTypeTemplateParmDecl *PD =
1403 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1404 if (PD->getDepth() == Depth) {
1405 Match = true;
1406 return false;
1407 }
1408 }
1409 return super::VisitDeclRefExpr(E);
1410 }
1411};
1412}
1413
1414/// Determines whether a template-id depends on the given parameter
1415/// list.
1416static bool
1417DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1418 TemplateParameterList *Params) {
1419 DependencyChecker Checker(Params);
1420 Checker.TraverseType(QualType(TemplateId, 0));
1421 return Checker.Match;
1422}
1423
Mike Stump1eb44332009-09-09 15:08:12 +00001424/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001425/// specifier, returning the template parameter list that applies to the
1426/// name.
1427///
1428/// \param DeclStartLoc the start of the declaration that has a scope
1429/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001430///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001431/// \param SS the scope specifier that will be matched to the given template
1432/// parameter lists. This scope specifier precedes a qualified name that is
1433/// being declared.
1434///
1435/// \param ParamLists the template parameter lists, from the outermost to the
1436/// innermost template parameter lists.
1437///
1438/// \param NumParamLists the number of template parameter lists in ParamLists.
1439///
John McCall77e8b112010-04-13 20:37:33 +00001440/// \param IsFriend Whether to apply the slightly different rules for
1441/// matching template parameters to scope specifiers in friend
1442/// declarations.
1443///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001444/// \param IsExplicitSpecialization will be set true if the entity being
1445/// declared is an explicit specialization, false otherwise.
1446///
Mike Stump1eb44332009-09-09 15:08:12 +00001447/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001448/// name that is preceded by the scope specifier @p SS. This template
1449/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001450/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001451/// template specialization), or may be NULL (if we were's declaring isn't
1452/// itself a template).
1453TemplateParameterList *
1454Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1455 const CXXScopeSpec &SS,
1456 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001457 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001458 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001459 bool &IsExplicitSpecialization,
1460 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001461 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001462
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001463 // Find the template-ids that occur within the nested-name-specifier. These
1464 // template-ids will match up with the template parameter lists.
1465 llvm::SmallVector<const TemplateSpecializationType *, 4>
1466 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001467 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1468 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001469 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1470 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001471 const Type *T = NNS->getAsType();
1472 if (!T) break;
1473
1474 // C++0x [temp.expl.spec]p17:
1475 // A member or a member template may be nested within many
1476 // enclosing class templates. In an explicit specialization for
1477 // such a member, the member declaration shall be preceded by a
1478 // template<> for each enclosing class template that is
1479 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001480 //
1481 // Following the existing practice of GNU and EDG, we allow a typedef of a
1482 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001483 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1484 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001485
Mike Stump1eb44332009-09-09 15:08:12 +00001486 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001487 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001488 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1489 if (!Template)
1490 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Ted Kremenek6217b802009-07-29 21:53:49 +00001492 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001493 ClassTemplateSpecializationDecl *SpecDecl
1494 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1495 // If the nested name specifier refers to an explicit specialization,
1496 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001497 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1498 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001499 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001500 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503 TemplateIdsInSpecifier.push_back(SpecType);
1504 }
1505 }
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001507 // Reverse the list of template-ids in the scope specifier, so that we can
1508 // more easily match up the template-ids and the template parameter lists.
1509 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001511 SourceLocation FirstTemplateLoc = DeclStartLoc;
1512 if (NumParamLists)
1513 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001515 // Match the template-ids found in the specifier to the template parameter
1516 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001517 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001518 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001519 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1520 const TemplateSpecializationType *TemplateId
1521 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001522 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001523
1524 // In friend declarations we can have template-ids which don't
1525 // depend on the corresponding template parameter lists. But
1526 // assume that empty parameter lists are supposed to match this
1527 // template-id.
1528 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1529 if (!DependentTemplateId ||
1530 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1531 continue;
1532 }
1533
1534 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001535 // We have a template-id without a corresponding template parameter
1536 // list.
John McCall77e8b112010-04-13 20:37:33 +00001537
1538 // ...which is fine if this is a friend declaration.
1539 if (IsFriend) {
1540 IsExplicitSpecialization = true;
1541 break;
1542 }
1543
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001544 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001545 // FIXME: the location information here isn't great.
1546 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001547 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001548 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001549 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001550 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001551 } else {
1552 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1553 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001554 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001555 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001556 }
1557 return 0;
1558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001560 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001561 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001562 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001563
John McCall31f17ec2010-04-27 00:57:59 +00001564 // Are there cases in (e.g.) friends where this won't match?
1565 if (const InjectedClassNameType *Injected
1566 = TemplateId->getAs<InjectedClassNameType>()) {
1567 CXXRecordDecl *Record = Injected->getDecl();
1568 if (ClassTemplatePartialSpecializationDecl *Partial =
1569 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1570 ExpectedTemplateParams = Partial->getTemplateParameters();
1571 else
1572 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1573 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001574 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001575
John McCall31f17ec2010-04-27 00:57:59 +00001576 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001577 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001578 ExpectedTemplateParams,
1579 true, TPL_TemplateMatch);
1580
John McCall4e2cbb22010-10-20 05:44:58 +00001581 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1582 TPC_ClassTemplateMember);
1583 } else if (ParamLists[ParamIdx]->size() > 0)
1584 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001585 diag::err_template_param_list_matches_nontemplate)
1586 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001587 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001588 else
1589 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001590
1591 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001594 // If there were at least as many template-ids as there were template
1595 // parameter lists, then there are no template parameter lists remaining for
1596 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001597 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001598 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001600 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001601 if (ParamIdx != NumParamLists - 1) {
1602 while (ParamIdx < NumParamLists - 1) {
1603 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1604 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001605 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1606 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001607 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1608 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001609
1610 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1611 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1612 diag::note_explicit_template_spec_does_not_need_header)
1613 << ExplicitSpecializationsInSpecifier.back();
1614 ExplicitSpecializationsInSpecifier.pop_back();
1615 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001616
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001617 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001618 // means that the resulting template declaration can't be instantiated
1619 // properly (we'll end up with dependent nodes when we shouldn't).
1620 if (!isExplicitSpecHeader)
1621 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001622
John McCall4e2cbb22010-10-20 05:44:58 +00001623 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001624 }
1625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001627 // Return the last template parameter list, which corresponds to the
1628 // entity being declared.
1629 return ParamLists[NumParamLists - 1];
1630}
1631
Douglas Gregor7532dc62009-03-30 22:58:21 +00001632QualType Sema::CheckTemplateIdType(TemplateName Name,
1633 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00001634 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001635 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001636 if (!Template) {
1637 // The template name does not resolve to a template, so we just
1638 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001639 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001640 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001641
Douglas Gregor40808ce2009-03-09 23:48:35 +00001642 // Check that the template argument list is well-formed for this
1643 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001644 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001645 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001646 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001647 return QualType();
1648
Douglas Gregor910f8002010-11-07 23:05:16 +00001649 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001650 "Converted template argument list is too short!");
1651
1652 QualType CanonType;
1653
Douglas Gregorcaddba02009-11-12 18:38:13 +00001654 if (Name.isDependent() ||
1655 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001656 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001657 // This class template specialization is a dependent
1658 // type. Therefore, its canonical type is another class template
1659 // specialization type that contains all of the converted
1660 // arguments in canonical form. This ensures that, e.g., A<T> and
1661 // A<T, T> have identical types when A is declared as:
1662 //
1663 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001664 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001665 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001666 Converted.data(),
1667 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Douglas Gregor1275ae02009-07-28 23:00:59 +00001669 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001670 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001671 // In the future, we need to teach getTemplateSpecializationType to only
1672 // build the canonical type and return that to us.
1673 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001674
1675 // This might work out to be a current instantiation, in which
1676 // case the canonical type needs to be the InjectedClassNameType.
1677 //
1678 // TODO: in theory this could be a simple hashtable lookup; most
1679 // changes to CurContext don't change the set of current
1680 // instantiations.
1681 if (isa<ClassTemplateDecl>(Template)) {
1682 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1683 // If we get out to a namespace, we're done.
1684 if (Ctx->isFileContext()) break;
1685
1686 // If this isn't a record, keep looking.
1687 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1688 if (!Record) continue;
1689
1690 // Look for one of the two cases with InjectedClassNameTypes
1691 // and check whether it's the same template.
1692 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1693 !Record->getDescribedClassTemplate())
1694 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001695
John McCall31f17ec2010-04-27 00:57:59 +00001696 // Fetch the injected class name type and check whether its
1697 // injected type is equal to the type we just built.
1698 QualType ICNT = Context.getTypeDeclType(Record);
1699 QualType Injected = cast<InjectedClassNameType>(ICNT)
1700 ->getInjectedSpecializationType();
1701
1702 if (CanonType != Injected->getCanonicalTypeInternal())
1703 continue;
1704
1705 // If so, the canonical type of this TST is the injected
1706 // class name type of the record we just found.
1707 assert(ICNT.isCanonical());
1708 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001709 break;
1710 }
1711 }
Mike Stump1eb44332009-09-09 15:08:12 +00001712 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001713 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001714 // Find the class template specialization declaration that
1715 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001716 void *InsertPos = 0;
1717 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001718 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001719 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001720 if (!Decl) {
1721 // This is the first time we have referenced this class template
1722 // specialization. Create the canonical declaration and add it to
1723 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001724 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001725 ClassTemplate->getTemplatedDecl()->getTagKind(),
1726 ClassTemplate->getDeclContext(),
1727 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001728 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001729 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001730 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001731 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001732 Decl->setLexicalDeclContext(CurContext);
1733 }
1734
1735 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001736 assert(isa<RecordType>(CanonType) &&
1737 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001738 }
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Douglas Gregor40808ce2009-03-09 23:48:35 +00001740 // Build the fully-sugared type for this class template
1741 // specialization, which refers back to the class template
1742 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001743 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001744}
1745
John McCallf312b1e2010-08-26 23:41:50 +00001746TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001747Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1748 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001749 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001750 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001751 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001752 if (SS.isInvalid())
1753 return true;
1754
Douglas Gregor7532dc62009-03-30 22:58:21 +00001755 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001756
Douglas Gregor40808ce2009-03-09 23:48:35 +00001757 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001758 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001759 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001760
Douglas Gregora88f09f2011-02-28 17:23:35 +00001761 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1762 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1763 DTN->getQualifier(),
1764 DTN->getIdentifier(),
1765 TemplateArgs);
1766
1767 // Build type-source information.
1768 TypeLocBuilder TLB;
1769 DependentTemplateSpecializationTypeLoc SpecTL
1770 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001771 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001772 SpecTL.setNameLoc(TemplateLoc);
1773 SpecTL.setLAngleLoc(LAngleLoc);
1774 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001775 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001776 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1777 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1778 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1779 }
1780
John McCalld5532b62009-11-23 01:53:49 +00001781 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001782 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001783
1784 if (Result.isNull())
1785 return true;
1786
Douglas Gregor059101f2011-03-02 00:47:37 +00001787 // Build type-source information.
1788 TypeLocBuilder TLB;
1789 TemplateSpecializationTypeLoc SpecTL
1790 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1791 SpecTL.setTemplateNameLoc(TemplateLoc);
1792 SpecTL.setLAngleLoc(LAngleLoc);
1793 SpecTL.setRAngleLoc(RAngleLoc);
1794 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1795 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001796
Douglas Gregor059101f2011-03-02 00:47:37 +00001797 if (SS.isNotEmpty()) {
1798 // Create an elaborated-type-specifier containing the nested-name-specifier.
1799 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1800 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1801 ElabTL.setKeywordLoc(SourceLocation());
1802 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1803 }
1804
1805 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001806}
John McCallf1bbbb42009-09-04 01:14:41 +00001807
Douglas Gregor059101f2011-03-02 00:47:37 +00001808TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001809 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001810 SourceLocation TagLoc,
1811 CXXScopeSpec &SS,
1812 TemplateTy TemplateD,
1813 SourceLocation TemplateLoc,
1814 SourceLocation LAngleLoc,
1815 ASTTemplateArgsPtr TemplateArgsIn,
1816 SourceLocation RAngleLoc) {
1817 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1818
1819 // Translate the parser's template argument list in our AST format.
1820 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1821 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1822
1823 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001824 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001825 ElaboratedTypeKeyword Keyword
1826 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Douglas Gregor059101f2011-03-02 00:47:37 +00001828 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1829 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1830 DTN->getQualifier(),
1831 DTN->getIdentifier(),
1832 TemplateArgs);
1833
1834 // Build type-source information.
1835 TypeLocBuilder TLB;
1836 DependentTemplateSpecializationTypeLoc SpecTL
1837 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1838 SpecTL.setKeywordLoc(TagLoc);
1839 SpecTL.setNameLoc(TemplateLoc);
1840 SpecTL.setLAngleLoc(LAngleLoc);
1841 SpecTL.setRAngleLoc(RAngleLoc);
1842 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1843 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1844 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1845 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1846 }
1847
1848 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1849 if (Result.isNull())
1850 return TypeResult();
1851
1852 // Check the tag kind
1853 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001854 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001855
John McCall6b2becf2009-09-08 17:47:29 +00001856 IdentifierInfo *Id = D->getIdentifier();
1857 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001858
John McCall6b2becf2009-09-08 17:47:29 +00001859 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1860 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001861 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001862 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001863 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001864 }
1865 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001866
1867 // Provide source-location information for the template specialization.
1868 TypeLocBuilder TLB;
1869 TemplateSpecializationTypeLoc SpecTL
1870 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1871 SpecTL.setTemplateNameLoc(TemplateLoc);
1872 SpecTL.setLAngleLoc(LAngleLoc);
1873 SpecTL.setRAngleLoc(RAngleLoc);
1874 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1875 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001876
Douglas Gregor059101f2011-03-02 00:47:37 +00001877 // Construct an elaborated type containing the nested-name-specifier (if any)
1878 // and keyword.
1879 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1880 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1881 ElabTL.setKeywordLoc(TagLoc);
1882 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1883 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001884}
1885
John McCall60d7b3a2010-08-24 06:29:42 +00001886ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001887 LookupResult &R,
1888 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001889 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001890 // FIXME: Can we do any checking at this point? I guess we could check the
1891 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001892 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001893 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001894 // foo<int> could identify a single function unambiguously
1895 // This approach does NOT work, since f<int>(1);
1896 // gets resolved prior to resorting to overload resolution
1897 // i.e., template<class T> void f(double);
1898 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001899
1900 // These should be filtered out by our callers.
1901 assert(!R.empty() && "empty lookup results when building templateid");
1902 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1903
John McCallc373d482010-01-27 01:50:18 +00001904 // We don't want lookup warnings at this point.
1905 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001906
John McCallf7a1a742009-11-24 19:00:30 +00001907 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001908 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001909 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001910 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001911 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001912 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001913
1914 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001915}
1916
John McCallf7a1a742009-11-24 19:00:30 +00001917// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001918ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001919Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001920 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001921 const TemplateArgumentListInfo &TemplateArgs) {
1922 DeclContext *DC;
1923 if (!(DC = computeDeclContext(SS, false)) ||
1924 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001925 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001926 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001928 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001929 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001930 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1931 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
John McCallf7a1a742009-11-24 19:00:30 +00001933 if (R.isAmbiguous())
1934 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001935
John McCallf7a1a742009-11-24 19:00:30 +00001936 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001937 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1938 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001939 return ExprError();
1940 }
1941
1942 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001943 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1944 << (NestedNameSpecifier*) SS.getScopeRep()
1945 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001946 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1947 return ExprError();
1948 }
1949
1950 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001951}
1952
Douglas Gregorc45c2322009-03-31 00:43:58 +00001953/// \brief Form a dependent template name.
1954///
1955/// This action forms a dependent template name given the template
1956/// name and its (presumably dependent) scope specifier. For
1957/// example, given "MetaFun::template apply", the scope specifier \p
1958/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1959/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001960TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001961 SourceLocation TemplateKWLoc,
1962 CXXScopeSpec &SS,
1963 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001964 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001965 bool EnteringContext,
1966 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001967 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1968 !getLangOptions().CPlusPlus0x)
1969 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001970 << FixItHint::CreateRemoval(TemplateKWLoc);
1971
Douglas Gregor0707bc52010-01-19 16:01:07 +00001972 DeclContext *LookupCtx = 0;
1973 if (SS.isSet())
1974 LookupCtx = computeDeclContext(SS, EnteringContext);
1975 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00001976 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00001977 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001978 // C++0x [temp.names]p5:
1979 // If a name prefixed by the keyword template is not the name of
1980 // a template, the program is ill-formed. [Note: the keyword
1981 // template may not be applied to non-template members of class
1982 // templates. -end note ] [ Note: as is the case with the
1983 // typename prefix, the template prefix is allowed in cases
1984 // where it is not strictly necessary; i.e., when the
1985 // nested-name-specifier or the expression on the left of the ->
1986 // or . is not dependent on a template-parameter, or the use
1987 // does not appear in the scope of a template. -end note]
1988 //
1989 // Note: C++03 was more strict here, because it banned the use of
1990 // the "template" keyword prior to a template-name that was not a
1991 // dependent name. C++ DR468 relaxed this requirement (the
1992 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00001993 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001994 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001995 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
1996 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001997 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001998 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1999 isa<CXXRecordDecl>(LookupCtx) &&
2000 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002001 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002002 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002003 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002004 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002005 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002006 << Name.getSourceRange()
2007 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002008 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002009 } else {
2010 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002011 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002012 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002013 }
2014
Mike Stump1eb44332009-09-09 15:08:12 +00002015 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002016 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002017
Douglas Gregor014e88d2009-11-03 23:16:33 +00002018 switch (Name.getKind()) {
2019 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002020 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002021 Name.Identifier));
2022 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002023
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002024 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002025 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002026 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002027 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002028
2029 case UnqualifiedId::IK_LiteralOperatorId:
2030 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2031
Douglas Gregor014e88d2009-11-03 23:16:33 +00002032 default:
2033 break;
2034 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002035
2036 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002037 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002038 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002039 << Name.getSourceRange()
2040 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002041 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002042}
2043
Mike Stump1eb44332009-09-09 15:08:12 +00002044bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002045 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002046 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002047 const TemplateArgument &Arg = AL.getArgument();
2048
Anders Carlsson436b1562009-06-13 00:33:33 +00002049 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002050 switch(Arg.getKind()) {
2051 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002052 // C++ [temp.arg.type]p1:
2053 // A template-argument for a template-parameter which is a
2054 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002055 break;
2056 case TemplateArgument::Template: {
2057 // We have a template type parameter but the template argument
2058 // is a template without any arguments.
2059 SourceRange SR = AL.getSourceRange();
2060 TemplateName Name = Arg.getAsTemplate();
2061 Diag(SR.getBegin(), diag::err_template_missing_args)
2062 << Name << SR;
2063 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2064 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002065
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002066 return true;
2067 }
2068 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002069 // We have a template type parameter but the template argument
2070 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002071 SourceRange SR = AL.getSourceRange();
2072 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002073 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Anders Carlsson436b1562009-06-13 00:33:33 +00002075 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002076 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002077 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002078
John McCalla93c9342009-12-07 02:54:59 +00002079 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002080 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Anders Carlsson436b1562009-06-13 00:33:33 +00002082 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002083 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002084 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002085 return false;
2086}
2087
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002088/// \brief Substitute template arguments into the default template argument for
2089/// the given template type parameter.
2090///
2091/// \param SemaRef the semantic analysis object for which we are performing
2092/// the substitution.
2093///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002094/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002095/// for.
2096///
2097/// \param TemplateLoc the location of the template name that started the
2098/// template-id we are checking.
2099///
2100/// \param RAngleLoc the location of the right angle bracket ('>') that
2101/// terminates the template-id.
2102///
2103/// \param Param the template template parameter whose default we are
2104/// substituting into.
2105///
2106/// \param Converted the list of template arguments provided for template
2107/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002108/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002109static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002110SubstDefaultTemplateArgument(Sema &SemaRef,
2111 TemplateDecl *Template,
2112 SourceLocation TemplateLoc,
2113 SourceLocation RAngleLoc,
2114 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002115 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002116 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002117
2118 // If the argument type is dependent, instantiate it now based
2119 // on the previously-computed template arguments.
2120 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002121 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002122 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002123
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002124 MultiLevelTemplateArgumentList AllTemplateArgs
2125 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2126
2127 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002128 Template, Converted.data(),
2129 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002130 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002131
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002132 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2133 Param->getDefaultArgumentLoc(),
2134 Param->getDeclName());
2135 }
2136
2137 return ArgType;
2138}
2139
2140/// \brief Substitute template arguments into the default template argument for
2141/// the given non-type template parameter.
2142///
2143/// \param SemaRef the semantic analysis object for which we are performing
2144/// the substitution.
2145///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002146/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002147/// for.
2148///
2149/// \param TemplateLoc the location of the template name that started the
2150/// template-id we are checking.
2151///
2152/// \param RAngleLoc the location of the right angle bracket ('>') that
2153/// terminates the template-id.
2154///
Douglas Gregor788cd062009-11-11 01:00:40 +00002155/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002156/// substituting into.
2157///
2158/// \param Converted the list of template arguments provided for template
2159/// parameters that precede \p Param in the template parameter list.
2160///
2161/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002162static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002163SubstDefaultTemplateArgument(Sema &SemaRef,
2164 TemplateDecl *Template,
2165 SourceLocation TemplateLoc,
2166 SourceLocation RAngleLoc,
2167 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002168 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002169 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002170 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002171
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002172 MultiLevelTemplateArgumentList AllTemplateArgs
2173 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002174
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002175 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002176 Template, Converted.data(),
2177 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002178 SourceRange(TemplateLoc, RAngleLoc));
2179
2180 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2181}
2182
Douglas Gregor788cd062009-11-11 01:00:40 +00002183/// \brief Substitute template arguments into the default template argument for
2184/// the given template template parameter.
2185///
2186/// \param SemaRef the semantic analysis object for which we are performing
2187/// the substitution.
2188///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002189/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002190/// for.
2191///
2192/// \param TemplateLoc the location of the template name that started the
2193/// template-id we are checking.
2194///
2195/// \param RAngleLoc the location of the right angle bracket ('>') that
2196/// terminates the template-id.
2197///
2198/// \param Param the template template parameter whose default we are
2199/// substituting into.
2200///
2201/// \param Converted the list of template arguments provided for template
2202/// parameters that precede \p Param in the template parameter list.
2203///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002204/// \param QualifierLoc Will be set to the nested-name-specifier (with
2205/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002206///
Douglas Gregor788cd062009-11-11 01:00:40 +00002207/// \returns the substituted template argument, or NULL if an error occurred.
2208static TemplateName
2209SubstDefaultTemplateArgument(Sema &SemaRef,
2210 TemplateDecl *Template,
2211 SourceLocation TemplateLoc,
2212 SourceLocation RAngleLoc,
2213 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002214 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2215 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002216 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002217 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002218
Douglas Gregor788cd062009-11-11 01:00:40 +00002219 MultiLevelTemplateArgumentList AllTemplateArgs
2220 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002221
Douglas Gregor788cd062009-11-11 01:00:40 +00002222 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002223 Template, Converted.data(),
2224 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002225 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002226
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002227 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002228 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002229 if (QualifierLoc) {
2230 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2231 AllTemplateArgs);
2232 if (!QualifierLoc)
2233 return TemplateName();
2234 }
2235
Douglas Gregor1d752d72011-03-02 18:46:51 +00002236 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002237 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002238 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002239 AllTemplateArgs);
2240}
2241
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002242/// \brief If the given template parameter has a default template
2243/// argument, substitute into that default template argument and
2244/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002245TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002246Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2247 SourceLocation TemplateLoc,
2248 SourceLocation RAngleLoc,
2249 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002250 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2251 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002252 if (!TypeParm->hasDefaultArgument())
2253 return TemplateArgumentLoc();
2254
John McCalla93c9342009-12-07 02:54:59 +00002255 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002256 TemplateLoc,
2257 RAngleLoc,
2258 TypeParm,
2259 Converted);
2260 if (DI)
2261 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2262
2263 return TemplateArgumentLoc();
2264 }
2265
2266 if (NonTypeTemplateParmDecl *NonTypeParm
2267 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2268 if (!NonTypeParm->hasDefaultArgument())
2269 return TemplateArgumentLoc();
2270
John McCall60d7b3a2010-08-24 06:29:42 +00002271 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002272 TemplateLoc,
2273 RAngleLoc,
2274 NonTypeParm,
2275 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002276 if (Arg.isInvalid())
2277 return TemplateArgumentLoc();
2278
2279 Expr *ArgE = Arg.takeAs<Expr>();
2280 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2281 }
2282
2283 TemplateTemplateParmDecl *TempTempParm
2284 = cast<TemplateTemplateParmDecl>(Param);
2285 if (!TempTempParm->hasDefaultArgument())
2286 return TemplateArgumentLoc();
2287
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002288
Douglas Gregor1d752d72011-03-02 18:46:51 +00002289 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002290 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002291 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002292 RAngleLoc,
2293 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002294 Converted,
2295 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002296 if (TName.isNull())
2297 return TemplateArgumentLoc();
2298
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002299 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002300 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002301 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2302}
2303
Douglas Gregore7526412009-11-11 19:31:23 +00002304/// \brief Check that the given template argument corresponds to the given
2305/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002306///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002307/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002308/// checked.
2309///
2310/// \param Arg The template argument.
2311///
2312/// \param Template The template in which the template argument resides.
2313///
2314/// \param TemplateLoc The location of the template name for the template
2315/// whose argument list we're matching.
2316///
2317/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2318/// the template argument list.
2319///
2320/// \param ArgumentPackIndex The index into the argument pack where this
2321/// argument will be placed. Only valid if the parameter is a parameter pack.
2322///
2323/// \param Converted The checked, converted argument will be added to the
2324/// end of this small vector.
2325///
2326/// \param CTAK Describes how we arrived at this particular template argument:
2327/// explicitly written, deduced, etc.
2328///
2329/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002330bool Sema::CheckTemplateArgument(NamedDecl *Param,
2331 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002332 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002333 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002334 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002335 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002336 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002337 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002338 // Check template type parameters.
2339 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002340 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002341
Douglas Gregord9e15302009-11-11 19:41:09 +00002342 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002343 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002344 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002345 // with the template arguments we've seen thus far. But if the
2346 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002347 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002348 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2349 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002350
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002351 if (NTTPType->isDependentType() &&
2352 !isa<TemplateTemplateParmDecl>(Template) &&
2353 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002354 // Do substitution on the type of the non-type template parameter.
2355 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002356 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002357 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002358
2359 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002360 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002361 NTTPType = SubstType(NTTPType,
2362 MultiLevelTemplateArgumentList(TemplateArgs),
2363 NTTP->getLocation(),
2364 NTTP->getDeclName());
2365 // If that worked, check the non-type template parameter type
2366 // for validity.
2367 if (!NTTPType.isNull())
2368 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2369 NTTP->getLocation());
2370 if (NTTPType.isNull())
2371 return true;
2372 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002373
Douglas Gregore7526412009-11-11 19:31:23 +00002374 switch (Arg.getArgument().getKind()) {
2375 case TemplateArgument::Null:
2376 assert(false && "Should never see a NULL template argument here");
2377 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002378
Douglas Gregore7526412009-11-11 19:31:23 +00002379 case TemplateArgument::Expression: {
2380 Expr *E = Arg.getArgument().getAsExpr();
2381 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002382 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002383 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002384
Douglas Gregor910f8002010-11-07 23:05:16 +00002385 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002386 break;
2387 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002388
Douglas Gregore7526412009-11-11 19:31:23 +00002389 case TemplateArgument::Declaration:
2390 case TemplateArgument::Integral:
2391 // We've already checked this template argument, so just copy
2392 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002393 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002394 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002395
Douglas Gregore7526412009-11-11 19:31:23 +00002396 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002397 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002398 // We were given a template template argument. It may not be ill-formed;
2399 // see below.
2400 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002401 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2402 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002403 // We have a template argument such as \c T::template X, which we
2404 // parsed as a template template argument. However, since we now
2405 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002406 // template name into an expression.
2407
2408 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2409 Arg.getTemplateNameLoc());
2410
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002411 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002412 SS.Adopt(Arg.getTemplateQualifierLoc());
John McCallf7a1a742009-11-24 19:00:30 +00002413 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002414 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002415 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002416
Douglas Gregora7fc9012011-01-05 18:58:31 +00002417 // If we parsed the template argument as a pack expansion, create a
2418 // pack expansion expression.
2419 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002420 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002421 Arg.getTemplateEllipsisLoc());
2422 if (Expansion.isInvalid())
2423 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002424
Douglas Gregora7fc9012011-01-05 18:58:31 +00002425 E = Expansion.get();
2426 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002427
Douglas Gregore7526412009-11-11 19:31:23 +00002428 TemplateArgument Result;
2429 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2430 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002431
Douglas Gregor910f8002010-11-07 23:05:16 +00002432 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002433 break;
2434 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002435
Douglas Gregore7526412009-11-11 19:31:23 +00002436 // We have a template argument that actually does refer to a class
2437 // template, template alias, or template template parameter, and
2438 // therefore cannot be a non-type template argument.
2439 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2440 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002441
Douglas Gregore7526412009-11-11 19:31:23 +00002442 Diag(Param->getLocation(), diag::note_template_param_here);
2443 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002444
Douglas Gregore7526412009-11-11 19:31:23 +00002445 case TemplateArgument::Type: {
2446 // We have a non-type template parameter but the template
2447 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002448
Douglas Gregore7526412009-11-11 19:31:23 +00002449 // C++ [temp.arg]p2:
2450 // In a template-argument, an ambiguity between a type-id and
2451 // an expression is resolved to a type-id, regardless of the
2452 // form of the corresponding template-parameter.
2453 //
2454 // We warn specifically about this case, since it can be rather
2455 // confusing for users.
2456 QualType T = Arg.getArgument().getAsType();
2457 SourceRange SR = Arg.getSourceRange();
2458 if (T->isFunctionType())
2459 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2460 else
2461 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2462 Diag(Param->getLocation(), diag::note_template_param_here);
2463 return true;
2464 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002465
Douglas Gregore7526412009-11-11 19:31:23 +00002466 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002467 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002468 break;
2469 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002470
Douglas Gregore7526412009-11-11 19:31:23 +00002471 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002472 }
2473
2474
Douglas Gregore7526412009-11-11 19:31:23 +00002475 // Check template template parameters.
2476 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002477
Douglas Gregore7526412009-11-11 19:31:23 +00002478 // Substitute into the template parameter list of the template
2479 // template parameter, since previously-supplied template arguments
2480 // may appear within the template template parameter.
2481 {
2482 // Set up a template instantiation context.
2483 LocalInstantiationScope Scope(*this);
2484 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002485 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002486 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002487
2488 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002489 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002490 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002491 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002492 MultiLevelTemplateArgumentList(TemplateArgs)));
2493 if (!TempParm)
2494 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002495 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002496
Douglas Gregore7526412009-11-11 19:31:23 +00002497 switch (Arg.getArgument().getKind()) {
2498 case TemplateArgument::Null:
2499 assert(false && "Should never see a NULL template argument here");
2500 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002501
Douglas Gregore7526412009-11-11 19:31:23 +00002502 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002503 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002504 if (CheckTemplateArgument(TempParm, Arg))
2505 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002506
Douglas Gregor910f8002010-11-07 23:05:16 +00002507 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002508 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002509
Douglas Gregore7526412009-11-11 19:31:23 +00002510 case TemplateArgument::Expression:
2511 case TemplateArgument::Type:
2512 // We have a template template parameter but the template
2513 // argument does not refer to a template.
2514 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2515 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002516
Douglas Gregore7526412009-11-11 19:31:23 +00002517 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002518 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002519 "Declaration argument with template template parameter");
2520 break;
2521 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002522 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002523 "Integral argument with template template parameter");
2524 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002525
Douglas Gregore7526412009-11-11 19:31:23 +00002526 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002527 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002528 break;
2529 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002530
Douglas Gregore7526412009-11-11 19:31:23 +00002531 return false;
2532}
2533
Douglas Gregorc15cb382009-02-09 23:23:08 +00002534/// \brief Check that the given template argument list is well-formed
2535/// for specializing the given template.
2536bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2537 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00002538 const TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002539 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002540 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002541 TemplateParameterList *Params = Template->getTemplateParameters();
2542 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002543 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002544 bool Invalid = false;
2545
John McCalld5532b62009-11-23 01:53:49 +00002546 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2547
Mike Stump1eb44332009-09-09 15:08:12 +00002548 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002549 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002550
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002551 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002552 (NumArgs < Params->getMinRequiredArguments() &&
2553 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002554 // FIXME: point at either the first arg beyond what we can handle,
2555 // or the '>', depending on whether we have too many or too few
2556 // arguments.
2557 SourceRange Range;
2558 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002559 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002560 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2561 << (NumArgs > NumParams)
2562 << (isa<ClassTemplateDecl>(Template)? 0 :
2563 isa<FunctionTemplateDecl>(Template)? 1 :
2564 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2565 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002566 Diag(Template->getLocation(), diag::note_template_decl_here)
2567 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002568 Invalid = true;
2569 }
Mike Stump1eb44332009-09-09 15:08:12 +00002570
2571 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002572 // [...] The type and form of each template-argument specified in
2573 // a template-id shall match the type and form specified for the
2574 // corresponding parameter declared by the template in its
2575 // template-parameter-list.
Douglas Gregor14be16b2010-12-20 16:57:52 +00002576 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2577 TemplateParameterList::iterator Param = Params->begin(),
2578 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002579 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002580 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002581 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002582 if (ArgIdx > NumArgs && PartialTemplateArgs)
2583 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Douglas Gregorf35f8282009-11-11 21:54:23 +00002585 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002586 // If we have an expanded parameter pack, make sure we don't have too
2587 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002588 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002589 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002590 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002591 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2592 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2593 << true
2594 << (isa<ClassTemplateDecl>(Template)? 0 :
2595 isa<FunctionTemplateDecl>(Template)? 1 :
2596 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2597 << Template;
2598 Diag(Template->getLocation(), diag::note_template_decl_here)
2599 << Params->getSourceRange();
2600 return true;
2601 }
2602 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002603
Douglas Gregorf35f8282009-11-11 21:54:23 +00002604 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002605 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2606 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002607 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002608 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002609
Douglas Gregor14be16b2010-12-20 16:57:52 +00002610 if ((*Param)->isTemplateParameterPack()) {
2611 // The template parameter was a template parameter pack, so take the
2612 // deduced argument and place it on the argument pack. Note that we
2613 // stay on the same template parameter so that we can deduce more
2614 // arguments.
2615 ArgumentPack.push_back(Converted.back());
2616 Converted.pop_back();
2617 } else {
2618 // Move to the next template parameter.
2619 ++Param;
2620 }
2621 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002622 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002623 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002624
2625 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002626 // arguments, just break out now and we'll fill in the argument pack below.
2627 if ((*Param)->isTemplateParameterPack())
2628 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002629
Douglas Gregorf35f8282009-11-11 21:54:23 +00002630 // We have a default template argument that we will use.
2631 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002632
Douglas Gregorf35f8282009-11-11 21:54:23 +00002633 // Retrieve the default template argument from the template
2634 // parameter. For each kind of template parameter, we substitute the
2635 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002636 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002637 // the default argument.
2638 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2639 if (!TTP->hasDefaultArgument()) {
2640 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2641 break;
2642 }
2643
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002644 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002645 Template,
2646 TemplateLoc,
2647 RAngleLoc,
2648 TTP,
2649 Converted);
2650 if (!ArgType)
2651 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002652
Douglas Gregorf35f8282009-11-11 21:54:23 +00002653 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2654 ArgType);
2655 } else if (NonTypeTemplateParmDecl *NTTP
2656 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2657 if (!NTTP->hasDefaultArgument()) {
2658 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2659 break;
2660 }
2661
John McCall60d7b3a2010-08-24 06:29:42 +00002662 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002663 TemplateLoc,
2664 RAngleLoc,
2665 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002666 Converted);
2667 if (E.isInvalid())
2668 return true;
2669
2670 Expr *Ex = E.takeAs<Expr>();
2671 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2672 } else {
2673 TemplateTemplateParmDecl *TempParm
2674 = cast<TemplateTemplateParmDecl>(*Param);
2675
2676 if (!TempParm->hasDefaultArgument()) {
2677 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2678 break;
2679 }
2680
Douglas Gregor1d752d72011-03-02 18:46:51 +00002681 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002682 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002683 TemplateLoc,
2684 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002685 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002686 Converted,
2687 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002688 if (Name.isNull())
2689 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002690
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002691 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2692 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002693 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002694
Douglas Gregorf35f8282009-11-11 21:54:23 +00002695 // Introduce an instantiation record that describes where we are using
2696 // the default template argument.
2697 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002698 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002699 SourceRange(TemplateLoc, RAngleLoc));
2700
Douglas Gregorf35f8282009-11-11 21:54:23 +00002701 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002702 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002703 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002704 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002705
Douglas Gregor14be16b2010-12-20 16:57:52 +00002706 // Move to the next template parameter and argument.
2707 ++Param;
2708 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002709 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002710
Douglas Gregor14be16b2010-12-20 16:57:52 +00002711 // Form argument packs for each of the parameter packs remaining.
2712 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002713 // If we're checking a partial list of template arguments, don't fill
2714 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002715
2716 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002717 if (PartialTemplateArgs && ArgumentPack.empty()) {
2718 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002719 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002720 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002721 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002722 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2723 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002724 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002725 ArgumentPack.clear();
2726 }
2727 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002728
Douglas Gregor14be16b2010-12-20 16:57:52 +00002729 ++Param;
2730 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002731
Douglas Gregorc15cb382009-02-09 23:23:08 +00002732 return Invalid;
2733}
2734
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002735namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002736 class UnnamedLocalNoLinkageFinder
2737 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002738 {
2739 Sema &S;
2740 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002741
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002742 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002743
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002744 public:
2745 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2746
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002747 bool Visit(QualType T) {
2748 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002749 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002750
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002751#define TYPE(Class, Parent) \
2752 bool Visit##Class##Type(const Class##Type *);
2753#define ABSTRACT_TYPE(Class, Parent) \
2754 bool Visit##Class##Type(const Class##Type *) { return false; }
2755#define NON_CANONICAL_TYPE(Class, Parent) \
2756 bool Visit##Class##Type(const Class##Type *) { return false; }
2757#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002758
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002759 bool VisitTagDecl(const TagDecl *Tag);
2760 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2761 };
2762}
2763
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002764bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002765 return false;
2766}
2767
2768bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2769 return Visit(T->getElementType());
2770}
2771
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002772bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002773 return Visit(T->getPointeeType());
2774}
2775
2776bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002777 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002778 return Visit(T->getPointeeType());
2779}
2780
2781bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002782 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002783 return Visit(T->getPointeeType());
2784}
2785
2786bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002787 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002788 return Visit(T->getPointeeType());
2789}
2790
2791bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002792 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002793 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2794}
2795
2796bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002797 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002798 return Visit(T->getElementType());
2799}
2800
2801bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002802 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002803 return Visit(T->getElementType());
2804}
2805
2806bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002807 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002808 return Visit(T->getElementType());
2809}
2810
2811bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002812 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002813 return Visit(T->getElementType());
2814}
2815
2816bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002817 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002818 return Visit(T->getElementType());
2819}
2820
2821bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2822 return Visit(T->getElementType());
2823}
2824
2825bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2826 return Visit(T->getElementType());
2827}
2828
2829bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2830 const FunctionProtoType* T) {
2831 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002832 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002833 A != AEnd; ++A) {
2834 if (Visit(*A))
2835 return true;
2836 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002837
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002838 return Visit(T->getResultType());
2839}
2840
2841bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2842 const FunctionNoProtoType* T) {
2843 return Visit(T->getResultType());
2844}
2845
2846bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2847 const UnresolvedUsingType*) {
2848 return false;
2849}
2850
2851bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2852 return false;
2853}
2854
2855bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2856 return Visit(T->getUnderlyingType());
2857}
2858
2859bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2860 return false;
2861}
2862
Richard Smith34b41d92011-02-20 03:19:35 +00002863bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2864 return Visit(T->getDeducedType());
2865}
2866
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002867bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2868 return VisitTagDecl(T->getDecl());
2869}
2870
2871bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2872 return VisitTagDecl(T->getDecl());
2873}
2874
2875bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2876 const TemplateTypeParmType*) {
2877 return false;
2878}
2879
Douglas Gregorc3069d62011-01-14 02:55:32 +00002880bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2881 const SubstTemplateTypeParmPackType *) {
2882 return false;
2883}
2884
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002885bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2886 const TemplateSpecializationType*) {
2887 return false;
2888}
2889
2890bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2891 const InjectedClassNameType* T) {
2892 return VisitTagDecl(T->getDecl());
2893}
2894
2895bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2896 const DependentNameType* T) {
2897 return VisitNestedNameSpecifier(T->getQualifier());
2898}
2899
2900bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2901 const DependentTemplateSpecializationType* T) {
2902 return VisitNestedNameSpecifier(T->getQualifier());
2903}
2904
Douglas Gregor7536dd52010-12-20 02:24:11 +00002905bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2906 const PackExpansionType* T) {
2907 return Visit(T->getPattern());
2908}
2909
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002910bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2911 return false;
2912}
2913
2914bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2915 const ObjCInterfaceType *) {
2916 return false;
2917}
2918
2919bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2920 const ObjCObjectPointerType *) {
2921 return false;
2922}
2923
2924bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2925 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2926 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2927 << S.Context.getTypeDeclType(Tag) << SR;
2928 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002929 }
2930
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002931 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2932 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2933 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2934 return true;
2935 }
2936
2937 return false;
2938}
2939
2940bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2941 NestedNameSpecifier *NNS) {
2942 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2943 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002944
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002945 switch (NNS->getKind()) {
2946 case NestedNameSpecifier::Identifier:
2947 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002948 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002949 case NestedNameSpecifier::Global:
2950 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002951
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002952 case NestedNameSpecifier::TypeSpec:
2953 case NestedNameSpecifier::TypeSpecWithTemplate:
2954 return Visit(QualType(NNS->getAsType(), 0));
2955 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002956 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002957}
2958
2959
Douglas Gregorc15cb382009-02-09 23:23:08 +00002960/// \brief Check a template argument against its corresponding
2961/// template type parameter.
2962///
2963/// This routine implements the semantics of C++ [temp.arg.type]. It
2964/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002965bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002966 TypeSourceInfo *ArgInfo) {
2967 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002968 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00002969 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00002970
2971 if (Arg->isVariablyModifiedType()) {
2972 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002973 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002974 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002975 }
2976
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002977 // C++03 [temp.arg.type]p2:
2978 // A local type, a type with no linkage, an unnamed type or a type
2979 // compounded from any of these types shall not be used as a
2980 // template-argument for a template type-parameter.
2981 //
2982 // C++0x allows these, and even in C++03 we allow them as an extension with
2983 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00002984 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002985 UnnamedLocalNoLinkageFinder Finder(*this, SR);
2986 (void)Finder.Visit(Context.getCanonicalType(Arg));
2987 }
2988
Douglas Gregorc15cb382009-02-09 23:23:08 +00002989 return false;
2990}
2991
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002992/// \brief Checks whether the given template argument is the address
2993/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002994static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00002995CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
2996 NonTypeTemplateParmDecl *Param,
2997 QualType ParamType,
2998 Expr *ArgIn,
2999 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003000 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003001 Expr *Arg = ArgIn;
3002 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003003
3004 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003005 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003006 Arg = Cast->getSubExpr();
3007
3008 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003009 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003010 // A template-argument for a non-type, non-template
3011 // template-parameter shall be one of: [...]
3012 //
3013 // -- the address of an object or function with external
3014 // linkage, including function templates and function
3015 // template-ids but excluding non-static class members,
3016 // expressed as & id-expression where the & is optional if
3017 // the name refers to a function or array, or if the
3018 // corresponding template-parameter is a reference; or
3019 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003020
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003021 // In C++98/03 mode, give an extension warning on any extra parentheses.
3022 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3023 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003024 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003025 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003026 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003027 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003028 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003029 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003030 }
3031
3032 Arg = Parens->getSubExpr();
3033 }
3034
Douglas Gregorb7a09262010-04-01 18:32:35 +00003035 bool AddressTaken = false;
3036 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003037 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003038 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003039 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003040 AddressTaken = true;
3041 AddrOpLoc = UnOp->getOperatorLoc();
3042 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003043 } else
3044 DRE = dyn_cast<DeclRefExpr>(Arg);
3045
Douglas Gregorb7a09262010-04-01 18:32:35 +00003046 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003047 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3048 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003049 S.Diag(Param->getLocation(), diag::note_template_param_here);
3050 return true;
3051 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003052
3053 // Stop checking the precise nature of the argument if it is value dependent,
3054 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003055 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003056 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003057 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003058 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003059
Douglas Gregorb7a09262010-04-01 18:32:35 +00003060 if (!isa<ValueDecl>(DRE->getDecl())) {
3061 S.Diag(Arg->getSourceRange().getBegin(),
3062 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003063 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003064 S.Diag(Param->getLocation(), diag::note_template_param_here);
3065 return true;
3066 }
3067
3068 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003069
3070 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003071 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3072 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003073 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003074 S.Diag(Param->getLocation(), diag::note_template_param_here);
3075 return true;
3076 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003077
3078 // Cannot refer to non-static member functions
3079 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003080 if (!Method->isStatic()) {
3081 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003082 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003083 S.Diag(Param->getLocation(), diag::note_template_param_here);
3084 return true;
3085 }
Mike Stump1eb44332009-09-09 15:08:12 +00003086
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003087 // Functions must have external linkage.
3088 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003089 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003090 S.Diag(Arg->getSourceRange().getBegin(),
3091 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003092 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003093 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003094 << true;
3095 return true;
3096 }
3097
3098 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003099 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003100
Douglas Gregorb7a09262010-04-01 18:32:35 +00003101 // If the template parameter has pointer type, the function decays.
3102 if (ParamType->isPointerType() && !AddressTaken)
3103 ArgType = S.Context.getPointerType(Func->getType());
3104 else if (AddressTaken && ParamType->isReferenceType()) {
3105 // If we originally had an address-of operator, but the
3106 // parameter has reference type, complain and (if things look
3107 // like they will work) drop the address-of operator.
3108 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3109 ParamType.getNonReferenceType())) {
3110 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3111 << ParamType;
3112 S.Diag(Param->getLocation(), diag::note_template_param_here);
3113 return true;
3114 }
3115
3116 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3117 << ParamType
3118 << FixItHint::CreateRemoval(AddrOpLoc);
3119 S.Diag(Param->getLocation(), diag::note_template_param_here);
3120
3121 ArgType = Func->getType();
3122 }
3123 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003124 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003125 S.Diag(Arg->getSourceRange().getBegin(),
3126 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003127 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003128 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003129 << true;
3130 return true;
3131 }
3132
Douglas Gregorb7a09262010-04-01 18:32:35 +00003133 // A value of reference type is not an object.
3134 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003135 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003136 diag::err_template_arg_reference_var)
3137 << Var->getType() << Arg->getSourceRange();
3138 S.Diag(Param->getLocation(), diag::note_template_param_here);
3139 return true;
3140 }
3141
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003142 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003143 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003144
3145 // If the template parameter has pointer type, we must have taken
3146 // the address of this object.
3147 if (ParamType->isReferenceType()) {
3148 if (AddressTaken) {
3149 // If we originally had an address-of operator, but the
3150 // parameter has reference type, complain and (if things look
3151 // like they will work) drop the address-of operator.
3152 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3153 ParamType.getNonReferenceType())) {
3154 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3155 << ParamType;
3156 S.Diag(Param->getLocation(), diag::note_template_param_here);
3157 return true;
3158 }
3159
3160 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3161 << ParamType
3162 << FixItHint::CreateRemoval(AddrOpLoc);
3163 S.Diag(Param->getLocation(), diag::note_template_param_here);
3164
3165 ArgType = Var->getType();
3166 }
3167 } else if (!AddressTaken && ParamType->isPointerType()) {
3168 if (Var->getType()->isArrayType()) {
3169 // Array-to-pointer decay.
3170 ArgType = S.Context.getArrayDecayedType(Var->getType());
3171 } else {
3172 // If the template parameter has pointer type but the address of
3173 // this object was not taken, complain and (possibly) recover by
3174 // taking the address of the entity.
3175 ArgType = S.Context.getPointerType(Var->getType());
3176 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3177 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3178 << ParamType;
3179 S.Diag(Param->getLocation(), diag::note_template_param_here);
3180 return true;
3181 }
3182
3183 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3184 << ParamType
3185 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3186
3187 S.Diag(Param->getLocation(), diag::note_template_param_here);
3188 }
3189 }
3190 } else {
3191 // We found something else, but we don't know specifically what it is.
3192 S.Diag(Arg->getSourceRange().getBegin(),
3193 diag::err_template_arg_not_object_or_func)
3194 << Arg->getSourceRange();
3195 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3196 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003197 }
Mike Stump1eb44332009-09-09 15:08:12 +00003198
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003199 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003200 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003201 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003202 // For pointer-to-object types, qualification conversions are
3203 // permitted.
3204 } else {
3205 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3206 if (!ParamRef->getPointeeType()->isFunctionType()) {
3207 // C++ [temp.arg.nontype]p5b3:
3208 // For a non-type template-parameter of type reference to
3209 // object, no conversions apply. The type referred to by the
3210 // reference may be more cv-qualified than the (otherwise
3211 // identical) type of the template- argument. The
3212 // template-parameter is bound directly to the
3213 // template-argument, which shall be an lvalue.
3214
3215 // FIXME: Other qualifiers?
3216 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3217 unsigned ArgQuals = ArgType.getCVRQualifiers();
3218
3219 if ((ParamQuals | ArgQuals) != ParamQuals) {
3220 S.Diag(Arg->getSourceRange().getBegin(),
3221 diag::err_template_arg_ref_bind_ignores_quals)
3222 << ParamType << Arg->getType()
3223 << Arg->getSourceRange();
3224 S.Diag(Param->getLocation(), diag::note_template_param_here);
3225 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003226 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003227 }
3228 }
3229
3230 // At this point, the template argument refers to an object or
3231 // function with external linkage. We now need to check whether the
3232 // argument and parameter types are compatible.
3233 if (!S.Context.hasSameUnqualifiedType(ArgType,
3234 ParamType.getNonReferenceType())) {
3235 // We can't perform this conversion or binding.
3236 if (ParamType->isReferenceType())
3237 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3238 << ParamType << Arg->getType() << Arg->getSourceRange();
3239 else
3240 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3241 << Arg->getType() << ParamType << Arg->getSourceRange();
3242 S.Diag(Param->getLocation(), diag::note_template_param_here);
3243 return true;
3244 }
3245 }
3246
3247 // Create the template argument.
3248 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003249 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003250 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003251}
3252
3253/// \brief Checks whether the given template argument is a pointer to
3254/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003255bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003256 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003257 bool Invalid = false;
3258
3259 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003260 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003261 Arg = Cast->getSubExpr();
3262
3263 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003264 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003265 // A template-argument for a non-type, non-template
3266 // template-parameter shall be one of: [...]
3267 //
3268 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003269 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003270
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003271 // In C++98/03 mode, give an extension warning on any extra parentheses.
3272 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3273 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003274 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003275 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003276 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003277 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003278 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003279 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003280 }
3281
3282 Arg = Parens->getSubExpr();
3283 }
3284
Douglas Gregorcaddba02009-11-12 18:38:13 +00003285 // A pointer-to-member constant written &Class::member.
3286 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003287 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003288 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3289 if (DRE && !DRE->getQualifier())
3290 DRE = 0;
3291 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003292 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003293 // A constant of pointer-to-member type.
3294 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3295 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3296 if (VD->getType()->isMemberPointerType()) {
3297 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003298 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003299 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3300 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003301 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003302 else
3303 Converted = TemplateArgument(VD->getCanonicalDecl());
3304 return Invalid;
3305 }
3306 }
3307 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003308
Douglas Gregorcaddba02009-11-12 18:38:13 +00003309 DRE = 0;
3310 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003311
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003312 if (!DRE)
3313 return Diag(Arg->getSourceRange().getBegin(),
3314 diag::err_template_arg_not_pointer_to_member_form)
3315 << Arg->getSourceRange();
3316
3317 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3318 assert((isa<FieldDecl>(DRE->getDecl()) ||
3319 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3320 "Only non-static member pointers can make it here");
3321
3322 // Okay: this is the address of a non-static member, and therefore
3323 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003324 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003325 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003326 else
3327 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003328 return Invalid;
3329 }
3330
3331 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003332 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003333 diag::err_template_arg_not_pointer_to_member_form)
3334 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003335 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003336 diag::note_template_arg_refers_here);
3337 return true;
3338}
3339
Douglas Gregorc15cb382009-02-09 23:23:08 +00003340/// \brief Check a template argument against its corresponding
3341/// non-type template parameter.
3342///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003343/// This routine implements the semantics of C++ [temp.arg.nontype].
3344/// It returns true if an error occurred, and false otherwise. \p
3345/// InstantiatedParamType is the type of the non-type template
3346/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003347///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003348/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003349bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003350 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003351 TemplateArgument &Converted,
3352 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003353 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3354
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003355 // If either the parameter has a dependent type or the argument is
3356 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003357 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3358 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003359 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003360 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003361 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003362
3363 // C++ [temp.arg.nontype]p5:
3364 // The following conversions are performed on each expression used
3365 // as a non-type template-argument. If a non-type
3366 // template-argument cannot be converted to the type of the
3367 // corresponding template-parameter then the program is
3368 // ill-formed.
3369 //
3370 // -- for a non-type template-parameter of integral or
3371 // enumeration type, integral promotions (4.5) and integral
3372 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003373 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003374 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003375 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003376 // C++ [temp.arg.nontype]p1:
3377 // A template-argument for a non-type, non-template
3378 // template-parameter shall be one of:
3379 //
3380 // -- an integral constant-expression of integral or enumeration
3381 // type; or
3382 // -- the name of a non-type template-parameter; or
3383 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003384 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003385 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003386 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003387 diag::err_template_arg_not_integral_or_enumeral)
3388 << ArgType << Arg->getSourceRange();
3389 Diag(Param->getLocation(), diag::note_template_param_here);
3390 return true;
3391 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003392 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003393 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3394 << ArgType << Arg->getSourceRange();
3395 return true;
3396 }
3397
Douglas Gregor02024a92010-03-28 02:42:43 +00003398 // From here on out, all we care about are the unqualified forms
3399 // of the parameter and argument types.
3400 ParamType = ParamType.getUnqualifiedType();
3401 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003402
3403 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003404 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003405 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003406 } else if (CTAK == CTAK_Deduced) {
3407 // C++ [temp.deduct.type]p17:
3408 // If, in the declaration of a function template with a non-type
3409 // template-parameter, the non-type template- parameter is used
3410 // in an expression in the function parameter-list and, if the
3411 // corresponding template-argument is deduced, the
3412 // template-argument type shall match the type of the
3413 // template-parameter exactly, except that a template-argument
3414 // deduced from an array bound may be of any integral type.
3415 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3416 << ArgType << ParamType;
3417 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003418 return true;
3419 } else if (ParamType->isBooleanType()) {
3420 // This is an integral-to-boolean conversion.
3421 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003422 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3423 !ParamType->isEnumeralType()) {
3424 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003425 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003426 } else {
3427 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003428 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003429 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003430 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003431 Diag(Param->getLocation(), diag::note_template_param_here);
3432 return true;
3433 }
3434
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003435 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003436 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003437 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003438
3439 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003440 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003441
3442 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003443 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003444 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003445 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003446 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003447 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003448
3449 // Complain if an unsigned parameter received a negative value.
3450 if (IntegerType->isUnsignedIntegerType()
3451 && (OldValue.isSigned() && OldValue.isNegative())) {
3452 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3453 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3454 << Arg->getSourceRange();
3455 Diag(Param->getLocation(), diag::note_template_param_here);
3456 }
3457
3458 // Complain if we overflowed the template parameter's type.
3459 unsigned RequiredBits;
3460 if (IntegerType->isUnsignedIntegerType())
3461 RequiredBits = OldValue.getActiveBits();
3462 else if (OldValue.isUnsigned())
3463 RequiredBits = OldValue.getActiveBits() + 1;
3464 else
3465 RequiredBits = OldValue.getMinSignedBits();
3466 if (RequiredBits > AllowedBits) {
3467 Diag(Arg->getSourceRange().getBegin(),
3468 diag::warn_template_arg_too_large)
3469 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3470 << Arg->getSourceRange();
3471 Diag(Param->getLocation(), diag::note_template_param_here);
3472 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003473 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003474
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003475 // Add the value of this argument to the list of converted
3476 // arguments. We use the bitwidth and signedness of the template
3477 // parameter.
3478 if (Arg->isValueDependent()) {
3479 // The argument is value-dependent. Create a new
3480 // TemplateArgument with the converted expression.
3481 Converted = TemplateArgument(Arg);
3482 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003483 }
3484
John McCall833ca992009-10-29 08:12:44 +00003485 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003486 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003487 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003488 return false;
3489 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003490
John McCall6bb80172010-03-30 21:47:33 +00003491 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3492
Douglas Gregorb7a09262010-04-01 18:32:35 +00003493 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3494 // from a template argument of type std::nullptr_t to a non-type
3495 // template parameter of type pointer to object, pointer to
3496 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003497 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003498 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3499 Converted = TemplateArgument((NamedDecl *)0);
3500 return false;
3501 }
3502
Douglas Gregorb86b0572009-02-11 01:18:59 +00003503 // Handle pointer-to-function, reference-to-function, and
3504 // pointer-to-member-function all in (roughly) the same way.
3505 if (// -- For a non-type template-parameter of type pointer to
3506 // function, only the function-to-pointer conversion (4.3) is
3507 // applied. If the template-argument represents a set of
3508 // overloaded functions (or a pointer to such), the matching
3509 // function is selected from the set (13.4).
3510 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003511 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003512 // -- For a non-type template-parameter of type reference to
3513 // function, no conversions apply. If the template-argument
3514 // represents a set of overloaded functions, the matching
3515 // function is selected from the set (13.4).
3516 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003517 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003518 // -- For a non-type template-parameter of type pointer to
3519 // member function, no conversions apply. If the
3520 // template-argument represents a set of overloaded member
3521 // functions, the matching member function is selected from
3522 // the set (13.4).
3523 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003524 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003525 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003526
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003527 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003528 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003529 true,
3530 FoundResult)) {
3531 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3532 return true;
3533
3534 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3535 ArgType = Arg->getType();
3536 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003537 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003538 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003539
Douglas Gregorb7a09262010-04-01 18:32:35 +00003540 if (!ParamType->isMemberPointerType())
3541 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003542 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003543 Arg, Converted);
3544
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003545 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3546 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003547 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003548 } else if (!Context.hasSameUnqualifiedType(ArgType,
3549 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003550 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003551 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003552 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003553 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003554 Diag(Param->getLocation(), diag::note_template_param_here);
3555 return true;
3556 }
Mike Stump1eb44332009-09-09 15:08:12 +00003557
Douglas Gregorb7a09262010-04-01 18:32:35 +00003558 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003559 }
3560
Chris Lattnerfe90de72009-02-20 21:37:53 +00003561 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003562 // -- for a non-type template-parameter of type pointer to
3563 // object, qualification conversions (4.4) and the
3564 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003565 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003566 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003567 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003568
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003569 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003570 ParamType,
3571 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003572 }
Mike Stump1eb44332009-09-09 15:08:12 +00003573
Ted Kremenek6217b802009-07-29 21:53:49 +00003574 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003575 // -- For a non-type template-parameter of type reference to
3576 // object, no conversions apply. The type referred to by the
3577 // reference may be more cv-qualified than the (otherwise
3578 // identical) type of the template-argument. The
3579 // template-parameter is bound directly to the
3580 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003581 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003582 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003583
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003584 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003585 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3586 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003587 true,
3588 FoundResult)) {
3589 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3590 return true;
3591
3592 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3593 ArgType = Arg->getType();
3594 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003595 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003596 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003597
3598 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003599 ParamType,
3600 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003601 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003602
3603 // -- For a non-type template-parameter of type pointer to data
3604 // member, qualification conversions (4.4) are applied.
3605 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3606
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003607 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003608 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003609 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003610 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003611 } else {
3612 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003613 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003614 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003615 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003616 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003617 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003618 }
3619
Douglas Gregorcaddba02009-11-12 18:38:13 +00003620 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003621}
3622
3623/// \brief Check a template argument against its corresponding
3624/// template template parameter.
3625///
3626/// This routine implements the semantics of C++ [temp.arg.template].
3627/// It returns true if an error occurred, and false otherwise.
3628bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003629 const TemplateArgumentLoc &Arg) {
3630 TemplateName Name = Arg.getArgument().getAsTemplate();
3631 TemplateDecl *Template = Name.getAsTemplateDecl();
3632 if (!Template) {
3633 // Any dependent template name is fine.
3634 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3635 return false;
3636 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003637
3638 // C++ [temp.arg.template]p1:
3639 // A template-argument for a template template-parameter shall be
3640 // the name of a class template, expressed as id-expression. Only
3641 // primary class templates are considered when matching the
3642 // template template argument with the corresponding parameter;
3643 // partial specializations are not considered even if their
3644 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003645 //
3646 // Note that we also allow template template parameters here, which
3647 // will happen when we are dealing with, e.g., class template
3648 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003649 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003650 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003651 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003652 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003653 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003654 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003655 << Template;
3656 }
3657
3658 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3659 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003660 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003661 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003662 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003663}
3664
Douglas Gregor02024a92010-03-28 02:42:43 +00003665/// \brief Given a non-type template argument that refers to a
3666/// declaration and the type of its corresponding non-type template
3667/// parameter, produce an expression that properly refers to that
3668/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003669ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003670Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3671 QualType ParamType,
3672 SourceLocation Loc) {
3673 assert(Arg.getKind() == TemplateArgument::Declaration &&
3674 "Only declaration template arguments permitted here");
3675 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3676
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003677 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003678 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3679 // If the value is a class member, we might have a pointer-to-member.
3680 // Determine whether the non-type template template parameter is of
3681 // pointer-to-member type. If so, we need to build an appropriate
3682 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3683 // would refer to the member itself.
3684 if (ParamType->isMemberPointerType()) {
3685 QualType ClassType
3686 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3687 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003688 = NestedNameSpecifier::Create(Context, 0, false,
3689 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003690 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003691 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003692
3693 // The actual value-ness of this is unimportant, but for
3694 // internal consistency's sake, references to instance methods
3695 // are r-values.
3696 ExprValueKind VK = VK_LValue;
3697 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3698 VK = VK_RValue;
3699
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003700 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003701 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003702 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003703 Loc,
3704 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003705 if (RefExpr.isInvalid())
3706 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003707
John McCall2de56d12010-08-25 11:45:40 +00003708 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003709
Douglas Gregorc0c83002010-04-30 21:46:38 +00003710 // We might need to perform a trailing qualification conversion, since
3711 // the element type on the parameter could be more qualified than the
3712 // element type in the expression we constructed.
3713 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003714 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003715 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003716 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003717 RefExpr = Owned(RefE);
3718 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003719
Douglas Gregor02024a92010-03-28 02:42:43 +00003720 assert(!RefExpr.isInvalid() &&
3721 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003722 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003723 return move(RefExpr);
3724 }
3725 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003726
Douglas Gregor02024a92010-03-28 02:42:43 +00003727 QualType T = VD->getType().getNonReferenceType();
3728 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003729 // When the non-type template parameter is a pointer, take the
3730 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003731 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003732 if (RefExpr.isInvalid())
3733 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003734
3735 if (T->isFunctionType() || T->isArrayType()) {
3736 // Decay functions and arrays.
3737 Expr *RefE = (Expr *)RefExpr.get();
3738 DefaultFunctionArrayConversion(RefE);
3739 if (RefE != RefExpr.get()) {
3740 RefExpr.release();
3741 RefExpr = Owned(RefE);
3742 }
3743
3744 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003745 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003746
Douglas Gregorb7a09262010-04-01 18:32:35 +00003747 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003748 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003749 }
3750
John McCallf89e55a2010-11-18 06:31:45 +00003751 ExprValueKind VK = VK_RValue;
3752
Douglas Gregor02024a92010-03-28 02:42:43 +00003753 // If the non-type template parameter has reference type, qualify the
3754 // resulting declaration reference with the extra qualifiers on the
3755 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003756 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3757 VK = VK_LValue;
3758 T = Context.getQualifiedType(T,
3759 TargetRef->getPointeeType().getQualifiers());
3760 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003761
John McCallf89e55a2010-11-18 06:31:45 +00003762 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003763}
3764
3765/// \brief Construct a new expression that refers to the given
3766/// integral template argument with the given source-location
3767/// information.
3768///
3769/// This routine takes care of the mapping from an integral template
3770/// argument (which may have any integral type) to the appropriate
3771/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003772ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003773Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3774 SourceLocation Loc) {
3775 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003776 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003777 QualType T = Arg.getIntegralType();
3778 if (T->isCharType() || T->isWideCharType())
3779 return Owned(new (Context) CharacterLiteral(
3780 Arg.getAsIntegral()->getZExtValue(),
3781 T->isWideCharType(),
3782 T,
3783 Loc));
3784 if (T->isBooleanType())
3785 return Owned(new (Context) CXXBoolLiteralExpr(
3786 Arg.getAsIntegral()->getBoolValue(),
3787 T,
3788 Loc));
3789
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003790 QualType BT;
3791 if (const EnumType *ET = T->getAs<EnumType>())
3792 BT = ET->getDecl()->getPromotionType();
3793 else
3794 BT = T;
3795
3796 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003797 if (T->isEnumeralType()) {
3798 // FIXME: This is a hack. We need a better way to handle substituted
3799 // non-type template parameters.
3800 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3801 E, 0,
3802 Context.getTrivialTypeSourceInfo(T, Loc),
3803 Loc, Loc);
3804 }
3805
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003806 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003807}
3808
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003809/// \brief Match two template parameters within template parameter lists.
3810static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3811 bool Complain,
3812 Sema::TemplateParameterListEqualKind Kind,
3813 SourceLocation TemplateArgLoc) {
3814 // Check the actual kind (type, non-type, template).
3815 if (Old->getKind() != New->getKind()) {
3816 if (Complain) {
3817 unsigned NextDiag = diag::err_template_param_different_kind;
3818 if (TemplateArgLoc.isValid()) {
3819 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3820 NextDiag = diag::note_template_param_different_kind;
3821 }
3822 S.Diag(New->getLocation(), NextDiag)
3823 << (Kind != Sema::TPL_TemplateMatch);
3824 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3825 << (Kind != Sema::TPL_TemplateMatch);
3826 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003827
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003828 return false;
3829 }
3830
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003831 // Check that both are parameter packs are neither are parameter packs.
3832 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003833 // template template parameter, the template template parameter can have
3834 // a parameter pack where the template template argument does not.
3835 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3836 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3837 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003838 if (Complain) {
3839 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3840 if (TemplateArgLoc.isValid()) {
3841 S.Diag(TemplateArgLoc,
3842 diag::err_template_arg_template_params_mismatch);
3843 NextDiag = diag::note_template_parameter_pack_non_pack;
3844 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003845
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003846 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3847 : isa<NonTypeTemplateParmDecl>(New)? 1
3848 : 2;
3849 S.Diag(New->getLocation(), NextDiag)
3850 << ParamKind << New->isParameterPack();
3851 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3852 << ParamKind << Old->isParameterPack();
3853 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003854
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003855 return false;
3856 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003857
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003858 // For non-type template parameters, check the type of the parameter.
3859 if (NonTypeTemplateParmDecl *OldNTTP
3860 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3861 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003862
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003863 // If we are matching a template template argument to a template
3864 // template parameter and one of the non-type template parameter types
3865 // is dependent, then we must wait until template instantiation time
3866 // to actually compare the arguments.
3867 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3868 (OldNTTP->getType()->isDependentType() ||
3869 NewNTTP->getType()->isDependentType()))
3870 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003871
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003872 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3873 if (Complain) {
3874 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3875 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003876 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003877 diag::err_template_arg_template_params_mismatch);
3878 NextDiag = diag::note_template_nontype_parm_different_type;
3879 }
3880 S.Diag(NewNTTP->getLocation(), NextDiag)
3881 << NewNTTP->getType()
3882 << (Kind != Sema::TPL_TemplateMatch);
3883 S.Diag(OldNTTP->getLocation(),
3884 diag::note_template_nontype_parm_prev_declaration)
3885 << OldNTTP->getType();
3886 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003887
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003888 return false;
3889 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003890
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003891 return true;
3892 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003893
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003894 // For template template parameters, check the template parameter types.
3895 // The template parameter lists of template template
3896 // parameters must agree.
3897 if (TemplateTemplateParmDecl *OldTTP
3898 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003899 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003900 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3901 OldTTP->getTemplateParameters(),
3902 Complain,
3903 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003904 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003905 : Kind),
3906 TemplateArgLoc);
3907 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003908
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003909 return true;
3910}
Douglas Gregor02024a92010-03-28 02:42:43 +00003911
Douglas Gregora0347822011-01-13 00:08:50 +00003912/// \brief Diagnose a known arity mismatch when comparing template argument
3913/// lists.
3914static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003915void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003916 TemplateParameterList *New,
3917 TemplateParameterList *Old,
3918 Sema::TemplateParameterListEqualKind Kind,
3919 SourceLocation TemplateArgLoc) {
3920 unsigned NextDiag = diag::err_template_param_list_different_arity;
3921 if (TemplateArgLoc.isValid()) {
3922 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3923 NextDiag = diag::note_template_param_list_different_arity;
3924 }
3925 S.Diag(New->getTemplateLoc(), NextDiag)
3926 << (New->size() > Old->size())
3927 << (Kind != Sema::TPL_TemplateMatch)
3928 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3929 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3930 << (Kind != Sema::TPL_TemplateMatch)
3931 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3932}
3933
Douglas Gregorddc29e12009-02-06 22:42:48 +00003934/// \brief Determine whether the given template parameter lists are
3935/// equivalent.
3936///
Mike Stump1eb44332009-09-09 15:08:12 +00003937/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003938/// source code as part of a new template declaration.
3939///
3940/// \param Old The old template parameter list, typically found via
3941/// name lookup of the template declared with this template parameter
3942/// list.
3943///
3944/// \param Complain If true, this routine will produce a diagnostic if
3945/// the template parameter lists are not equivalent.
3946///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003947/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003948///
3949/// \param TemplateArgLoc If this source location is valid, then we
3950/// are actually checking the template parameter list of a template
3951/// argument (New) against the template parameter list of its
3952/// corresponding template template parameter (Old). We produce
3953/// slightly different diagnostics in this scenario.
3954///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003955/// \returns True if the template parameter lists are equal, false
3956/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003957bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003958Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3959 TemplateParameterList *Old,
3960 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003961 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00003962 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00003963 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
3964 if (Complain)
3965 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3966 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00003967
3968 return false;
3969 }
3970
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003971 // C++0x [temp.arg.template]p3:
3972 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003973 // when each of the template parameters in the template-parameter-list of
3974 // the template-argument's corresponding class template or template alias
3975 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00003976 // template-parameter-list of P. [...]
3977 TemplateParameterList::iterator NewParm = New->begin();
3978 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003979 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00003980 OldParmEnd = Old->end();
3981 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00003982 if (Kind != TPL_TemplateTemplateArgumentMatch ||
3983 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00003984 if (NewParm == NewParmEnd) {
3985 if (Complain)
3986 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3987 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003988
Douglas Gregora0347822011-01-13 00:08:50 +00003989 return false;
3990 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003991
Douglas Gregora0347822011-01-13 00:08:50 +00003992 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3993 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003994 return false;
3995
Douglas Gregora0347822011-01-13 00:08:50 +00003996 ++NewParm;
3997 continue;
3998 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003999
Douglas Gregora0347822011-01-13 00:08:50 +00004000 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004001 // [...] When P's template- parameter-list contains a template parameter
4002 // pack (14.5.3), the template parameter pack will match zero or more
4003 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004004 // template-parameter-list of A with the same type and form as the
4005 // template parameter pack in P (ignoring whether those template
4006 // parameters are template parameter packs).
4007 for (; NewParm != NewParmEnd; ++NewParm) {
4008 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4009 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004010 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004011 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004012 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004013
Douglas Gregora0347822011-01-13 00:08:50 +00004014 // Make sure we exhausted all of the arguments.
4015 if (NewParm != NewParmEnd) {
4016 if (Complain)
4017 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4018 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004019
Douglas Gregora0347822011-01-13 00:08:50 +00004020 return false;
4021 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004022
Douglas Gregorddc29e12009-02-06 22:42:48 +00004023 return true;
4024}
4025
4026/// \brief Check whether a template can be declared within this scope.
4027///
4028/// If the template declaration is valid in this scope, returns
4029/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004030bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004031Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004032 // Find the nearest enclosing declaration scope.
4033 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4034 (S->getFlags() & Scope::TemplateParamScope) != 0)
4035 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004036
Douglas Gregorddc29e12009-02-06 22:42:48 +00004037 // C++ [temp]p2:
4038 // A template-declaration can appear only as a namespace scope or
4039 // class scope declaration.
4040 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004041 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4042 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004043 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004044 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004045
Eli Friedman1503f772009-07-31 01:43:05 +00004046 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004047 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004048
4049 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4050 return false;
4051
Mike Stump1eb44332009-09-09 15:08:12 +00004052 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004053 diag::err_template_outside_namespace_or_class_scope)
4054 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004055}
Douglas Gregorcc636682009-02-17 23:15:12 +00004056
Douglas Gregord5cb8762009-10-07 00:13:32 +00004057/// \brief Determine what kind of template specialization the given declaration
4058/// is.
4059static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4060 if (!D)
4061 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004062
Douglas Gregorf6b11852009-10-08 15:14:33 +00004063 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4064 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004065 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4066 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004067 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4068 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004069
Douglas Gregord5cb8762009-10-07 00:13:32 +00004070 return TSK_Undeclared;
4071}
4072
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004073/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004074/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004075///
Douglas Gregor9302da62009-10-14 23:50:59 +00004076/// This routine determines whether a template specialization can be declared
4077/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004078///
4079/// \param S the semantic analysis object for which this check is being
4080/// performed.
4081///
4082/// \param Specialized the entity being specialized or instantiated, which
4083/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004084/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004085/// member class).
4086///
4087/// \param PrevDecl the previous declaration of this entity, if any.
4088///
4089/// \param Loc the location of the explicit specialization or instantiation of
4090/// this entity.
4091///
4092/// \param IsPartialSpecialization whether this is a partial specialization of
4093/// a class template.
4094///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004095/// \returns true if there was an error that we cannot recover from, false
4096/// otherwise.
4097static bool CheckTemplateSpecializationScope(Sema &S,
4098 NamedDecl *Specialized,
4099 NamedDecl *PrevDecl,
4100 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004101 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004102 // Keep these "kind" numbers in sync with the %select statements in the
4103 // various diagnostics emitted by this routine.
4104 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004105 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004106 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004107 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004108 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004109 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004110 EntityKind = 3;
4111 else if (isa<VarDecl>(Specialized))
4112 EntityKind = 4;
4113 else if (isa<RecordDecl>(Specialized))
4114 EntityKind = 5;
4115 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004116 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4117 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004118 return true;
4119 }
4120
Douglas Gregor88b70942009-02-25 22:02:03 +00004121 // C++ [temp.expl.spec]p2:
4122 // An explicit specialization shall be declared in the namespace
4123 // of which the template is a member, or, for member templates, in
4124 // the namespace of which the enclosing class or enclosing class
4125 // template is a member. An explicit specialization of a member
4126 // function, member class or static data member of a class
4127 // template shall be declared in the namespace of which the class
4128 // template is a member. Such a declaration may also be a
4129 // definition. If the declaration is not a definition, the
4130 // specialization may be defined later in the name- space in which
4131 // the explicit specialization was declared, or in a namespace
4132 // that encloses the one in which the explicit specialization was
4133 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004134 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004135 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004136 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004137 return true;
4138 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004139
Douglas Gregor0a407472009-10-07 17:30:37 +00004140 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4141 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004142 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004143 return true;
4144 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004145
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004146 // C++ [temp.class.spec]p6:
4147 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004148 // in any namespace scope in which its definition may be defined (14.5.1
4149 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004150 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004151 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004152 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004153 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004154 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004155 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4156 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004157 // C++ [temp.exp.spec]p2:
4158 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004159 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004160 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004161 // An explicit specialization of a member function, member class or
4162 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004163 // namespace of which the class template is a member.
4164 //
4165 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004166 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004167 // the specialized template.
4168 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4169 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004170 bool IsCPlusPlus0xExtension
4171 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004172 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004173 S.Diag(Loc, IsCPlusPlus0xExtension
4174 ? diag::ext_template_spec_decl_out_of_scope_global
4175 : diag::err_template_spec_decl_out_of_scope_global)
4176 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004177 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004178 S.Diag(Loc, IsCPlusPlus0xExtension
4179 ? diag::ext_template_spec_decl_out_of_scope
4180 : diag::err_template_spec_decl_out_of_scope)
4181 << EntityKind << Specialized
4182 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004183
Douglas Gregor9302da62009-10-14 23:50:59 +00004184 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4185 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004186 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004187 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004188
4189 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004190 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004191 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004192 // specializations of function templates, static data members, and member
4193 // functions, so we skip the check here for those kinds of entities.
4194 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004195 // Should we refactor that check, so that it occurs later?
4196 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004197 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4198 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004199 if (isa<TranslationUnitDecl>(SpecializedContext))
4200 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4201 << EntityKind << Specialized;
4202 else if (isa<NamespaceDecl>(SpecializedContext))
4203 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4204 << EntityKind << Specialized
4205 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004206
Douglas Gregor9302da62009-10-14 23:50:59 +00004207 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004208 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004209
Douglas Gregord5cb8762009-10-07 00:13:32 +00004210 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004211
Douglas Gregor88b70942009-02-25 22:02:03 +00004212 return false;
4213}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004214
Douglas Gregorbacb9492011-01-03 21:13:47 +00004215/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4216/// that checks non-type template partial specialization arguments.
4217static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4218 NonTypeTemplateParmDecl *Param,
4219 const TemplateArgument *Args,
4220 unsigned NumArgs) {
4221 for (unsigned I = 0; I != NumArgs; ++I) {
4222 if (Args[I].getKind() == TemplateArgument::Pack) {
4223 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004224 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004225 Args[I].pack_size()))
4226 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004227
Douglas Gregore94866f2009-06-12 21:21:02 +00004228 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004229 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004230
Douglas Gregorbacb9492011-01-03 21:13:47 +00004231 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004232 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004233 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004234 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004235
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004236 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004237 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4238 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004239
4240 // Strip off any implicit casts we added as part of type checking.
4241 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4242 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004243
Douglas Gregore94866f2009-06-12 21:21:02 +00004244 // C++ [temp.class.spec]p8:
4245 // A non-type argument is non-specialized if it is the name of a
4246 // non-type parameter. All other non-type arguments are
4247 // specialized.
4248 //
4249 // Below, we check the two conditions that only apply to
4250 // specialized non-type arguments, so skip any non-specialized
4251 // arguments.
4252 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004253 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004254 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004255
Douglas Gregore94866f2009-06-12 21:21:02 +00004256 // C++ [temp.class.spec]p9:
4257 // Within the argument list of a class template partial
4258 // specialization, the following restrictions apply:
4259 // -- A partially specialized non-type argument expression
4260 // shall not involve a template parameter of the partial
4261 // specialization except when the argument expression is a
4262 // simple identifier.
4263 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004264 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004265 diag::err_dependent_non_type_arg_in_partial_spec)
4266 << ArgExpr->getSourceRange();
4267 return true;
4268 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004269
Douglas Gregore94866f2009-06-12 21:21:02 +00004270 // -- The type of a template parameter corresponding to a
4271 // specialized non-type argument shall not be dependent on a
4272 // parameter of the specialization.
4273 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004274 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004275 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4276 << Param->getType()
4277 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004278 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004279 return true;
4280 }
4281 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004282
Douglas Gregorbacb9492011-01-03 21:13:47 +00004283 return false;
4284}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004285
Douglas Gregorbacb9492011-01-03 21:13:47 +00004286/// \brief Check the non-type template arguments of a class template
4287/// partial specialization according to C++ [temp.class.spec]p9.
4288///
4289/// \param TemplateParams the template parameters of the primary class
4290/// template.
4291///
4292/// \param TemplateArg the template arguments of the class template
4293/// partial specialization.
4294///
4295/// \returns true if there was an error, false otherwise.
4296static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4297 TemplateParameterList *TemplateParams,
4298 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4299 const TemplateArgument *ArgList = TemplateArgs.data();
4300
4301 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4302 NonTypeTemplateParmDecl *Param
4303 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4304 if (!Param)
4305 continue;
4306
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004307 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004308 &ArgList[I], 1))
4309 return true;
4310 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004311
4312 return false;
4313}
4314
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004315/// \brief Retrieve the previous declaration of the given declaration.
4316static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4317 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4318 return VD->getPreviousDeclaration();
4319 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4320 return FD->getPreviousDeclaration();
4321 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4322 return TD->getPreviousDeclaration();
4323 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4324 return TD->getPreviousDeclaration();
4325 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4326 return FTD->getPreviousDeclaration();
4327 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4328 return CTD->getPreviousDeclaration();
4329 return 0;
4330}
4331
John McCalld226f652010-08-21 09:40:31 +00004332DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004333Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4334 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004335 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004336 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004337 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004338 SourceLocation TemplateNameLoc,
4339 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004340 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004341 SourceLocation RAngleLoc,
4342 AttributeList *Attr,
4343 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004344 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004345
Douglas Gregorcc636682009-02-17 23:15:12 +00004346 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004347 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004348 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004349 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4350
4351 if (!ClassTemplate) {
4352 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004353 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004354 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4355 return true;
4356 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004357
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004358 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004359 bool isPartialSpecialization = false;
4360
Douglas Gregor88b70942009-02-25 22:02:03 +00004361 // Check the validity of the template headers that introduce this
4362 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004363 // FIXME: We probably shouldn't complain about these headers for
4364 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004365 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004366 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004367 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4368 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004369 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004370 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004371 isExplicitSpecialization,
4372 Invalid);
4373 if (Invalid)
4374 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004375
Abramo Bagnara9b934882010-06-12 08:15:14 +00004376 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4377 if (TemplateParams)
4378 --NumMatchedTemplateParamLists;
4379
Douglas Gregor05396e22009-08-25 17:23:04 +00004380 if (TemplateParams && TemplateParams->size() > 0) {
4381 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004382
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004383 if (TUK == TUK_Friend) {
4384 Diag(KWLoc, diag::err_partial_specialization_friend)
4385 << SourceRange(LAngleLoc, RAngleLoc);
4386 return true;
4387 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004388
Douglas Gregor05396e22009-08-25 17:23:04 +00004389 // C++ [temp.class.spec]p10:
4390 // The template parameter list of a specialization shall not
4391 // contain default template argument values.
4392 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4393 Decl *Param = TemplateParams->getParam(I);
4394 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4395 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004396 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004397 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004398 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004399 }
4400 } else if (NonTypeTemplateParmDecl *NTTP
4401 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4402 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004403 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004404 diag::err_default_arg_in_partial_spec)
4405 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004406 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004407 }
4408 } else {
4409 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004410 if (TTP->hasDefaultArgument()) {
4411 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004412 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004413 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004414 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004415 }
4416 }
4417 }
Douglas Gregora735b202009-10-13 14:39:41 +00004418 } else if (TemplateParams) {
4419 if (TUK == TUK_Friend)
4420 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004421 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004422 SourceRange(TemplateParams->getTemplateLoc(),
4423 TemplateParams->getRAngleLoc()))
4424 << SourceRange(LAngleLoc, RAngleLoc);
4425 else
4426 isExplicitSpecialization = true;
4427 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004428 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004429 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004430 isExplicitSpecialization = true;
4431 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004432
Douglas Gregorcc636682009-02-17 23:15:12 +00004433 // Check that the specialization uses the same tag kind as the
4434 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004435 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4436 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004437 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004438 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004439 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004440 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004441 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004442 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004443 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004444 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004445 diag::note_previous_use);
4446 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4447 }
4448
Douglas Gregor40808ce2009-03-09 23:48:35 +00004449 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004450 TemplateArgumentListInfo TemplateArgs;
4451 TemplateArgs.setLAngleLoc(LAngleLoc);
4452 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004453 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004454
Douglas Gregor925910d2011-01-03 20:35:03 +00004455 // Check for unexpanded parameter packs in any of the template arguments.
4456 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004457 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004458 UPPC_PartialSpecialization))
4459 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004460
Douglas Gregorcc636682009-02-17 23:15:12 +00004461 // Check that the template argument list is well-formed for this
4462 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004463 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004464 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4465 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004466 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004467
Douglas Gregor910f8002010-11-07 23:05:16 +00004468 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004469 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004470
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004471 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004472 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004473 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004474 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004475 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004476 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004477 return true;
4478
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004479 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004480 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004481 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004482 TemplateArgs.size())) {
4483 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4484 << ClassTemplate->getDeclName();
4485 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004486 }
4487 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004488
Douglas Gregorcc636682009-02-17 23:15:12 +00004489 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004490 ClassTemplateSpecializationDecl *PrevDecl = 0;
4491
4492 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004493 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004494 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004495 = ClassTemplate->findPartialSpecialization(Converted.data(),
4496 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004497 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004498 else
4499 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004500 = ClassTemplate->findSpecialization(Converted.data(),
4501 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004502
4503 ClassTemplateSpecializationDecl *Specialization = 0;
4504
Douglas Gregor88b70942009-02-25 22:02:03 +00004505 // Check whether we can declare a class template specialization in
4506 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004507 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004508 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4509 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004510 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004511 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004512
Douglas Gregorb88e8882009-07-30 17:40:51 +00004513 // The canonical type
4514 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004515 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004516 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004517 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004518 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004519 // arguments was referenced but not declared, or we're only
4520 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004521 // declaration node as our own, updating its source location to
4522 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004523 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004524 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004525 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004526 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004527 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004528 // Build the canonical type that describes the converted template
4529 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004530 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4531 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004532 Converted.data(),
4533 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004534
4535 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004536 ClassTemplate->getInjectedClassNameSpecialization())) {
4537 // C++ [temp.class.spec]p9b3:
4538 //
4539 // -- The argument list of the specialization shall not be identical
4540 // to the implicit argument list of the primary template.
4541 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4542 << (TUK == TUK_Definition)
4543 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4544 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4545 ClassTemplate->getIdentifier(),
4546 TemplateNameLoc,
4547 Attr,
4548 TemplateParams,
4549 AS_none);
4550 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004551
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004552 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004553 ClassTemplatePartialSpecializationDecl *PrevPartial
4554 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004555 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004556 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004557 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004558 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004559 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004560 TemplateNameLoc,
4561 TemplateParams,
4562 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004563 Converted.data(),
4564 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004565 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004566 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004567 PrevPartial,
4568 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004569 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004570 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004571 Partial->setTemplateParameterListsInfo(Context,
4572 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004573 (TemplateParameterList**) TemplateParameterLists.release());
4574 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004575
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004576 if (!PrevPartial)
4577 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004578 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004579
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004580 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004581 // template specialization, make a note of that.
4582 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4583 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004584
Douglas Gregor031a5882009-06-13 00:26:55 +00004585 // Check that all of the template parameters of the class template
4586 // partial specialization are deducible from the template
4587 // arguments. If not, this class template partial specialization
4588 // will never be used.
4589 llvm::SmallVector<bool, 8> DeducibleParams;
4590 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004591 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004592 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004593 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004594 unsigned NumNonDeducible = 0;
4595 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4596 if (!DeducibleParams[I])
4597 ++NumNonDeducible;
4598
4599 if (NumNonDeducible) {
4600 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4601 << (NumNonDeducible > 1)
4602 << SourceRange(TemplateNameLoc, RAngleLoc);
4603 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4604 if (!DeducibleParams[I]) {
4605 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4606 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004607 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004608 diag::note_partial_spec_unused_parameter)
4609 << Param->getDeclName();
4610 else
Mike Stump1eb44332009-09-09 15:08:12 +00004611 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004612 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004613 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004614 }
4615 }
4616 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004617 } else {
4618 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004619 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004620 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004621 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004622 ClassTemplate->getDeclContext(),
4623 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004624 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004625 Converted.data(),
4626 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004627 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004628 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004629 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004630 Specialization->setTemplateParameterListsInfo(Context,
4631 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004632 (TemplateParameterList**) TemplateParameterLists.release());
4633 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004634
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004635 if (!PrevDecl)
4636 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004637
4638 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004639 }
4640
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004641 // C++ [temp.expl.spec]p6:
4642 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004643 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004644 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004645 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004646 // use occurs; no diagnostic is required.
4647 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004648 bool Okay = false;
4649 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4650 // Is there any previous explicit specialization declaration?
4651 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4652 Okay = true;
4653 break;
4654 }
4655 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004656
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004657 if (!Okay) {
4658 SourceRange Range(TemplateNameLoc, RAngleLoc);
4659 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4660 << Context.getTypeDeclType(Specialization) << Range;
4661
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004662 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004663 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004664 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004665 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004666 return true;
4667 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004668 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004669
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004670 // If this is not a friend, note that this is an explicit specialization.
4671 if (TUK != TUK_Friend)
4672 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004673
4674 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004675 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004676 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004677 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004678 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004679 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004680 Diag(Def->getLocation(), diag::note_previous_definition);
4681 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004682 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004683 }
4684 }
4685
John McCall7f1b9872010-12-18 03:30:47 +00004686 if (Attr)
4687 ProcessDeclAttributeList(S, Specialization, Attr);
4688
Douglas Gregorfc705b82009-02-26 22:19:44 +00004689 // Build the fully-sugared type for this class template
4690 // specialization as the user wrote in the specialization
4691 // itself. This means that we'll pretty-print the type retrieved
4692 // from the specialization's declaration the way that the user
4693 // actually wrote the specialization, rather than formatting the
4694 // name based on the "canonical" representation used to store the
4695 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004696 TypeSourceInfo *WrittenTy
4697 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4698 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004699 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004700 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004701 if (TemplateParams)
4702 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004703 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004704 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004705
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004706 // C++ [temp.expl.spec]p9:
4707 // A template explicit specialization is in the scope of the
4708 // namespace in which the template was defined.
4709 //
4710 // We actually implement this paragraph where we set the semantic
4711 // context (in the creation of the ClassTemplateSpecializationDecl),
4712 // but we also maintain the lexical context where the actual
4713 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004714 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004715
Douglas Gregorcc636682009-02-17 23:15:12 +00004716 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004717 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004718 Specialization->startDefinition();
4719
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004720 if (TUK == TUK_Friend) {
4721 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4722 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004723 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004724 /*FIXME:*/KWLoc);
4725 Friend->setAccess(AS_public);
4726 CurContext->addDecl(Friend);
4727 } else {
4728 // Add the specialization into its lexical context, so that it can
4729 // be seen when iterating through the list of declarations in that
4730 // context. However, specializations are not found by name lookup.
4731 CurContext->addDecl(Specialization);
4732 }
John McCalld226f652010-08-21 09:40:31 +00004733 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004734}
Douglas Gregord57959a2009-03-27 23:10:48 +00004735
John McCalld226f652010-08-21 09:40:31 +00004736Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004737 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004738 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004739 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4740}
4741
John McCalld226f652010-08-21 09:40:31 +00004742Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004743 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004744 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004745 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004746 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004747
Douglas Gregor52591bf2009-06-24 00:54:41 +00004748 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004749 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004750 }
Mike Stump1eb44332009-09-09 15:08:12 +00004751
Douglas Gregor52591bf2009-06-24 00:54:41 +00004752 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004753
John McCalld226f652010-08-21 09:40:31 +00004754 Decl *DP = HandleDeclarator(ParentScope, D,
4755 move(TemplateParameterLists),
4756 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004757 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004758 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004759 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004760 FunctionTemplate->getTemplatedDecl());
4761 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4762 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4763 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004764}
4765
John McCall75042392010-02-11 01:33:53 +00004766/// \brief Strips various properties off an implicit instantiation
4767/// that has just been explicitly specialized.
4768static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004769 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004770
4771 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4772 FD->setInlineSpecified(false);
4773 }
4774}
4775
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004776/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004777/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004778/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004779/// new specialization/instantiation will have any effect.
4780///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004781/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004782/// instantiation.
4783///
4784/// \param NewTSK the kind of the new explicit specialization or instantiation.
4785///
4786/// \param PrevDecl the previous declaration of the entity.
4787///
4788/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4789///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004790/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004791/// declaration was instantiated (either implicitly or explicitly).
4792///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004793/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004794/// specialization or instantiation has no effect and should be ignored.
4795///
4796/// \returns true if there was an error that should prevent the introduction of
4797/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004798bool
4799Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4800 TemplateSpecializationKind NewTSK,
4801 NamedDecl *PrevDecl,
4802 TemplateSpecializationKind PrevTSK,
4803 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004804 bool &HasNoEffect) {
4805 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004806
Douglas Gregor454885e2009-10-15 15:54:05 +00004807 switch (NewTSK) {
4808 case TSK_Undeclared:
4809 case TSK_ImplicitInstantiation:
4810 assert(false && "Don't check implicit instantiations here");
4811 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004812
Douglas Gregor454885e2009-10-15 15:54:05 +00004813 case TSK_ExplicitSpecialization:
4814 switch (PrevTSK) {
4815 case TSK_Undeclared:
4816 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004817 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004818 // explicitly specialized or has merely been mentioned without any
4819 // instantiation.
4820 return false;
4821
4822 case TSK_ImplicitInstantiation:
4823 if (PrevPointOfInstantiation.isInvalid()) {
4824 // The declaration itself has not actually been instantiated, so it is
4825 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004826 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004827 return false;
4828 }
4829 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004830
Douglas Gregor454885e2009-10-15 15:54:05 +00004831 case TSK_ExplicitInstantiationDeclaration:
4832 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004833 assert((PrevTSK == TSK_ImplicitInstantiation ||
4834 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004835 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004836
Douglas Gregor454885e2009-10-15 15:54:05 +00004837 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004838 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004839 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004840 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004841 // implicit instantiation to take place, in every translation unit in
4842 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004843 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4844 // Is there any previous explicit specialization declaration?
4845 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4846 return false;
4847 }
4848
Douglas Gregor0d035142009-10-27 18:42:08 +00004849 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004850 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004851 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004852 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004853
Douglas Gregor454885e2009-10-15 15:54:05 +00004854 return true;
4855 }
4856 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004857
Douglas Gregor454885e2009-10-15 15:54:05 +00004858 case TSK_ExplicitInstantiationDeclaration:
4859 switch (PrevTSK) {
4860 case TSK_ExplicitInstantiationDeclaration:
4861 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004862 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004863 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004864
Douglas Gregor454885e2009-10-15 15:54:05 +00004865 case TSK_Undeclared:
4866 case TSK_ImplicitInstantiation:
4867 // We're explicitly instantiating something that may have already been
4868 // implicitly instantiated; that's fine.
4869 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004870
Douglas Gregor454885e2009-10-15 15:54:05 +00004871 case TSK_ExplicitSpecialization:
4872 // C++0x [temp.explicit]p4:
4873 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004874 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004875 // specialization for that template, the explicit instantiation has no
4876 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004877 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004878 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004879
Douglas Gregor454885e2009-10-15 15:54:05 +00004880 case TSK_ExplicitInstantiationDefinition:
4881 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004882 // If an entity is the subject of both an explicit instantiation
4883 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004884 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004885 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004886 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004887 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004888 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004889 assert(PrevPointOfInstantiation.isValid() &&
4890 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004891 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004892 return false;
4893 }
4894 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004895
Douglas Gregor454885e2009-10-15 15:54:05 +00004896 case TSK_ExplicitInstantiationDefinition:
4897 switch (PrevTSK) {
4898 case TSK_Undeclared:
4899 case TSK_ImplicitInstantiation:
4900 // We're explicitly instantiating something that may have already been
4901 // implicitly instantiated; that's fine.
4902 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004903
Douglas Gregor454885e2009-10-15 15:54:05 +00004904 case TSK_ExplicitSpecialization:
4905 // C++ DR 259, C++0x [temp.explicit]p4:
4906 // For a given set of template parameters, if an explicit
4907 // instantiation of a template appears after a declaration of
4908 // an explicit specialization for that template, the explicit
4909 // instantiation has no effect.
4910 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004911 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004912 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004913 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004914 if (!getLangOptions().CPlusPlus0x) {
4915 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004916 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004917 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004918 diag::note_previous_template_specialization);
4919 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004920 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004921 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004922
Douglas Gregor454885e2009-10-15 15:54:05 +00004923 case TSK_ExplicitInstantiationDeclaration:
4924 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004925 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004926 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004927
Douglas Gregor454885e2009-10-15 15:54:05 +00004928 case TSK_ExplicitInstantiationDefinition:
4929 // C++0x [temp.spec]p5:
4930 // For a given template and a given set of template-arguments,
4931 // - an explicit instantiation definition shall appear at most once
4932 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004933 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004934 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004935 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004936 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004937 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004938 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004939 }
4940 break;
4941 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004942
Douglas Gregor454885e2009-10-15 15:54:05 +00004943 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004944
Douglas Gregor454885e2009-10-15 15:54:05 +00004945 return false;
4946}
4947
John McCallaf2094e2010-04-08 09:05:18 +00004948/// \brief Perform semantic analysis for the given dependent function
4949/// template specialization. The only possible way to get a dependent
4950/// function template specialization is with a friend declaration,
4951/// like so:
4952///
4953/// template <class T> void foo(T);
4954/// template <class T> class A {
4955/// friend void foo<>(T);
4956/// };
4957///
4958/// There really isn't any useful analysis we can do here, so we
4959/// just store the information.
4960bool
4961Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
4962 const TemplateArgumentListInfo &ExplicitTemplateArgs,
4963 LookupResult &Previous) {
4964 // Remove anything from Previous that isn't a function template in
4965 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004966 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00004967 LookupResult::Filter F = Previous.makeFilter();
4968 while (F.hasNext()) {
4969 NamedDecl *D = F.next()->getUnderlyingDecl();
4970 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00004971 !FDLookupContext->InEnclosingNamespaceSetOf(
4972 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00004973 F.erase();
4974 }
4975 F.done();
4976
4977 // Should this be diagnosed here?
4978 if (Previous.empty()) return true;
4979
4980 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
4981 ExplicitTemplateArgs);
4982 return false;
4983}
4984
Abramo Bagnarae03db982010-05-20 15:32:11 +00004985/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004986/// specialization.
4987///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004988/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004989/// explicit function template specialization. On successful completion,
4990/// the function declaration \p FD will become a function template
4991/// specialization.
4992///
4993/// \param FD the function declaration, which will be updated to become a
4994/// function template specialization.
4995///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004996/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
4997/// if any. Note that this may be valid info even when 0 arguments are
4998/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
4999/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005000///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005001/// \param PrevDecl the set of declarations that may be specialized by
5002/// this function specialization.
5003bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005004Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
John McCalld5532b62009-11-23 01:53:49 +00005005 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005006 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005007 // The set of function template specializations that could match this
5008 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005009 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005010
Sebastian Redl7a126a42010-08-31 00:36:30 +00005011 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005012 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5013 I != E; ++I) {
5014 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5015 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005016 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005017 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005018 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5019 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005020 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005021
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005022 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005023 // A trailing template-argument can be left unspecified in the
5024 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005025 // provided it can be deduced from the function argument type.
5026 // Perform template argument deduction to determine whether we may be
5027 // specializing this template.
5028 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005029 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005030 FunctionDecl *Specialization = 0;
5031 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005032 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005033 FD->getType(),
5034 Specialization,
5035 Info)) {
5036 // FIXME: Template argument deduction failed; record why it failed, so
5037 // that we can provide nifty diagnostics.
5038 (void)TDK;
5039 continue;
5040 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005041
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005042 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005043 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005044 }
5045 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005046
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005047 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005048 UnresolvedSetIterator Result
5049 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005050 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005051 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005052 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005053 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005054 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005055 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005056 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005057 return true;
John McCallc373d482010-01-27 01:50:18 +00005058
5059 // Ignore access information; it doesn't figure into redeclaration checking.
5060 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregorc42b6522010-04-09 21:02:29 +00005061 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005062
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005063 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005064 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005065
5066 // If this is a friend declaration, then we're not really declaring
5067 // an explicit specialization.
5068 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005069
Douglas Gregord5cb8762009-10-07 00:13:32 +00005070 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005071 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005072 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005073 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005074 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005075 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005076 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005077
5078 // C++ [temp.expl.spec]p6:
5079 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005080 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005081 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005082 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005083 // use occurs; no diagnostic is required.
5084 FunctionTemplateSpecializationInfo *SpecInfo
5085 = Specialization->getTemplateSpecializationInfo();
5086 assert(SpecInfo && "Function template specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005087
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005088 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005089 if (!isFriend &&
5090 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005091 TSK_ExplicitSpecialization,
5092 Specialization,
5093 SpecInfo->getTemplateSpecializationKind(),
5094 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005095 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005096 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005097
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005098 // Mark the prior declaration as an explicit specialization, so that later
5099 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005100 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005101 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005102 MarkUnusedFileScopedDecl(Specialization);
5103 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005104
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005105 // Turn the given function declaration into a function template
5106 // specialization, with the template arguments from the previous
5107 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005108 // Take copies of (semantic and syntactic) template argument lists.
5109 const TemplateArgumentList* TemplArgs = new (Context)
5110 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5111 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5112 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005113 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005114 TemplArgs, /*InsertPos=*/0,
5115 SpecInfo->getTemplateSpecializationKind(),
5116 TemplArgsAsWritten);
5117
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005118 // The "previous declaration" for this function template specialization is
5119 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005120 Previous.clear();
5121 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005122 return false;
5123}
5124
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005125/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005126/// specialization.
5127///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005128/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005129/// explicit member function specialization. On successful completion,
5130/// the function declaration \p FD will become a member function
5131/// specialization.
5132///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005133/// \param Member the member declaration, which will be updated to become a
5134/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005135///
John McCall68263142009-11-18 22:49:29 +00005136/// \param Previous the set of declarations, one of which may be specialized
5137/// by this function specialization; the set will be modified to contain the
5138/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005139bool
John McCall68263142009-11-18 22:49:29 +00005140Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005141 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005142
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005143 // Try to find the member we are instantiating.
5144 NamedDecl *Instantiation = 0;
5145 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005146 MemberSpecializationInfo *MSInfo = 0;
5147
John McCall68263142009-11-18 22:49:29 +00005148 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005149 // Nowhere to look anyway.
5150 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005151 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5152 I != E; ++I) {
5153 NamedDecl *D = (*I)->getUnderlyingDecl();
5154 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005155 if (Context.hasSameType(Function->getType(), Method->getType())) {
5156 Instantiation = Method;
5157 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005158 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005159 break;
5160 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005161 }
5162 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005163 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005164 VarDecl *PrevVar;
5165 if (Previous.isSingleResult() &&
5166 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005167 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005168 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005169 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005170 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005171 }
5172 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005173 CXXRecordDecl *PrevRecord;
5174 if (Previous.isSingleResult() &&
5175 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5176 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005177 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005178 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005179 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005180 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005181
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005182 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005183 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005184 // specializations are always out-of-line, the caller will complain about
5185 // this mismatch later.
5186 return false;
5187 }
John McCall77e8b112010-04-13 20:37:33 +00005188
5189 // If this is a friend, just bail out here before we start turning
5190 // things into explicit specializations.
5191 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5192 // Preserve instantiation information.
5193 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5194 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5195 cast<CXXMethodDecl>(InstantiatedFrom),
5196 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5197 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5198 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5199 cast<CXXRecordDecl>(InstantiatedFrom),
5200 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5201 }
5202
5203 Previous.clear();
5204 Previous.addDecl(Instantiation);
5205 return false;
5206 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005207
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005208 // Make sure that this is a specialization of a member.
5209 if (!InstantiatedFrom) {
5210 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5211 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005212 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5213 return true;
5214 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005215
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005216 // C++ [temp.expl.spec]p6:
5217 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005218 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005219 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005220 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005221 // use occurs; no diagnostic is required.
5222 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005223
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005224 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005225 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5226 TSK_ExplicitSpecialization,
5227 Instantiation,
5228 MSInfo->getTemplateSpecializationKind(),
5229 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005230 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005231 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005232
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005233 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005234 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005235 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005236 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005237 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005238 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005239
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005240 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005241 // the original declaration to note that it is an explicit specialization
5242 // (if it was previously an implicit instantiation). This latter step
5243 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005244 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005245 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5246 if (InstantiationFunction->getTemplateSpecializationKind() ==
5247 TSK_ImplicitInstantiation) {
5248 InstantiationFunction->setTemplateSpecializationKind(
5249 TSK_ExplicitSpecialization);
5250 InstantiationFunction->setLocation(Member->getLocation());
5251 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005252
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005253 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5254 cast<CXXMethodDecl>(InstantiatedFrom),
5255 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005256 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005257 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005258 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5259 if (InstantiationVar->getTemplateSpecializationKind() ==
5260 TSK_ImplicitInstantiation) {
5261 InstantiationVar->setTemplateSpecializationKind(
5262 TSK_ExplicitSpecialization);
5263 InstantiationVar->setLocation(Member->getLocation());
5264 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005265
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005266 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5267 cast<VarDecl>(InstantiatedFrom),
5268 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005269 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005270 } else {
5271 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005272 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5273 if (InstantiationClass->getTemplateSpecializationKind() ==
5274 TSK_ImplicitInstantiation) {
5275 InstantiationClass->setTemplateSpecializationKind(
5276 TSK_ExplicitSpecialization);
5277 InstantiationClass->setLocation(Member->getLocation());
5278 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005279
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005280 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005281 cast<CXXRecordDecl>(InstantiatedFrom),
5282 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005283 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005284
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005285 // Save the caller the trouble of having to figure out which declaration
5286 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005287 Previous.clear();
5288 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005289 return false;
5290}
5291
Douglas Gregor558c0322009-10-14 23:41:34 +00005292/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005293///
5294/// \returns true if a serious error occurs, false otherwise.
5295static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005296 SourceLocation InstLoc,
5297 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005298 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5299 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005300
Douglas Gregor669eed82010-07-13 00:10:04 +00005301 if (CurContext->isRecord()) {
5302 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5303 << D;
5304 return true;
5305 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005306
Douglas Gregor558c0322009-10-14 23:41:34 +00005307 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005308 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005309 // template.
5310 //
5311 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005312 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005313 !CurContext->Encloses(OrigContext)) {
5314 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005315 S.Diag(InstLoc,
5316 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005317 diag::err_explicit_instantiation_out_of_scope
5318 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005319 << D << NS;
5320 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005321 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005322 S.getLangOptions().CPlusPlus0x?
5323 diag::err_explicit_instantiation_must_be_global
5324 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005325 << D;
5326 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005327 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005328 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005329
Douglas Gregor558c0322009-10-14 23:41:34 +00005330 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005331 // If the name declared in the explicit instantiation is an unqualified
5332 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005333 // its template is declared or, if that namespace is inline (7.3.1), any
5334 // namespace from its enclosing namespace set.
5335 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005336 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005337
5338 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005339 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005340
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005341 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005342 S.getLangOptions().CPlusPlus0x?
5343 diag::err_explicit_instantiation_unqualified_wrong_namespace
5344 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005345 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005346 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005347 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005348}
5349
5350/// \brief Determine whether the given scope specifier has a template-id in it.
5351static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5352 if (!SS.isSet())
5353 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005354
Douglas Gregor558c0322009-10-14 23:41:34 +00005355 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005356 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005357 // or a static data member of a class template specialization, the name of
5358 // the class template specialization in the qualified-id for the member
5359 // name shall be a simple-template-id.
5360 //
5361 // C++98 has the same restriction, just worded differently.
5362 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5363 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005364 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005365 if (isa<TemplateSpecializationType>(T))
5366 return true;
5367
5368 return false;
5369}
5370
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005371// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005372DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005373Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005374 SourceLocation ExternLoc,
5375 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005376 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005377 SourceLocation KWLoc,
5378 const CXXScopeSpec &SS,
5379 TemplateTy TemplateD,
5380 SourceLocation TemplateNameLoc,
5381 SourceLocation LAngleLoc,
5382 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005383 SourceLocation RAngleLoc,
5384 AttributeList *Attr) {
5385 // Find the class template we're specializing
5386 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005387 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005388 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5389
5390 // Check that the specialization uses the same tag kind as the
5391 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005392 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5393 assert(Kind != TTK_Enum &&
5394 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005395 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005396 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005397 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005398 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005399 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005400 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005401 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005402 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005403 diag::note_previous_use);
5404 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5405 }
5406
Douglas Gregor558c0322009-10-14 23:41:34 +00005407 // C++0x [temp.explicit]p2:
5408 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005409 // definition and an explicit instantiation declaration. An explicit
5410 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005411 TemplateSpecializationKind TSK
5412 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5413 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005414
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005415 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005416 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005417 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005418
5419 // Check that the template argument list is well-formed for this
5420 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005421 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005422 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5423 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005424 return true;
5425
Douglas Gregor910f8002010-11-07 23:05:16 +00005426 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005427 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005428
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005429 // Find the class template specialization declaration that
5430 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005431 void *InsertPos = 0;
5432 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005433 = ClassTemplate->findSpecialization(Converted.data(),
5434 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005435
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005436 TemplateSpecializationKind PrevDecl_TSK
5437 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5438
Douglas Gregord5cb8762009-10-07 00:13:32 +00005439 // C++0x [temp.explicit]p2:
5440 // [...] An explicit instantiation shall appear in an enclosing
5441 // namespace of its template. [...]
5442 //
5443 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005444 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5445 SS.isSet()))
5446 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005447
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005448 ClassTemplateSpecializationDecl *Specialization = 0;
5449
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005450 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005451 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005452 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005453 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005454 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005455 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005456 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005457
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005458 // Even though HasNoEffect == true means that this explicit instantiation
5459 // has no effect on semantics, we go on to put its syntax in the AST.
5460
5461 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5462 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005463 // Since the only prior class template specialization with these
5464 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005465 // declaration node as our own, updating the source location
5466 // for the template name to reflect our new declaration.
5467 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005468 Specialization = PrevDecl;
5469 Specialization->setLocation(TemplateNameLoc);
5470 PrevDecl = 0;
5471 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005472 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005473
Douglas Gregor52604ab2009-09-11 21:19:12 +00005474 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005475 // Create a new class template specialization declaration node for
5476 // this explicit specialization.
5477 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005478 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005479 ClassTemplate->getDeclContext(),
5480 TemplateNameLoc,
5481 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005482 Converted.data(),
5483 Converted.size(),
5484 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005485 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005486
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005487 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005488 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005489 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005490 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005491 }
5492
5493 // Build the fully-sugared type for this explicit instantiation as
5494 // the user wrote in the explicit instantiation itself. This means
5495 // that we'll pretty-print the type retrieved from the
5496 // specialization's declaration the way that the user actually wrote
5497 // the explicit instantiation, rather than formatting the name based
5498 // on the "canonical" representation used to store the template
5499 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005500 TypeSourceInfo *WrittenTy
5501 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5502 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005503 Context.getTypeDeclType(Specialization));
5504 Specialization->setTypeAsWritten(WrittenTy);
5505 TemplateArgsIn.release();
5506
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005507 // Set source locations for keywords.
5508 Specialization->setExternLoc(ExternLoc);
5509 Specialization->setTemplateKeywordLoc(TemplateLoc);
5510
5511 // Add the explicit instantiation into its lexical context. However,
5512 // since explicit instantiations are never found by name lookup, we
5513 // just put it into the declaration context directly.
5514 Specialization->setLexicalDeclContext(CurContext);
5515 CurContext->addDecl(Specialization);
5516
5517 // Syntax is now OK, so return if it has no other effect on semantics.
5518 if (HasNoEffect) {
5519 // Set the template specialization kind.
5520 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005521 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005522 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005523
5524 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005525 // A definition of a class template or class member template
5526 // shall be in scope at the point of the explicit instantiation of
5527 // the class template or class member template.
5528 //
5529 // This check comes when we actually try to perform the
5530 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005531 ClassTemplateSpecializationDecl *Def
5532 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005533 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005534 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005535 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005536 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005537 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005538 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5539 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005540
Douglas Gregor0d035142009-10-27 18:42:08 +00005541 // Instantiate the members of this class template specialization.
5542 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005543 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005544 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005545 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5546
5547 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5548 // TSK_ExplicitInstantiationDefinition
5549 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5550 TSK == TSK_ExplicitInstantiationDefinition)
5551 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005552
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005553 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005554 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005555
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005556 // Set the template specialization kind.
5557 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005558 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005559}
5560
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005561// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005562DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005563Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005564 SourceLocation ExternLoc,
5565 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005566 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005567 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005568 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005569 IdentifierInfo *Name,
5570 SourceLocation NameLoc,
5571 AttributeList *Attr) {
5572
Douglas Gregor402abb52009-05-28 23:31:59 +00005573 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005574 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005575 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005576 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5577 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005578 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005579 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005580 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5581
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005582 if (!TagD)
5583 return true;
5584
John McCalld226f652010-08-21 09:40:31 +00005585 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005586 if (Tag->isEnum()) {
5587 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5588 << Context.getTypeDeclType(Tag);
5589 return true;
5590 }
5591
Douglas Gregord0c87372009-05-27 17:30:49 +00005592 if (Tag->isInvalidDecl())
5593 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005594
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005595 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5596 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5597 if (!Pattern) {
5598 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5599 << Context.getTypeDeclType(Record);
5600 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5601 return true;
5602 }
5603
Douglas Gregor558c0322009-10-14 23:41:34 +00005604 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005605 // If the explicit instantiation is for a class or member class, the
5606 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005607 // simple-template-id.
5608 //
5609 // C++98 has the same restriction, just worded differently.
5610 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005611 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005612 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005613
Douglas Gregor558c0322009-10-14 23:41:34 +00005614 // C++0x [temp.explicit]p2:
5615 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005616 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005617 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005618 TemplateSpecializationKind TSK
5619 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5620 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005621
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005622 // C++0x [temp.explicit]p2:
5623 // [...] An explicit instantiation shall appear in an enclosing
5624 // namespace of its template. [...]
5625 //
5626 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005627 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005628
Douglas Gregor454885e2009-10-15 15:54:05 +00005629 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005630 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005631 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005632 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005633 PrevDecl = Record;
5634 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005635 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005636 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005637 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005638 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005639 PrevDecl,
5640 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005641 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005642 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005643 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005644 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005645 return TagD;
5646 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005647
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005648 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005649 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005650 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005651 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005652 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005653 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005654 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005655 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005656 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005657 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5658 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005659 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5660 << Pattern;
5661 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005662 } else {
5663 if (InstantiateClass(NameLoc, Record, Def,
5664 getTemplateInstantiationArgs(Record),
5665 TSK))
5666 return true;
5667
Douglas Gregor952b0172010-02-11 01:04:33 +00005668 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005669 if (!RecordDef)
5670 return true;
5671 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005672 }
5673
Douglas Gregor0d035142009-10-27 18:42:08 +00005674 // Instantiate all of the members of the class.
5675 InstantiateClassMembers(NameLoc, RecordDef,
5676 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005677
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005678 if (TSK == TSK_ExplicitInstantiationDefinition)
5679 MarkVTableUsed(NameLoc, RecordDef, true);
5680
Mike Stump390b4cc2009-05-16 07:39:55 +00005681 // FIXME: We don't have any representation for explicit instantiations of
5682 // member classes. Such a representation is not needed for compilation, but it
5683 // should be available for clients that want to see all of the declarations in
5684 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005685 return TagD;
5686}
5687
John McCallf312b1e2010-08-26 23:41:50 +00005688DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5689 SourceLocation ExternLoc,
5690 SourceLocation TemplateLoc,
5691 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005692 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005693 // TODO: check if/when DNInfo should replace Name.
5694 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5695 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005696 if (!Name) {
5697 if (!D.isInvalidType())
5698 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5699 diag::err_explicit_instantiation_requires_name)
5700 << D.getDeclSpec().getSourceRange()
5701 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005702
Douglas Gregord5a423b2009-09-25 18:43:00 +00005703 return true;
5704 }
5705
5706 // The scope passed in may not be a decl scope. Zip up the scope tree until
5707 // we find one that is.
5708 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5709 (S->getFlags() & Scope::TemplateParamScope) != 0)
5710 S = S->getParent();
5711
5712 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005713 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5714 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005715 if (R.isNull())
5716 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005717
Douglas Gregord5a423b2009-09-25 18:43:00 +00005718 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5719 // Cannot explicitly instantiate a typedef.
5720 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5721 << Name;
5722 return true;
5723 }
5724
Douglas Gregor663b5a02009-10-14 20:14:33 +00005725 // C++0x [temp.explicit]p1:
5726 // [...] An explicit instantiation of a function template shall not use the
5727 // inline or constexpr specifiers.
5728 // Presumably, this also applies to member functions of class templates as
5729 // well.
5730 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005731 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005732 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005733 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005734
Douglas Gregor663b5a02009-10-14 20:14:33 +00005735 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005736
Douglas Gregor558c0322009-10-14 23:41:34 +00005737 // C++0x [temp.explicit]p2:
5738 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005739 // definition and an explicit instantiation declaration. An explicit
5740 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005741 TemplateSpecializationKind TSK
5742 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5743 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005744
Abramo Bagnara25777432010-08-11 22:01:17 +00005745 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005746 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005747
5748 if (!R->isFunctionType()) {
5749 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005750 // A [...] static data member of a class template can be explicitly
5751 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005752 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005753 if (Previous.isAmbiguous())
5754 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005755
John McCall1bcee0a2009-12-02 08:25:40 +00005756 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005757 if (!Prev || !Prev->isStaticDataMember()) {
5758 // We expect to see a data data member here.
5759 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5760 << Name;
5761 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5762 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005763 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005764 return true;
5765 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005766
Douglas Gregord5a423b2009-09-25 18:43:00 +00005767 if (!Prev->getInstantiatedFromStaticDataMember()) {
5768 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005769 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005770 diag::err_explicit_instantiation_data_member_not_instantiated)
5771 << Prev;
5772 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5773 // FIXME: Can we provide a note showing where this was declared?
5774 return true;
5775 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005776
Douglas Gregor558c0322009-10-14 23:41:34 +00005777 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005778 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005779 // or a static data member of a class template specialization, the name of
5780 // the class template specialization in the qualified-id for the member
5781 // name shall be a simple-template-id.
5782 //
5783 // C++98 has the same restriction, just worded differently.
5784 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005785 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005786 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005787 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005788
Douglas Gregor558c0322009-10-14 23:41:34 +00005789 // Check the scope of this explicit instantiation.
5790 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005791
Douglas Gregor454885e2009-10-15 15:54:05 +00005792 // Verify that it is okay to explicitly instantiate here.
5793 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5794 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005795 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005796 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005797 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005798 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005799 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005800 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005801 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005802 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005803
Douglas Gregord5a423b2009-09-25 18:43:00 +00005804 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005805 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005806 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005807 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005808
Douglas Gregord5a423b2009-09-25 18:43:00 +00005809 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005810 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005811 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005812
5813 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005814 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005815 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005816 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005817 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5818 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005819 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5820 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005821 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5822 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005823 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005824 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005825 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005826 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005827 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005828
Douglas Gregord5a423b2009-09-25 18:43:00 +00005829 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005830 // A [...] function [...] can be explicitly instantiated from its template.
5831 // A member function [...] of a class template can be explicitly
5832 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005833 // template.
John McCallc373d482010-01-27 01:50:18 +00005834 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005835 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5836 P != PEnd; ++P) {
5837 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005838 if (!HasExplicitTemplateArgs) {
5839 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5840 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5841 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005842
John McCallc373d482010-01-27 01:50:18 +00005843 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005844 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5845 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005846 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005847 }
5848 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005849
Douglas Gregord5a423b2009-09-25 18:43:00 +00005850 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5851 if (!FunTmpl)
5852 continue;
5853
John McCall5769d612010-02-08 23:07:23 +00005854 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005855 FunctionDecl *Specialization = 0;
5856 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005857 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005858 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005859 R, Specialization, Info)) {
5860 // FIXME: Keep track of almost-matches?
5861 (void)TDK;
5862 continue;
5863 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005864
John McCallc373d482010-01-27 01:50:18 +00005865 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005866 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005867
Douglas Gregord5a423b2009-09-25 18:43:00 +00005868 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005869 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005870 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005871 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005872 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5873 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5874 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005875
John McCallc373d482010-01-27 01:50:18 +00005876 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005877 return true;
John McCallc373d482010-01-27 01:50:18 +00005878
5879 // Ignore access control bits, we don't need them for redeclaration checking.
5880 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005881
Douglas Gregor0a897e32009-10-15 17:21:20 +00005882 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005883 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005884 diag::err_explicit_instantiation_member_function_not_instantiated)
5885 << Specialization
5886 << (Specialization->getTemplateSpecializationKind() ==
5887 TSK_ExplicitSpecialization);
5888 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5889 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005890 }
5891
Douglas Gregor0a897e32009-10-15 17:21:20 +00005892 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005893 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5894 PrevDecl = Specialization;
5895
Douglas Gregor0a897e32009-10-15 17:21:20 +00005896 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005897 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005898 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005899 PrevDecl,
5900 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005901 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005902 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005903 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005904
Douglas Gregor0a897e32009-10-15 17:21:20 +00005905 // FIXME: We may still want to build some representation of this
5906 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005907 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005908 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005909 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005910
5911 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005912
Douglas Gregor0a897e32009-10-15 17:21:20 +00005913 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005914 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005915
Douglas Gregor558c0322009-10-14 23:41:34 +00005916 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005917 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005918 // or a static data member of a class template specialization, the name of
5919 // the class template specialization in the qualified-id for the member
5920 // name shall be a simple-template-id.
5921 //
5922 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005923 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005924 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005925 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005926 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005927 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005928 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005929 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005930
Douglas Gregor558c0322009-10-14 23:41:34 +00005931 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005932 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005933 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005934 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005935 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005936
Douglas Gregord5a423b2009-09-25 18:43:00 +00005937 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005938 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005939}
5940
John McCallf312b1e2010-08-26 23:41:50 +00005941TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005942Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5943 const CXXScopeSpec &SS, IdentifierInfo *Name,
5944 SourceLocation TagLoc, SourceLocation NameLoc) {
5945 // This has to hold, because SS is expected to be defined.
5946 assert(Name && "Expected a name in a dependent tag");
5947
5948 NestedNameSpecifier *NNS
5949 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5950 if (!NNS)
5951 return true;
5952
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005953 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005954
Douglas Gregor48c89f42010-04-24 16:38:41 +00005955 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
5956 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005957 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00005958 return true;
5959 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005960
Douglas Gregor059101f2011-03-02 00:47:37 +00005961 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005962 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00005963 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
5964
5965 // Create type-source location information for this type.
5966 TypeLocBuilder TLB;
5967 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
5968 TL.setKeywordLoc(TagLoc);
5969 TL.setQualifierLoc(SS.getWithLocInContext(Context));
5970 TL.setNameLoc(NameLoc);
5971 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00005972}
5973
John McCallf312b1e2010-08-26 23:41:50 +00005974TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005975Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5976 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005977 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00005978 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00005979 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00005980
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005981 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5982 !getLangOptions().CPlusPlus0x)
5983 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005984 << FixItHint::CreateRemoval(TypenameLoc);
5985
Douglas Gregor2494dd02011-03-01 01:34:45 +00005986 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00005987 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
5988 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00005989 if (T.isNull())
5990 return true;
John McCall63b43852010-04-29 23:50:39 +00005991
5992 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
5993 if (isa<DependentNameType>(T)) {
5994 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005995 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00005996 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00005997 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005998 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005999 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006000 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006001 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006002 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006003 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006004
John McCallb3d87482010-08-24 05:47:05 +00006005 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006006}
6007
John McCallf312b1e2010-08-26 23:41:50 +00006008TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006009Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6010 const CXXScopeSpec &SS,
6011 SourceLocation TemplateLoc,
6012 TemplateTy TemplateIn,
6013 SourceLocation TemplateNameLoc,
6014 SourceLocation LAngleLoc,
6015 ASTTemplateArgsPtr TemplateArgsIn,
6016 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006017 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6018 !getLangOptions().CPlusPlus0x)
6019 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006020 << FixItHint::CreateRemoval(TypenameLoc);
6021
6022 // Translate the parser's template argument list in our AST format.
6023 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6024 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6025
6026 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006027 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6028 // Construct a dependent template specialization type.
6029 assert(DTN && "dependent template has non-dependent name?");
6030 assert(DTN->getQualifier()
6031 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6032 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6033 DTN->getQualifier(),
6034 DTN->getIdentifier(),
6035 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006036
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006037 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006038 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006039 DependentTemplateSpecializationTypeLoc SpecTL
6040 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006041 SpecTL.setLAngleLoc(LAngleLoc);
6042 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006043 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006044 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006045 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006046 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6047 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006048 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006049 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006050
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006051 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6052 if (T.isNull())
6053 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006054
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006055 // Provide source-location information for the template specialization
6056 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006057 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006058 TemplateSpecializationTypeLoc SpecTL
6059 = Builder.push<TemplateSpecializationTypeLoc>(T);
6060
6061 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006062 SpecTL.setLAngleLoc(LAngleLoc);
6063 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006064 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006065 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6066 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6067
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006068 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6069 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6070 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006071 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6072
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006073 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6074 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006075}
6076
Douglas Gregora02411e2011-02-27 22:46:49 +00006077
Douglas Gregord57959a2009-03-27 23:10:48 +00006078/// \brief Build the type that describes a C++ typename specifier,
6079/// e.g., "typename T::type".
6080QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006081Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6082 SourceLocation KeywordLoc,
6083 NestedNameSpecifierLoc QualifierLoc,
6084 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006085 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006086 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006087 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006088
John McCall77bb1aa2010-05-01 00:40:08 +00006089 DeclContext *Ctx = computeDeclContext(SS);
6090 if (!Ctx) {
6091 // If the nested-name-specifier is dependent and couldn't be
6092 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006093 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6094 return Context.getDependentNameType(Keyword,
6095 QualifierLoc.getNestedNameSpecifier(),
6096 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006097 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006098
John McCall77bb1aa2010-05-01 00:40:08 +00006099 // If the nested-name-specifier refers to the current instantiation,
6100 // the "typename" keyword itself is superfluous. In C++03, the
6101 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6102 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006103 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006104
John McCall77bb1aa2010-05-01 00:40:08 +00006105 if (RequireCompleteDeclContext(SS, Ctx))
6106 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006107
6108 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006109 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006110 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006111 unsigned DiagID = 0;
6112 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006113 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006114 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006115 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006116 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006117
6118 case LookupResult::FoundUnresolvedValue: {
6119 // We found a using declaration that is a value. Most likely, the using
6120 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006121 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006122 IILoc);
6123 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6124 << Name << Ctx << FullRange;
6125 if (UnresolvedUsingValueDecl *Using
6126 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006127 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006128 Diag(Loc, diag::note_using_value_decl_missing_typename)
6129 << FixItHint::CreateInsertion(Loc, "typename ");
6130 }
6131 }
6132 // Fall through to create a dependent typename type, from which we can recover
6133 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006134
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006135 case LookupResult::NotFoundInCurrentInstantiation:
6136 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006137 return Context.getDependentNameType(Keyword,
6138 QualifierLoc.getNestedNameSpecifier(),
6139 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006140
6141 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006142 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006143 // We found a type. Build an ElaboratedType, since the
6144 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006145 return Context.getElaboratedType(ETK_Typename,
6146 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006147 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006148 }
6149
6150 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006151 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006152 break;
6153
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006154
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006155 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006156 return QualType();
6157
Douglas Gregord57959a2009-03-27 23:10:48 +00006158 case LookupResult::FoundOverloaded:
6159 DiagID = diag::err_typename_nested_not_type;
6160 Referenced = *Result.begin();
6161 break;
6162
John McCall6e247262009-10-10 05:48:19 +00006163 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006164 return QualType();
6165 }
6166
6167 // If we get here, it's because name lookup did not find a
6168 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006169 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006170 IILoc);
6171 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006172 if (Referenced)
6173 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6174 << Name;
6175 return QualType();
6176}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006177
6178namespace {
6179 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006180 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006181 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006182 SourceLocation Loc;
6183 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006184
Douglas Gregor4a959d82009-08-06 16:20:37 +00006185 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006186 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006187
Mike Stump1eb44332009-09-09 15:08:12 +00006188 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006189 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006190 DeclarationName Entity)
6191 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006192 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006193
6194 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006195 /// transformed.
6196 ///
6197 /// For the purposes of type reconstruction, a type has already been
6198 /// transformed if it is NULL or if it is not dependent.
6199 bool AlreadyTransformed(QualType T) {
6200 return T.isNull() || !T->isDependentType();
6201 }
Mike Stump1eb44332009-09-09 15:08:12 +00006202
6203 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006204 /// rebuilt.
6205 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006206
Douglas Gregor4a959d82009-08-06 16:20:37 +00006207 /// \brief Returns the name of the entity whose type is being rebuilt.
6208 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006209
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006210 /// \brief Sets the "base" location and entity when that
6211 /// information is known based on another transformation.
6212 void setBase(SourceLocation Loc, DeclarationName Entity) {
6213 this->Loc = Loc;
6214 this->Entity = Entity;
6215 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006216 };
6217}
6218
Douglas Gregor4a959d82009-08-06 16:20:37 +00006219/// \brief Rebuilds a type within the context of the current instantiation.
6220///
Mike Stump1eb44332009-09-09 15:08:12 +00006221/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006222/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006223/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006224/// partial specialization thereof). This routine will rebuild that type now
6225/// that we have entered the declarator's scope, which may produce different
6226/// canonical types, e.g.,
6227///
6228/// \code
6229/// template<typename T>
6230/// struct X {
6231/// typedef T* pointer;
6232/// pointer data();
6233/// };
6234///
6235/// template<typename T>
6236/// typename X<T>::pointer X<T>::data() { ... }
6237/// \endcode
6238///
Douglas Gregor4714c122010-03-31 17:34:00 +00006239/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006240/// since we do not know that we can look into X<T> when we parsed the type.
6241/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006242/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006243/// as the canonical type of T*, allowing the return types of the out-of-line
6244/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006245TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6246 SourceLocation Loc,
6247 DeclarationName Name) {
6248 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006249 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006250
Douglas Gregor4a959d82009-08-06 16:20:37 +00006251 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6252 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006253}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006254
John McCall60d7b3a2010-08-24 06:29:42 +00006255ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006256 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6257 DeclarationName());
6258 return Rebuilder.TransformExpr(E);
6259}
6260
John McCall63b43852010-04-29 23:50:39 +00006261bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006262 if (SS.isInvalid())
6263 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006264
Douglas Gregor7e384942011-02-25 16:07:42 +00006265 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006266 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6267 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006268 NestedNameSpecifierLoc Rebuilt
6269 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6270 if (!Rebuilt)
6271 return true;
John McCall63b43852010-04-29 23:50:39 +00006272
Douglas Gregor7e384942011-02-25 16:07:42 +00006273 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006274 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006275}
6276
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006277/// \brief Produces a formatted string that describes the binding of
6278/// template parameters to template arguments.
6279std::string
6280Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6281 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006282 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006283}
6284
6285std::string
6286Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6287 const TemplateArgument *Args,
6288 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006289 llvm::SmallString<128> Str;
6290 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006291
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006292 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006293 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006294
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006295 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006296 if (I >= NumArgs)
6297 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006298
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006299 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006300 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006301 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006302 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006303
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006304 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006305 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006306 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006307 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006308 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006309
Douglas Gregor87dd6972010-12-20 16:52:59 +00006310 Out << " = ";
6311 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006312 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006313
6314 Out << ']';
6315 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006316}