blob: 08eb65423609be6235af2bd5da15736091bc8211 [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
John McCallf7a1a742009-11-24 19:00:30 +000079static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
John McCallad00b772010-06-16 08:42:20 +000085 NamedDecl *Repl = isAcceptableTemplateName(C, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
95 // is followed by a template-argument-list, the reference refers to the
96 // class template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
98 //
99 // FIXME: Will we eventually have to do the same for alias templates?
100 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
101 if (!ClassTemplates.insert(ClassTmpl)) {
102 filter.erase();
103 continue;
104 }
John McCall8ba66912010-08-13 07:02:08 +0000105
106 // FIXME: we promote access to public here as a workaround to
107 // the fact that LookupResult doesn't let us remember that we
108 // found this template through a particular injected class name,
109 // which means we end up doing nasty things to the invariants.
110 // Pretending that access is public is *much* safer.
111 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000112 }
John McCallf7a1a742009-11-24 19:00:30 +0000113 }
114 filter.done();
115}
116
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000117TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000118 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000119 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000120 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000121 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000122 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000123 TemplateTy &TemplateResult,
124 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000125 assert(getLangOptions().CPlusPlus && "No template names in C!");
126
Douglas Gregor014e88d2009-11-03 23:16:33 +0000127 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000128 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000129
Douglas Gregor014e88d2009-11-03 23:16:33 +0000130 switch (Name.getKind()) {
131 case UnqualifiedId::IK_Identifier:
132 TName = DeclarationName(Name.Identifier);
133 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 case UnqualifiedId::IK_OperatorFunctionId:
136 TName = Context.DeclarationNames.getCXXOperatorName(
137 Name.OperatorFunctionId.Operator);
138 break;
139
Sean Hunte6252d12009-11-28 08:58:14 +0000140 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000141 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
142 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000143
Douglas Gregor014e88d2009-11-03 23:16:33 +0000144 default:
145 return TNK_Non_template;
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
John McCallb3d87482010-08-24 05:47:05 +0000148 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000150 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000151 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000152 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
153 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000154 if (R.empty()) return TNK_Non_template;
155 if (R.isAmbiguous()) {
156 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000157 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000158
159 // FIXME: we might have ambiguous templates, in which case we
160 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000161 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000162 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000163
John McCall0bd6feb2009-12-02 08:04:21 +0000164 TemplateName Template;
165 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
John McCall0bd6feb2009-12-02 08:04:21 +0000167 unsigned ResultCount = R.end() - R.begin();
168 if (ResultCount > 1) {
169 // We assume that we'll preserve the qualifier from a function
170 // template name in other ways.
171 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
172 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000173
174 // We'll do this lookup again later.
175 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000177 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
178
179 if (SS.isSet() && !SS.isInvalid()) {
180 NestedNameSpecifier *Qualifier
181 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000182 Template = Context.getQualifiedTemplateName(Qualifier,
183 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000184 } else {
185 Template = TemplateName(TD);
186 }
187
John McCallb8592062010-08-13 02:23:42 +0000188 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000189 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000190
191 // We'll do this lookup again later.
192 R.suppressDiagnostics();
193 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000194 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
195 TemplateKind = TNK_Type_template;
196 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
John McCall0bd6feb2009-12-02 08:04:21 +0000199 TemplateResult = TemplateTy::make(Template);
200 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000201}
202
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000203bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000204 SourceLocation IILoc,
205 Scope *S,
206 const CXXScopeSpec *SS,
207 TemplateTy &SuggestedTemplate,
208 TemplateNameKind &SuggestedKind) {
209 // We can't recover unless there's a dependent scope specifier preceding the
210 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000211 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000212 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
213 computeDeclContext(*SS))
214 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000215
Douglas Gregor84d0a192010-01-12 21:28:44 +0000216 // The code is missing a 'template' keyword prior to the dependent template
217 // name.
218 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
219 Diag(IILoc, diag::err_template_kw_missing)
220 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000221 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
224 SuggestedKind = TNK_Dependent_template_name;
225 return true;
226}
227
John McCallf7a1a742009-11-24 19:00:30 +0000228void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000229 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000230 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000231 bool EnteringContext,
232 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000233 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000234 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000235 DeclContext *LookupCtx = 0;
236 bool isDependent = false;
237 if (!ObjectType.isNull()) {
238 // This nested-name-specifier occurs in a member access expression, e.g.,
239 // x->B::f, and we are looking into the type of the object.
240 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
241 LookupCtx = computeDeclContext(ObjectType);
242 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000243 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000244 "Caller should have completed object type");
245 } else if (SS.isSet()) {
246 // This nested-name-specifier occurs after another nested-name-specifier,
247 // so long into the context associated with the prior nested-name-specifier.
248 LookupCtx = computeDeclContext(SS, EnteringContext);
249 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250
John McCallf7a1a742009-11-24 19:00:30 +0000251 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000252 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000253 return;
254 }
255
256 bool ObjectTypeSearchedInScope = false;
257 if (LookupCtx) {
258 // Perform "qualified" name lookup into the declaration context we
259 // computed, which is either the type of the base of a member access
260 // expression or the declaration context associated with a prior
261 // nested-name-specifier.
262 LookupQualifiedName(Found, LookupCtx);
263
264 if (!ObjectType.isNull() && Found.empty()) {
265 // C++ [basic.lookup.classref]p1:
266 // In a class member access expression (5.2.5), if the . or -> token is
267 // immediately followed by an identifier followed by a <, the
268 // identifier must be looked up to determine whether the < is the
269 // beginning of a template argument list (14.2) or a less-than operator.
270 // The identifier is first looked up in the class of the object
271 // expression. If the identifier is not found, it is then looked up in
272 // the context of the entire postfix-expression and shall name a class
273 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000274 if (S) LookupName(Found, S);
275 ObjectTypeSearchedInScope = true;
276 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000277 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000278 // We cannot look into a dependent object type or nested nme
279 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000280 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000281 return;
282 } else {
283 // Perform unqualified name lookup in the current scope.
284 LookupName(Found, S);
285 }
286
Douglas Gregor2e933882010-01-12 17:06:20 +0000287 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000288 // If we did not find any names, attempt to correct any typos.
289 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000290 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000291 false, CTC_CXXCasts)) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000292 FilterAcceptableTemplateNames(Context, Found);
John McCallad00b772010-06-16 08:42:20 +0000293 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000294 if (LookupCtx)
295 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
296 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000297 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000298 Found.getLookupName().getAsString());
299 else
300 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
301 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000302 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000303 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000304 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
305 Diag(Template->getLocation(), diag::note_previous_decl)
306 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000307 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000308 } else {
309 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000310 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000311 }
312 }
313
John McCallf7a1a742009-11-24 19:00:30 +0000314 FilterAcceptableTemplateNames(Context, Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000315 if (Found.empty()) {
316 if (isDependent)
317 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000318 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000319 }
John McCallf7a1a742009-11-24 19:00:30 +0000320
321 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
322 // C++ [basic.lookup.classref]p1:
323 // [...] If the lookup in the class of the object expression finds a
324 // template, the name is also looked up in the context of the entire
325 // postfix-expression and [...]
326 //
327 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
328 LookupOrdinaryName);
329 LookupName(FoundOuter, S);
330 FilterAcceptableTemplateNames(Context, FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000331
John McCallf7a1a742009-11-24 19:00:30 +0000332 if (FoundOuter.empty()) {
333 // - if the name is not found, the name found in the class of the
334 // object expression is used, otherwise
335 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
336 // - if the name is found in the context of the entire
337 // postfix-expression and does not name a class template, the name
338 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000339 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000340 // - if the name found is a class template, it must refer to the same
341 // entity as the one found in the class of the object expression,
342 // otherwise the program is ill-formed.
343 if (!Found.isSingleResult() ||
344 Found.getFoundDecl()->getCanonicalDecl()
345 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000346 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000347 diag::ext_nested_name_member_ref_lookup_ambiguous)
348 << Found.getLookupName()
349 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000350 Diag(Found.getRepresentativeDecl()->getLocation(),
351 diag::note_ambig_member_ref_object_type)
352 << ObjectType;
353 Diag(FoundOuter.getFoundDecl()->getLocation(),
354 diag::note_ambig_member_ref_scope);
355
356 // Recover by taking the template that we found in the object
357 // expression's type.
358 }
359 }
360 }
361}
362
John McCall2f841ba2009-12-02 03:53:29 +0000363/// ActOnDependentIdExpression - Handle a dependent id-expression that
364/// was just parsed. This is only possible with an explicit scope
365/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000366ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000367Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000369 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000371 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000372
John McCall2f841ba2009-12-02 03:53:29 +0000373 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000374 isa<CXXMethodDecl>(DC) &&
375 cast<CXXMethodDecl>(DC)->isInstance()) {
376 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000377
John McCallf7a1a742009-11-24 19:00:30 +0000378 // Since the 'this' expression is synthesized, we don't need to
379 // perform the double-lookup check.
380 NamedDecl *FirstQualifierInScope = 0;
381
John McCallaa81e162009-12-01 22:10:20 +0000382 return Owned(CXXDependentScopeMemberExpr::Create(Context,
383 /*This*/ 0, ThisType,
384 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000385 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000386 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000387 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000388 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000389 TemplateArgs));
390 }
391
Abramo Bagnara25777432010-08-11 22:01:17 +0000392 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000393}
394
John McCall60d7b3a2010-08-24 06:29:42 +0000395ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000396Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000397 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000398 const TemplateArgumentListInfo *TemplateArgs) {
399 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000400 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000401 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000402 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000403}
404
Douglas Gregor72c3f312008-12-05 18:15:24 +0000405/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
406/// that the template parameter 'PrevDecl' is being shadowed by a new
407/// declaration at location Loc. Returns true to indicate that this is
408/// an error, and false otherwise.
409bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000410 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000411
412 // Microsoft Visual C++ permits template parameters to be shadowed.
413 if (getLangOptions().Microsoft)
414 return false;
415
416 // C++ [temp.local]p4:
417 // A template-parameter shall not be redeclared within its
418 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000419 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000420 << cast<NamedDecl>(PrevDecl)->getDeclName();
421 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
422 return true;
423}
424
Douglas Gregor2943aed2009-03-03 04:44:36 +0000425/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000426/// the parameter D to reference the templated declaration and return a pointer
427/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000428TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
429 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
430 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000431 return Temp;
432 }
433 return 0;
434}
435
Douglas Gregorba68eca2011-01-05 17:40:24 +0000436ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
437 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000438 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000439 "Only template template arguments can be pack expansions here");
440 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
441 "Template template argument pack expansion without packs");
442 ParsedTemplateArgument Result(*this);
443 Result.EllipsisLoc = EllipsisLoc;
444 return Result;
445}
446
Douglas Gregor788cd062009-11-11 01:00:40 +0000447static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
448 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000449
Douglas Gregor788cd062009-11-11 01:00:40 +0000450 switch (Arg.getKind()) {
451 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000452 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000453 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000454 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000455 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000456 return TemplateArgumentLoc(TemplateArgument(T), DI);
457 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000458
Douglas Gregor788cd062009-11-11 01:00:40 +0000459 case ParsedTemplateArgument::NonType: {
460 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
461 return TemplateArgumentLoc(TemplateArgument(E), E);
462 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000465 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000466 TemplateArgument TArg;
467 if (Arg.getEllipsisLoc().isValid())
468 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
469 else
470 TArg = Template;
471 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000472 Arg.getScopeSpec().getWithLocInContext(
473 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000474 Arg.getLocation(),
475 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000476 }
477 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000478
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000479 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000480 return TemplateArgumentLoc();
481}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000482
Douglas Gregor788cd062009-11-11 01:00:40 +0000483/// \brief Translates template arguments as provided by the parser
484/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000485void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
486 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000487 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000488 TemplateArgs.addArgument(translateTemplateArgument(*this,
489 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000490}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000491
Douglas Gregor72c3f312008-12-05 18:15:24 +0000492/// ActOnTypeParameter - Called when a C++ template type parameter
493/// (e.g., "typename T") has been parsed. Typename specifies whether
494/// the keyword "typename" was used to declare the type parameter
495/// (otherwise, "class" was used), and KeyLoc is the location of the
496/// "class" or "typename" keyword. ParamName is the name of the
497/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000498/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000499/// If the type parameter has a default argument, it will be added
500/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000501Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
502 SourceLocation EllipsisLoc,
503 SourceLocation KeyLoc,
504 IdentifierInfo *ParamName,
505 SourceLocation ParamNameLoc,
506 unsigned Depth, unsigned Position,
507 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000508 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000509 assert(S->isTemplateParamScope() &&
510 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000511 bool Invalid = false;
512
513 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000514 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000515 LookupOrdinaryName,
516 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000517 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000518 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000519 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000520 }
521
Douglas Gregorddc29e12009-02-06 22:42:48 +0000522 SourceLocation Loc = ParamNameLoc;
523 if (!ParamName)
524 Loc = KeyLoc;
525
Douglas Gregor72c3f312008-12-05 18:15:24 +0000526 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000527 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000528 KeyLoc, Loc, Depth, Position, ParamName,
529 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000530 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000531 if (Invalid)
532 Param->setInvalidDecl();
533
534 if (ParamName) {
535 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000536 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000537 IdResolver.AddDecl(Param);
538 }
539
Douglas Gregor61c4d282011-01-05 15:48:55 +0000540 // C++0x [temp.param]p9:
541 // A default template-argument may be specified for any kind of
542 // template-parameter that is not a template parameter pack.
543 if (DefaultArg && Ellipsis) {
544 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
545 DefaultArg = ParsedType();
546 }
547
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000548 // Handle the default argument, if provided.
549 if (DefaultArg) {
550 TypeSourceInfo *DefaultTInfo;
551 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000552
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000553 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000554
Douglas Gregor6f526752010-12-16 08:48:57 +0000555 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000556 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000557 UPPC_DefaultArgument))
558 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000559
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000560 // Check the template argument itself.
561 if (CheckTemplateArgument(Param, DefaultTInfo)) {
562 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000563 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000564 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000565
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000566 Param->setDefaultArgument(DefaultTInfo, false);
567 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000568
John McCalld226f652010-08-21 09:40:31 +0000569 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000570}
571
Douglas Gregor2943aed2009-03-03 04:44:36 +0000572/// \brief Check that the type of a non-type template parameter is
573/// well-formed.
574///
575/// \returns the (possibly-promoted) parameter type if valid;
576/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000577QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000578Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000579 // We don't allow variably-modified types as the type of non-type template
580 // parameters.
581 if (T->isVariablyModifiedType()) {
582 Diag(Loc, diag::err_variably_modified_nontype_template_param)
583 << T;
584 return QualType();
585 }
586
Douglas Gregor2943aed2009-03-03 04:44:36 +0000587 // C++ [temp.param]p4:
588 //
589 // A non-type template-parameter shall have one of the following
590 // (optionally cv-qualified) types:
591 //
592 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000593 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000594 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000595 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000596 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000597 T->isReferenceType() ||
598 // -- pointer to member.
599 T->isMemberPointerType() ||
600 // If T is a dependent type, we can't do the check now, so we
601 // assume that it is well-formed.
602 T->isDependentType())
603 return T;
604 // C++ [temp.param]p8:
605 //
606 // A non-type template-parameter of type "array of T" or
607 // "function returning T" is adjusted to be of type "pointer to
608 // T" or "pointer to function returning T", respectively.
609 else if (T->isArrayType())
610 // FIXME: Keep the type prior to promotion?
611 return Context.getArrayDecayedType(T);
612 else if (T->isFunctionType())
613 // FIXME: Keep the type prior to promotion?
614 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000615
Douglas Gregor2943aed2009-03-03 04:44:36 +0000616 Diag(Loc, diag::err_template_nontype_parm_bad_type)
617 << T;
618
619 return QualType();
620}
621
John McCalld226f652010-08-21 09:40:31 +0000622Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
623 unsigned Depth,
624 unsigned Position,
625 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000626 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000627 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
628 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000629
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000630 assert(S->isTemplateParamScope() &&
631 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000632 bool Invalid = false;
633
634 IdentifierInfo *ParamName = D.getIdentifier();
635 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000636 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000637 LookupOrdinaryName,
638 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000639 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000640 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000641 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000642 }
643
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000644 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
645 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000646 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000647 Invalid = true;
648 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000649
Douglas Gregor10738d32010-12-23 23:51:58 +0000650 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000651 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000652 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000653 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000654 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000655 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000656 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000657 Param->setAccess(AS_public);
658
Douglas Gregor72c3f312008-12-05 18:15:24 +0000659 if (Invalid)
660 Param->setInvalidDecl();
661
662 if (D.getIdentifier()) {
663 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000664 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000665 IdResolver.AddDecl(Param);
666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000667
Douglas Gregor61c4d282011-01-05 15:48:55 +0000668 // C++0x [temp.param]p9:
669 // A default template-argument may be specified for any kind of
670 // template-parameter that is not a template parameter pack.
671 if (Default && IsParameterPack) {
672 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
673 Default = 0;
674 }
675
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000676 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000677 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000678 // Check for unexpanded parameter packs.
679 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
680 return Param;
681
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000682 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000683 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
684 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000685 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000686 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000687 }
John Wiegley429bb272011-04-08 18:41:53 +0000688 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000689
John McCall9ae2f072010-08-23 23:25:46 +0000690 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000691 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000692
John McCalld226f652010-08-21 09:40:31 +0000693 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000694}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000695
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000696/// ActOnTemplateTemplateParameter - Called when a C++ template template
697/// parameter (e.g. T in template <template <typename> class T> class array)
698/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000699Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
700 SourceLocation TmpLoc,
701 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000702 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000703 IdentifierInfo *Name,
704 SourceLocation NameLoc,
705 unsigned Depth,
706 unsigned Position,
707 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000708 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000709 assert(S->isTemplateParamScope() &&
710 "Template template parameter not in template parameter scope!");
711
712 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000713 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000714 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000715 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000716 NameLoc.isInvalid()? TmpLoc : NameLoc,
717 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000718 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000719 Param->setAccess(AS_public);
720
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000721 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000722 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000723 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000724 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000725 IdResolver.AddDecl(Param);
726 }
727
Douglas Gregor6f526752010-12-16 08:48:57 +0000728 if (Params->size() == 0) {
729 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
730 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
731 Param->setInvalidDecl();
732 }
733
Douglas Gregor61c4d282011-01-05 15:48:55 +0000734 // C++0x [temp.param]p9:
735 // A default template-argument may be specified for any kind of
736 // template-parameter that is not a template parameter pack.
737 if (IsParameterPack && !Default.isInvalid()) {
738 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
739 Default = ParsedTemplateArgument();
740 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000741
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000742 if (!Default.isInvalid()) {
743 // Check only that we have a template template argument. We don't want to
744 // try to check well-formedness now, because our template template parameter
745 // might have dependent types in its template parameters, which we wouldn't
746 // be able to match now.
747 //
748 // If none of the template template parameter's template arguments mention
749 // other template parameters, we could actually perform more checking here.
750 // However, it isn't worth doing.
751 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
752 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
753 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
754 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000755 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000756 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757
Douglas Gregor6f526752010-12-16 08:48:57 +0000758 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000759 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000760 DefaultArg.getArgument().getAsTemplate(),
761 UPPC_DefaultArgument))
762 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000763
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000764 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000765 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000766
John McCalld226f652010-08-21 09:40:31 +0000767 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000768}
769
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000770/// ActOnTemplateParameterList - Builds a TemplateParameterList that
771/// contains the template parameters in Params/NumParams.
772Sema::TemplateParamsTy *
773Sema::ActOnTemplateParameterList(unsigned Depth,
774 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000775 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000776 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000777 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000778 SourceLocation RAngleLoc) {
779 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000780 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000781
Douglas Gregorddc29e12009-02-06 22:42:48 +0000782 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000783 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000784 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000785}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000786
John McCallb6217662010-03-15 10:12:16 +0000787static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
788 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000789 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000790}
791
John McCallf312b1e2010-08-26 23:41:50 +0000792DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000793Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000794 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000795 IdentifierInfo *Name, SourceLocation NameLoc,
796 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000797 TemplateParameterList *TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000798 AccessSpecifier AS,
799 unsigned NumOuterTemplateParamLists,
800 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000801 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000802 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000803 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000804 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000805
806 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000807 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000808 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000809
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000810 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
811 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000812
813 // There is no such thing as an unnamed class template.
814 if (!Name) {
815 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000816 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000817 }
818
819 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000820 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000821 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000822 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000823 if (SS.isNotEmpty() && !SS.isInvalid()) {
824 SemanticContext = computeDeclContext(SS, true);
825 if (!SemanticContext) {
826 // FIXME: Produce a reasonable diagnostic here
827 return true;
828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
John McCall77bb1aa2010-05-01 00:40:08 +0000830 if (RequireCompleteDeclContext(SS, SemanticContext))
831 return true;
832
John McCalla24dc2e2009-11-17 02:14:36 +0000833 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000834 } else {
835 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000836 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Douglas Gregor57265e32010-04-12 16:00:01 +0000839 if (Previous.isAmbiguous())
840 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000841
Douglas Gregorddc29e12009-02-06 22:42:48 +0000842 NamedDecl *PrevDecl = 0;
843 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000844 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000845
Douglas Gregorddc29e12009-02-06 22:42:48 +0000846 // If there is a previous declaration with the same name, check
847 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000848 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000849 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000850
851 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000852 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000853 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000854 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000855 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
856 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000857 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000858 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
859 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
860 PrevClassTemplate
861 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
862 ->getSpecializedTemplate();
863 }
864 }
865
John McCall65c49462009-12-18 11:25:59 +0000866 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000867 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000868 // [...] When looking for a prior declaration of a class or a function
869 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000870 // function is neither a qualified name nor a template-id, scopes outside
871 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000872 if (!SS.isSet()) {
873 DeclContext *OutermostContext = CurContext;
874 while (!OutermostContext->isFileContext())
875 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000876
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000877 if (PrevDecl &&
878 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
879 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
880 SemanticContext = PrevDecl->getDeclContext();
881 } else {
882 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000883 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000884 // declaration.
885 PrevDecl = PrevClassTemplate = 0;
886 SemanticContext = OutermostContext;
887 }
John McCalle129d442009-12-17 23:21:11 +0000888 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000889
John McCalle129d442009-12-17 23:21:11 +0000890 if (CurContext->isDependentContext()) {
891 // If this is a dependent context, we don't want to link the friend
892 // class template to the template in scope, because that would perform
893 // checking of the template parameter lists that can't be performed
894 // until the outer context is instantiated.
895 PrevDecl = PrevClassTemplate = 0;
896 }
897 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
898 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000899
Douglas Gregorddc29e12009-02-06 22:42:48 +0000900 if (PrevClassTemplate) {
901 // Ensure that the template parameter lists are compatible.
902 if (!TemplateParameterListsAreEqual(TemplateParams,
903 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000904 /*Complain=*/true,
905 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000906 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000907
908 // C++ [temp.class]p4:
909 // In a redeclaration, partial specialization, explicit
910 // specialization or explicit instantiation of a class template,
911 // the class-key shall agree in kind with the original class
912 // template declaration (7.1.5.3).
913 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000914 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000915 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000916 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000917 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000918 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000919 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000920 }
921
Douglas Gregorddc29e12009-02-06 22:42:48 +0000922 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000923 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000924 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000925 Diag(NameLoc, diag::err_redefinition) << Name;
926 Diag(Def->getLocation(), diag::note_previous_definition);
927 // FIXME: Would it make sense to try to "forget" the previous
928 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000929 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000930 }
931 }
932 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
933 // Maybe we will complain about the shadowed template parameter.
934 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
935 // Just pretend that we didn't see the previous declaration.
936 PrevDecl = 0;
937 } else if (PrevDecl) {
938 // C++ [temp]p5:
939 // A class template shall not have the same name as any other
940 // template, class, function, object, enumeration, enumerator,
941 // namespace, or type in the same scope (3.3), except as specified
942 // in (14.5.4).
943 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
944 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000945 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000946 }
947
Douglas Gregord684b002009-02-10 19:49:53 +0000948 // Check the template parameter list of this declaration, possibly
949 // merging in the template parameter list from the previous class
950 // template declaration.
951 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000952 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000953 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000954 SemanticContext->isRecord() &&
955 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000956 ? TPC_ClassTemplateMember
957 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000958 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregor57265e32010-04-12 16:00:01 +0000960 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000961 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000962 // template out-of-line.
963 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
964 !(TUK == TUK_Friend && CurContext->isDependentContext()))
965 Diag(NameLoc, diag::err_member_def_does_not_match)
966 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000967 }
968
Mike Stump1eb44332009-09-09 15:08:12 +0000969 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000970 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000971 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000972 PrevClassTemplate->getTemplatedDecl() : 0,
973 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000974 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000975 if (NumOuterTemplateParamLists > 0)
976 NewClass->setTemplateParameterListsInfo(Context,
977 NumOuterTemplateParamLists,
978 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000979
980 ClassTemplateDecl *NewTemplate
981 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
982 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000983 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000984 NewClass->setDescribedClassTemplate(NewTemplate);
985
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000986 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000987 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000988 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000989 assert(T->isDependentType() && "Class template type is not dependent?");
990 (void)T;
991
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000992 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000993 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000994 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000995 PrevClassTemplate->getInstantiatedFromMemberTemplate())
996 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000997
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000998 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000999 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001000 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregorddc29e12009-02-06 22:42:48 +00001002 // Set the lexical context of these templates
1003 NewClass->setLexicalDeclContext(CurContext);
1004 NewTemplate->setLexicalDeclContext(CurContext);
1005
John McCall0f434ec2009-07-31 02:45:11 +00001006 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001007 NewClass->startDefinition();
1008
1009 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001010 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001011
John McCall05b23ea2009-09-14 21:59:20 +00001012 if (TUK != TUK_Friend)
1013 PushOnScopeChains(NewTemplate, S);
1014 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001015 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001016 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001017 NewClass->setAccess(PrevClassTemplate->getAccess());
1018 }
John McCall05b23ea2009-09-14 21:59:20 +00001019
Douglas Gregord85bea22009-09-26 06:47:28 +00001020 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1021 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001022
John McCall05b23ea2009-09-14 21:59:20 +00001023 // Friend templates are visible in fairly strange ways.
1024 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001025 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001026 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1027 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1028 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001029 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001030 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001031
Douglas Gregord85bea22009-09-26 06:47:28 +00001032 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1033 NewClass->getLocation(),
1034 NewTemplate,
1035 /*FIXME:*/NewClass->getLocation());
1036 Friend->setAccess(AS_public);
1037 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001038 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001039
Douglas Gregord684b002009-02-10 19:49:53 +00001040 if (Invalid) {
1041 NewTemplate->setInvalidDecl();
1042 NewClass->setInvalidDecl();
1043 }
John McCalld226f652010-08-21 09:40:31 +00001044 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001045}
1046
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001047/// \brief Diagnose the presence of a default template argument on a
1048/// template parameter, which is ill-formed in certain contexts.
1049///
1050/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001051static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001052 Sema::TemplateParamListContext TPC,
1053 SourceLocation ParamLoc,
1054 SourceRange DefArgRange) {
1055 switch (TPC) {
1056 case Sema::TPC_ClassTemplate:
1057 return false;
1058
1059 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001060 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001061 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001062 // A default template-argument shall not be specified in a
1063 // function template declaration or a function template
1064 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001065 // If a friend function template declaration specifies a default
1066 // template-argument, that declaration shall be a definition and shall be
1067 // the only declaration of the function template in the translation unit.
1068 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001069 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001070 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001071 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001072 << DefArgRange;
1073 return false;
1074
1075 case Sema::TPC_ClassTemplateMember:
1076 // C++0x [temp.param]p9:
1077 // A default template-argument shall not be specified in the
1078 // template-parameter-lists of the definition of a member of a
1079 // class template that appears outside of the member's class.
1080 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1081 << DefArgRange;
1082 return true;
1083
1084 case Sema::TPC_FriendFunctionTemplate:
1085 // C++ [temp.param]p9:
1086 // A default template-argument shall not be specified in a
1087 // friend template declaration.
1088 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1089 << DefArgRange;
1090 return true;
1091
1092 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1093 // for friend function templates if there is only a single
1094 // declaration (and it is a definition). Strange!
1095 }
1096
1097 return false;
1098}
1099
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001100/// \brief Check for unexpanded parameter packs within the template parameters
1101/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001102static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1103 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001104 TemplateParameterList *Params = TTP->getTemplateParameters();
1105 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1106 NamedDecl *P = Params->getParam(I);
1107 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001108 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001109 NTTP->getTypeSourceInfo(),
1110 Sema::UPPC_NonTypeTemplateParameterType))
1111 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001112
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001113 continue;
1114 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001115
1116 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001117 = dyn_cast<TemplateTemplateParmDecl>(P))
1118 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1119 return true;
1120 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001121
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001122 return false;
1123}
1124
Douglas Gregord684b002009-02-10 19:49:53 +00001125/// \brief Checks the validity of a template parameter list, possibly
1126/// considering the template parameter list from a previous
1127/// declaration.
1128///
1129/// If an "old" template parameter list is provided, it must be
1130/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1131/// template parameter list.
1132///
1133/// \param NewParams Template parameter list for a new template
1134/// declaration. This template parameter list will be updated with any
1135/// default arguments that are carried through from the previous
1136/// template parameter list.
1137///
1138/// \param OldParams If provided, template parameter list from a
1139/// previous declaration of the same template. Default template
1140/// arguments will be merged from the old template parameter list to
1141/// the new template parameter list.
1142///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001143/// \param TPC Describes the context in which we are checking the given
1144/// template parameter list.
1145///
Douglas Gregord684b002009-02-10 19:49:53 +00001146/// \returns true if an error occurred, false otherwise.
1147bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001148 TemplateParameterList *OldParams,
1149 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001150 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Douglas Gregord684b002009-02-10 19:49:53 +00001152 // C++ [temp.param]p10:
1153 // The set of default template-arguments available for use with a
1154 // template declaration or definition is obtained by merging the
1155 // default arguments from the definition (if in scope) and all
1156 // declarations in scope in the same way default function
1157 // arguments are (8.3.6).
1158 bool SawDefaultArgument = false;
1159 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001160
Anders Carlsson49d25572009-06-12 23:20:15 +00001161 bool SawParameterPack = false;
1162 SourceLocation ParameterPackLoc;
1163
Mike Stump1a35fde2009-02-11 23:03:27 +00001164 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001165 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001166 if (OldParams)
1167 OldParam = OldParams->begin();
1168
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001169 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001170 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1171 NewParamEnd = NewParams->end();
1172 NewParam != NewParamEnd; ++NewParam) {
1173 // Variables used to diagnose redundant default arguments
1174 bool RedundantDefaultArg = false;
1175 SourceLocation OldDefaultLoc;
1176 SourceLocation NewDefaultLoc;
1177
1178 // Variables used to diagnose missing default arguments
1179 bool MissingDefaultArg = false;
1180
Anders Carlsson49d25572009-06-12 23:20:15 +00001181 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001182 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001183 // parameter pack, it shall be the last template parameter.
1184 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001185 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001186 diag::err_template_param_pack_must_be_last_template_parameter);
1187 Invalid = true;
1188 }
1189
Douglas Gregord684b002009-02-10 19:49:53 +00001190 if (TemplateTypeParmDecl *NewTypeParm
1191 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001192 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001193 if (NewTypeParm->hasDefaultArgument() &&
1194 DiagnoseDefaultTemplateArgument(*this, TPC,
1195 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001196 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001197 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001198 NewTypeParm->removeDefaultArgument();
1199
1200 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001201 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001202 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Anders Carlsson49d25572009-06-12 23:20:15 +00001204 if (NewTypeParm->isParameterPack()) {
1205 assert(!NewTypeParm->hasDefaultArgument() &&
1206 "Parameter packs can't have a default argument!");
1207 SawParameterPack = true;
1208 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001209 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001210 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001211 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1212 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1213 SawDefaultArgument = true;
1214 RedundantDefaultArg = true;
1215 PreviousDefaultArgLoc = NewDefaultLoc;
1216 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1217 // Merge the default argument from the old declaration to the
1218 // new declaration.
1219 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001220 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001221 true);
1222 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1223 } else if (NewTypeParm->hasDefaultArgument()) {
1224 SawDefaultArgument = true;
1225 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1226 } else if (SawDefaultArgument)
1227 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001228 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001229 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001230 // Check for unexpanded parameter packs.
1231 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001232 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001233 UPPC_NonTypeTemplateParameterType)) {
1234 Invalid = true;
1235 continue;
1236 }
1237
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001238 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001239 if (NewNonTypeParm->hasDefaultArgument() &&
1240 DiagnoseDefaultTemplateArgument(*this, TPC,
1241 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001242 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001243 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001244 }
1245
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001246 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001247 NonTypeTemplateParmDecl *OldNonTypeParm
1248 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001249 if (NewNonTypeParm->isParameterPack()) {
1250 assert(!NewNonTypeParm->hasDefaultArgument() &&
1251 "Parameter packs can't have a default argument!");
1252 SawParameterPack = true;
1253 ParameterPackLoc = NewNonTypeParm->getLocation();
1254 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001255 NewNonTypeParm->hasDefaultArgument()) {
1256 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1257 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1258 SawDefaultArgument = true;
1259 RedundantDefaultArg = true;
1260 PreviousDefaultArgLoc = NewDefaultLoc;
1261 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1262 // Merge the default argument from the old declaration to the
1263 // new declaration.
1264 SawDefaultArgument = true;
1265 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001266 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001267 // parameter.
1268 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001269 OldNonTypeParm->getDefaultArgument(),
1270 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001271 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1272 } else if (NewNonTypeParm->hasDefaultArgument()) {
1273 SawDefaultArgument = true;
1274 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1275 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001276 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001277 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001278 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001279 TemplateTemplateParmDecl *NewTemplateParm
1280 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001281
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001282 // Check for unexpanded parameter packs, recursively.
1283 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1284 Invalid = true;
1285 continue;
1286 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001287
1288 if (NewTemplateParm->hasDefaultArgument() &&
1289 DiagnoseDefaultTemplateArgument(*this, TPC,
1290 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001291 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001292 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001293
1294 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001295 TemplateTemplateParmDecl *OldTemplateParm
1296 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001297 if (NewTemplateParm->isParameterPack()) {
1298 assert(!NewTemplateParm->hasDefaultArgument() &&
1299 "Parameter packs can't have a default argument!");
1300 SawParameterPack = true;
1301 ParameterPackLoc = NewTemplateParm->getLocation();
1302 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001303 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001304 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1305 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001306 SawDefaultArgument = true;
1307 RedundantDefaultArg = true;
1308 PreviousDefaultArgLoc = NewDefaultLoc;
1309 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1310 // Merge the default argument from the old declaration to the
1311 // new declaration.
1312 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001313 // FIXME: We need to create a new kind of "default argument" expression
1314 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001315 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001316 OldTemplateParm->getDefaultArgument(),
1317 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001318 PreviousDefaultArgLoc
1319 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001320 } else if (NewTemplateParm->hasDefaultArgument()) {
1321 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001322 PreviousDefaultArgLoc
1323 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001324 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001325 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001326 }
1327
1328 if (RedundantDefaultArg) {
1329 // C++ [temp.param]p12:
1330 // A template-parameter shall not be given default arguments
1331 // by two different declarations in the same scope.
1332 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1333 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1334 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001335 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001336 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001337 // If a template-parameter of a class template has a default
1338 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001339 // have a default template-argument supplied or be a template parameter
1340 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001341 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001342 diag::err_template_param_default_arg_missing);
1343 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1344 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001345 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001346 }
1347
1348 // If we have an old template parameter list that we're merging
1349 // in, move on to the next parameter.
1350 if (OldParams)
1351 ++OldParam;
1352 }
1353
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001354 // We were missing some default arguments at the end of the list, so remove
1355 // all of the default arguments.
1356 if (RemoveDefaultArguments) {
1357 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1358 NewParamEnd = NewParams->end();
1359 NewParam != NewParamEnd; ++NewParam) {
1360 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1361 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001362 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001363 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1364 NTTP->removeDefaultArgument();
1365 else
1366 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1367 }
1368 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001369
Douglas Gregord684b002009-02-10 19:49:53 +00001370 return Invalid;
1371}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001372
John McCall4e2cbb22010-10-20 05:44:58 +00001373namespace {
1374
1375/// A class which looks for a use of a certain level of template
1376/// parameter.
1377struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1378 typedef RecursiveASTVisitor<DependencyChecker> super;
1379
1380 unsigned Depth;
1381 bool Match;
1382
1383 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1384 NamedDecl *ND = Params->getParam(0);
1385 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1386 Depth = PD->getDepth();
1387 } else if (NonTypeTemplateParmDecl *PD =
1388 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1389 Depth = PD->getDepth();
1390 } else {
1391 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1392 }
1393 }
1394
1395 bool Matches(unsigned ParmDepth) {
1396 if (ParmDepth >= Depth) {
1397 Match = true;
1398 return true;
1399 }
1400 return false;
1401 }
1402
1403 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1404 return !Matches(T->getDepth());
1405 }
1406
1407 bool TraverseTemplateName(TemplateName N) {
1408 if (TemplateTemplateParmDecl *PD =
1409 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1410 if (Matches(PD->getDepth())) return false;
1411 return super::TraverseTemplateName(N);
1412 }
1413
1414 bool VisitDeclRefExpr(DeclRefExpr *E) {
1415 if (NonTypeTemplateParmDecl *PD =
1416 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1417 if (PD->getDepth() == Depth) {
1418 Match = true;
1419 return false;
1420 }
1421 }
1422 return super::VisitDeclRefExpr(E);
1423 }
1424};
1425}
1426
1427/// Determines whether a template-id depends on the given parameter
1428/// list.
1429static bool
1430DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1431 TemplateParameterList *Params) {
1432 DependencyChecker Checker(Params);
1433 Checker.TraverseType(QualType(TemplateId, 0));
1434 return Checker.Match;
1435}
1436
Mike Stump1eb44332009-09-09 15:08:12 +00001437/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001438/// specifier, returning the template parameter list that applies to the
1439/// name.
1440///
1441/// \param DeclStartLoc the start of the declaration that has a scope
1442/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001443///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001444/// \param SS the scope specifier that will be matched to the given template
1445/// parameter lists. This scope specifier precedes a qualified name that is
1446/// being declared.
1447///
1448/// \param ParamLists the template parameter lists, from the outermost to the
1449/// innermost template parameter lists.
1450///
1451/// \param NumParamLists the number of template parameter lists in ParamLists.
1452///
John McCall77e8b112010-04-13 20:37:33 +00001453/// \param IsFriend Whether to apply the slightly different rules for
1454/// matching template parameters to scope specifiers in friend
1455/// declarations.
1456///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001457/// \param IsExplicitSpecialization will be set true if the entity being
1458/// declared is an explicit specialization, false otherwise.
1459///
Mike Stump1eb44332009-09-09 15:08:12 +00001460/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001461/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001462/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001463/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001464/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001465/// itself a template).
1466TemplateParameterList *
1467Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1468 const CXXScopeSpec &SS,
1469 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001470 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001471 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001472 bool &IsExplicitSpecialization,
1473 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001474 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001475
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001476 // Find the template-ids that occur within the nested-name-specifier. These
1477 // template-ids will match up with the template parameter lists.
1478 llvm::SmallVector<const TemplateSpecializationType *, 4>
1479 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001480 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1481 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001482 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1483 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001484 const Type *T = NNS->getAsType();
1485 if (!T) break;
1486
1487 // C++0x [temp.expl.spec]p17:
1488 // A member or a member template may be nested within many
1489 // enclosing class templates. In an explicit specialization for
1490 // such a member, the member declaration shall be preceded by a
1491 // template<> for each enclosing class template that is
1492 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001493 //
1494 // Following the existing practice of GNU and EDG, we allow a typedef of a
1495 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001496 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1497 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001498
Mike Stump1eb44332009-09-09 15:08:12 +00001499 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001500 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001501 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1502 if (!Template)
1503 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Ted Kremenek6217b802009-07-29 21:53:49 +00001505 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001506 ClassTemplateSpecializationDecl *SpecDecl
1507 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1508 // If the nested name specifier refers to an explicit specialization,
1509 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001510 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1511 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001512 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001513 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001514 }
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001516 TemplateIdsInSpecifier.push_back(SpecType);
1517 }
1518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001520 // Reverse the list of template-ids in the scope specifier, so that we can
1521 // more easily match up the template-ids and the template parameter lists.
1522 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001524 SourceLocation FirstTemplateLoc = DeclStartLoc;
1525 if (NumParamLists)
1526 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001528 // Match the template-ids found in the specifier to the template parameter
1529 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001530 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001531 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001532 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1533 const TemplateSpecializationType *TemplateId
1534 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001535 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001536
1537 // In friend declarations we can have template-ids which don't
1538 // depend on the corresponding template parameter lists. But
1539 // assume that empty parameter lists are supposed to match this
1540 // template-id.
1541 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1542 if (!DependentTemplateId ||
1543 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1544 continue;
1545 }
1546
1547 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001548 // We have a template-id without a corresponding template parameter
1549 // list.
John McCall77e8b112010-04-13 20:37:33 +00001550
1551 // ...which is fine if this is a friend declaration.
1552 if (IsFriend) {
1553 IsExplicitSpecialization = true;
1554 break;
1555 }
1556
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001557 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001558 // FIXME: the location information here isn't great.
1559 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001560 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001561 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001562 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001563 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001564 } else {
1565 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1566 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001567 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001568 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001569 }
1570 return 0;
1571 }
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001573 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001574 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001575 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001576
John McCall31f17ec2010-04-27 00:57:59 +00001577 // Are there cases in (e.g.) friends where this won't match?
1578 if (const InjectedClassNameType *Injected
1579 = TemplateId->getAs<InjectedClassNameType>()) {
1580 CXXRecordDecl *Record = Injected->getDecl();
1581 if (ClassTemplatePartialSpecializationDecl *Partial =
1582 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1583 ExpectedTemplateParams = Partial->getTemplateParameters();
1584 else
1585 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1586 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001587 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001588
John McCall31f17ec2010-04-27 00:57:59 +00001589 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001590 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001591 ExpectedTemplateParams,
1592 true, TPL_TemplateMatch);
1593
John McCall4e2cbb22010-10-20 05:44:58 +00001594 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1595 TPC_ClassTemplateMember);
1596 } else if (ParamLists[ParamIdx]->size() > 0)
1597 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001598 diag::err_template_param_list_matches_nontemplate)
1599 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001600 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001601 else
1602 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001603
1604 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001605 }
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001607 // If there were at least as many template-ids as there were template
1608 // parameter lists, then there are no template parameter lists remaining for
1609 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001610 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001611 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001613 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001614 if (ParamIdx != NumParamLists - 1) {
1615 while (ParamIdx < NumParamLists - 1) {
1616 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1617 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001618 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1619 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001620 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1621 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001622
1623 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1624 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1625 diag::note_explicit_template_spec_does_not_need_header)
1626 << ExplicitSpecializationsInSpecifier.back();
1627 ExplicitSpecializationsInSpecifier.pop_back();
1628 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001629
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001630 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001631 // means that the resulting template declaration can't be instantiated
1632 // properly (we'll end up with dependent nodes when we shouldn't).
1633 if (!isExplicitSpecHeader)
1634 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001635
John McCall4e2cbb22010-10-20 05:44:58 +00001636 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001637 }
1638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001640 // Return the last template parameter list, which corresponds to the
1641 // entity being declared.
1642 return ParamLists[NumParamLists - 1];
1643}
1644
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001645void Sema::NoteAllFoundTemplates(TemplateName Name) {
1646 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1647 Diag(Template->getLocation(), diag::note_template_declared_here)
1648 << (isa<FunctionTemplateDecl>(Template)? 0
1649 : isa<ClassTemplateDecl>(Template)? 1
1650 : 2)
1651 << Template->getDeclName();
1652 return;
1653 }
1654
1655 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1656 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1657 IEnd = OST->end();
1658 I != IEnd; ++I)
1659 Diag((*I)->getLocation(), diag::note_template_declared_here)
1660 << 0 << (*I)->getDeclName();
1661
1662 return;
1663 }
1664}
1665
1666
Douglas Gregor7532dc62009-03-30 22:58:21 +00001667QualType Sema::CheckTemplateIdType(TemplateName Name,
1668 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001669 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001670 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001671 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1672 // We might have a substituted template template parameter pack. If so,
1673 // build a template specialization type for it.
1674 if (Name.getAsSubstTemplateTemplateParmPack())
1675 return Context.getTemplateSpecializationType(Name, TemplateArgs);
1676
1677 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1678 << Name;
1679 NoteAllFoundTemplates(Name);
1680 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001681 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001682
Douglas Gregor40808ce2009-03-09 23:48:35 +00001683 // Check that the template argument list is well-formed for this
1684 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001685 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001686 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001687 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001688 return QualType();
1689
Douglas Gregor910f8002010-11-07 23:05:16 +00001690 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001691 "Converted template argument list is too short!");
1692
1693 QualType CanonType;
1694
Douglas Gregorcaddba02009-11-12 18:38:13 +00001695 if (Name.isDependent() ||
1696 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001697 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001698 // This class template specialization is a dependent
1699 // type. Therefore, its canonical type is another class template
1700 // specialization type that contains all of the converted
1701 // arguments in canonical form. This ensures that, e.g., A<T> and
1702 // A<T, T> have identical types when A is declared as:
1703 //
1704 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001705 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001706 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001707 Converted.data(),
1708 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Douglas Gregor1275ae02009-07-28 23:00:59 +00001710 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001711 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001712 // In the future, we need to teach getTemplateSpecializationType to only
1713 // build the canonical type and return that to us.
1714 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001715
1716 // This might work out to be a current instantiation, in which
1717 // case the canonical type needs to be the InjectedClassNameType.
1718 //
1719 // TODO: in theory this could be a simple hashtable lookup; most
1720 // changes to CurContext don't change the set of current
1721 // instantiations.
1722 if (isa<ClassTemplateDecl>(Template)) {
1723 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1724 // If we get out to a namespace, we're done.
1725 if (Ctx->isFileContext()) break;
1726
1727 // If this isn't a record, keep looking.
1728 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1729 if (!Record) continue;
1730
1731 // Look for one of the two cases with InjectedClassNameTypes
1732 // and check whether it's the same template.
1733 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1734 !Record->getDescribedClassTemplate())
1735 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001736
John McCall31f17ec2010-04-27 00:57:59 +00001737 // Fetch the injected class name type and check whether its
1738 // injected type is equal to the type we just built.
1739 QualType ICNT = Context.getTypeDeclType(Record);
1740 QualType Injected = cast<InjectedClassNameType>(ICNT)
1741 ->getInjectedSpecializationType();
1742
1743 if (CanonType != Injected->getCanonicalTypeInternal())
1744 continue;
1745
1746 // If so, the canonical type of this TST is the injected
1747 // class name type of the record we just found.
1748 assert(ICNT.isCanonical());
1749 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001750 break;
1751 }
1752 }
Mike Stump1eb44332009-09-09 15:08:12 +00001753 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001754 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001755 // Find the class template specialization declaration that
1756 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001757 void *InsertPos = 0;
1758 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001759 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001760 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001761 if (!Decl) {
1762 // This is the first time we have referenced this class template
1763 // specialization. Create the canonical declaration and add it to
1764 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001765 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001766 ClassTemplate->getTemplatedDecl()->getTagKind(),
1767 ClassTemplate->getDeclContext(),
1768 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001769 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001770 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001771 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001772 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001773 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001774 Decl->setLexicalDeclContext(CurContext);
1775 }
1776
1777 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001778 assert(isa<RecordType>(CanonType) &&
1779 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001780 }
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Douglas Gregor40808ce2009-03-09 23:48:35 +00001782 // Build the fully-sugared type for this class template
1783 // specialization, which refers back to the class template
1784 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001785 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001786}
1787
John McCallf312b1e2010-08-26 23:41:50 +00001788TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001789Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1790 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001791 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001792 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001793 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001794 if (SS.isInvalid())
1795 return true;
1796
Douglas Gregor7532dc62009-03-30 22:58:21 +00001797 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001798
Douglas Gregor40808ce2009-03-09 23:48:35 +00001799 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001800 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001801 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001802
Douglas Gregora88f09f2011-02-28 17:23:35 +00001803 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1804 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1805 DTN->getQualifier(),
1806 DTN->getIdentifier(),
1807 TemplateArgs);
1808
1809 // Build type-source information.
1810 TypeLocBuilder TLB;
1811 DependentTemplateSpecializationTypeLoc SpecTL
1812 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001813 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001814 SpecTL.setNameLoc(TemplateLoc);
1815 SpecTL.setLAngleLoc(LAngleLoc);
1816 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001817 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001818 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1819 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1820 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1821 }
1822
John McCalld5532b62009-11-23 01:53:49 +00001823 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001824 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001825
1826 if (Result.isNull())
1827 return true;
1828
Douglas Gregor059101f2011-03-02 00:47:37 +00001829 // Build type-source information.
1830 TypeLocBuilder TLB;
1831 TemplateSpecializationTypeLoc SpecTL
1832 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1833 SpecTL.setTemplateNameLoc(TemplateLoc);
1834 SpecTL.setLAngleLoc(LAngleLoc);
1835 SpecTL.setRAngleLoc(RAngleLoc);
1836 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1837 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001838
Douglas Gregor059101f2011-03-02 00:47:37 +00001839 if (SS.isNotEmpty()) {
1840 // Create an elaborated-type-specifier containing the nested-name-specifier.
1841 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1842 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1843 ElabTL.setKeywordLoc(SourceLocation());
1844 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1845 }
1846
1847 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001848}
John McCallf1bbbb42009-09-04 01:14:41 +00001849
Douglas Gregor059101f2011-03-02 00:47:37 +00001850TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001851 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001852 SourceLocation TagLoc,
1853 CXXScopeSpec &SS,
1854 TemplateTy TemplateD,
1855 SourceLocation TemplateLoc,
1856 SourceLocation LAngleLoc,
1857 ASTTemplateArgsPtr TemplateArgsIn,
1858 SourceLocation RAngleLoc) {
1859 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1860
1861 // Translate the parser's template argument list in our AST format.
1862 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1863 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1864
1865 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001866 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001867 ElaboratedTypeKeyword Keyword
1868 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Douglas Gregor059101f2011-03-02 00:47:37 +00001870 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1871 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1872 DTN->getQualifier(),
1873 DTN->getIdentifier(),
1874 TemplateArgs);
1875
1876 // Build type-source information.
1877 TypeLocBuilder TLB;
1878 DependentTemplateSpecializationTypeLoc SpecTL
1879 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1880 SpecTL.setKeywordLoc(TagLoc);
1881 SpecTL.setNameLoc(TemplateLoc);
1882 SpecTL.setLAngleLoc(LAngleLoc);
1883 SpecTL.setRAngleLoc(RAngleLoc);
1884 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1885 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1886 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1887 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1888 }
1889
1890 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1891 if (Result.isNull())
1892 return TypeResult();
1893
1894 // Check the tag kind
1895 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001896 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001897
John McCall6b2becf2009-09-08 17:47:29 +00001898 IdentifierInfo *Id = D->getIdentifier();
1899 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001900
John McCall6b2becf2009-09-08 17:47:29 +00001901 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1902 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001903 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001904 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001905 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001906 }
1907 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001908
1909 // Provide source-location information for the template specialization.
1910 TypeLocBuilder TLB;
1911 TemplateSpecializationTypeLoc SpecTL
1912 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1913 SpecTL.setTemplateNameLoc(TemplateLoc);
1914 SpecTL.setLAngleLoc(LAngleLoc);
1915 SpecTL.setRAngleLoc(RAngleLoc);
1916 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1917 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001918
Douglas Gregor059101f2011-03-02 00:47:37 +00001919 // Construct an elaborated type containing the nested-name-specifier (if any)
1920 // and keyword.
1921 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1922 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1923 ElabTL.setKeywordLoc(TagLoc);
1924 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1925 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001926}
1927
John McCall60d7b3a2010-08-24 06:29:42 +00001928ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001929 LookupResult &R,
1930 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001931 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001932 // FIXME: Can we do any checking at this point? I guess we could check the
1933 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001934 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001935 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001936 // foo<int> could identify a single function unambiguously
1937 // This approach does NOT work, since f<int>(1);
1938 // gets resolved prior to resorting to overload resolution
1939 // i.e., template<class T> void f(double);
1940 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001941
1942 // These should be filtered out by our callers.
1943 assert(!R.empty() && "empty lookup results when building templateid");
1944 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1945
John McCallc373d482010-01-27 01:50:18 +00001946 // We don't want lookup warnings at this point.
1947 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001948
John McCallf7a1a742009-11-24 19:00:30 +00001949 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001950 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001951 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001952 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001953 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001954 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001955
1956 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001957}
1958
John McCallf7a1a742009-11-24 19:00:30 +00001959// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001960ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001961Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001962 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001963 const TemplateArgumentListInfo &TemplateArgs) {
1964 DeclContext *DC;
1965 if (!(DC = computeDeclContext(SS, false)) ||
1966 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001967 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001968 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001970 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001971 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001972 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1973 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001974
John McCallf7a1a742009-11-24 19:00:30 +00001975 if (R.isAmbiguous())
1976 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001977
John McCallf7a1a742009-11-24 19:00:30 +00001978 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001979 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1980 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001981 return ExprError();
1982 }
1983
1984 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001985 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1986 << (NestedNameSpecifier*) SS.getScopeRep()
1987 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001988 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1989 return ExprError();
1990 }
1991
1992 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001993}
1994
Douglas Gregorc45c2322009-03-31 00:43:58 +00001995/// \brief Form a dependent template name.
1996///
1997/// This action forms a dependent template name given the template
1998/// name and its (presumably dependent) scope specifier. For
1999/// example, given "MetaFun::template apply", the scope specifier \p
2000/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2001/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002002TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002003 SourceLocation TemplateKWLoc,
2004 CXXScopeSpec &SS,
2005 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002006 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002007 bool EnteringContext,
2008 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002009 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2010 !getLangOptions().CPlusPlus0x)
2011 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002012 << FixItHint::CreateRemoval(TemplateKWLoc);
2013
Douglas Gregor0707bc52010-01-19 16:01:07 +00002014 DeclContext *LookupCtx = 0;
2015 if (SS.isSet())
2016 LookupCtx = computeDeclContext(SS, EnteringContext);
2017 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002018 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002019 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002020 // C++0x [temp.names]p5:
2021 // If a name prefixed by the keyword template is not the name of
2022 // a template, the program is ill-formed. [Note: the keyword
2023 // template may not be applied to non-template members of class
2024 // templates. -end note ] [ Note: as is the case with the
2025 // typename prefix, the template prefix is allowed in cases
2026 // where it is not strictly necessary; i.e., when the
2027 // nested-name-specifier or the expression on the left of the ->
2028 // or . is not dependent on a template-parameter, or the use
2029 // does not appear in the scope of a template. -end note]
2030 //
2031 // Note: C++03 was more strict here, because it banned the use of
2032 // the "template" keyword prior to a template-name that was not a
2033 // dependent name. C++ DR468 relaxed this requirement (the
2034 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002035 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002036 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002037 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2038 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002039 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002040 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2041 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002042 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2043 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002044 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002045 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002046 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002047 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002048 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002049 << Name.getSourceRange()
2050 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002051 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002052 } else {
2053 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002054 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002055 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002056 }
2057
Mike Stump1eb44332009-09-09 15:08:12 +00002058 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002059 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002060
Douglas Gregor014e88d2009-11-03 23:16:33 +00002061 switch (Name.getKind()) {
2062 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002063 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002064 Name.Identifier));
2065 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002066
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002067 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002068 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002069 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002070 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002071
2072 case UnqualifiedId::IK_LiteralOperatorId:
2073 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2074
Douglas Gregor014e88d2009-11-03 23:16:33 +00002075 default:
2076 break;
2077 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002078
2079 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002080 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002081 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002082 << Name.getSourceRange()
2083 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002084 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002085}
2086
Mike Stump1eb44332009-09-09 15:08:12 +00002087bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002088 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002089 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002090 const TemplateArgument &Arg = AL.getArgument();
2091
Anders Carlsson436b1562009-06-13 00:33:33 +00002092 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002093 switch(Arg.getKind()) {
2094 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002095 // C++ [temp.arg.type]p1:
2096 // A template-argument for a template-parameter which is a
2097 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002098 break;
2099 case TemplateArgument::Template: {
2100 // We have a template type parameter but the template argument
2101 // is a template without any arguments.
2102 SourceRange SR = AL.getSourceRange();
2103 TemplateName Name = Arg.getAsTemplate();
2104 Diag(SR.getBegin(), diag::err_template_missing_args)
2105 << Name << SR;
2106 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2107 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002108
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002109 return true;
2110 }
2111 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002112 // We have a template type parameter but the template argument
2113 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002114 SourceRange SR = AL.getSourceRange();
2115 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002116 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Anders Carlsson436b1562009-06-13 00:33:33 +00002118 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002119 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002120 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002121
John McCalla93c9342009-12-07 02:54:59 +00002122 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002123 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002124
Anders Carlsson436b1562009-06-13 00:33:33 +00002125 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002126 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002127 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002128 return false;
2129}
2130
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002131/// \brief Substitute template arguments into the default template argument for
2132/// the given template type parameter.
2133///
2134/// \param SemaRef the semantic analysis object for which we are performing
2135/// the substitution.
2136///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002137/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002138/// for.
2139///
2140/// \param TemplateLoc the location of the template name that started the
2141/// template-id we are checking.
2142///
2143/// \param RAngleLoc the location of the right angle bracket ('>') that
2144/// terminates the template-id.
2145///
2146/// \param Param the template template parameter whose default we are
2147/// substituting into.
2148///
2149/// \param Converted the list of template arguments provided for template
2150/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002151/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002152static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002153SubstDefaultTemplateArgument(Sema &SemaRef,
2154 TemplateDecl *Template,
2155 SourceLocation TemplateLoc,
2156 SourceLocation RAngleLoc,
2157 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002158 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002159 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002160
2161 // If the argument type is dependent, instantiate it now based
2162 // on the previously-computed template arguments.
2163 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002164 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002165 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002166
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002167 MultiLevelTemplateArgumentList AllTemplateArgs
2168 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2169
2170 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002171 Template, Converted.data(),
2172 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002173 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002174
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002175 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2176 Param->getDefaultArgumentLoc(),
2177 Param->getDeclName());
2178 }
2179
2180 return ArgType;
2181}
2182
2183/// \brief Substitute template arguments into the default template argument for
2184/// the given non-type 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 Gregor0f8716b2009-11-09 19:17:50 +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///
Douglas Gregor788cd062009-11-11 01:00:40 +00002198/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002199/// 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///
2204/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002205static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002206SubstDefaultTemplateArgument(Sema &SemaRef,
2207 TemplateDecl *Template,
2208 SourceLocation TemplateLoc,
2209 SourceLocation RAngleLoc,
2210 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002211 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002212 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002213 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002214
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002215 MultiLevelTemplateArgumentList AllTemplateArgs
2216 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002217
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002218 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002219 Template, Converted.data(),
2220 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002221 SourceRange(TemplateLoc, RAngleLoc));
2222
2223 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2224}
2225
Douglas Gregor788cd062009-11-11 01:00:40 +00002226/// \brief Substitute template arguments into the default template argument for
2227/// the given template template parameter.
2228///
2229/// \param SemaRef the semantic analysis object for which we are performing
2230/// the substitution.
2231///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002232/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002233/// for.
2234///
2235/// \param TemplateLoc the location of the template name that started the
2236/// template-id we are checking.
2237///
2238/// \param RAngleLoc the location of the right angle bracket ('>') that
2239/// terminates the template-id.
2240///
2241/// \param Param the template template parameter whose default we are
2242/// substituting into.
2243///
2244/// \param Converted the list of template arguments provided for template
2245/// parameters that precede \p Param in the template parameter list.
2246///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002247/// \param QualifierLoc Will be set to the nested-name-specifier (with
2248/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002249///
Douglas Gregor788cd062009-11-11 01:00:40 +00002250/// \returns the substituted template argument, or NULL if an error occurred.
2251static TemplateName
2252SubstDefaultTemplateArgument(Sema &SemaRef,
2253 TemplateDecl *Template,
2254 SourceLocation TemplateLoc,
2255 SourceLocation RAngleLoc,
2256 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002257 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2258 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002259 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002260 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002261
Douglas Gregor788cd062009-11-11 01:00:40 +00002262 MultiLevelTemplateArgumentList AllTemplateArgs
2263 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002264
Douglas Gregor788cd062009-11-11 01:00:40 +00002265 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002266 Template, Converted.data(),
2267 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002268 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002269
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002270 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002271 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002272 if (QualifierLoc) {
2273 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2274 AllTemplateArgs);
2275 if (!QualifierLoc)
2276 return TemplateName();
2277 }
2278
Douglas Gregor1d752d72011-03-02 18:46:51 +00002279 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002280 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002281 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002282 AllTemplateArgs);
2283}
2284
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002285/// \brief If the given template parameter has a default template
2286/// argument, substitute into that default template argument and
2287/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002288TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002289Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2290 SourceLocation TemplateLoc,
2291 SourceLocation RAngleLoc,
2292 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002293 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2294 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002295 if (!TypeParm->hasDefaultArgument())
2296 return TemplateArgumentLoc();
2297
John McCalla93c9342009-12-07 02:54:59 +00002298 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002299 TemplateLoc,
2300 RAngleLoc,
2301 TypeParm,
2302 Converted);
2303 if (DI)
2304 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2305
2306 return TemplateArgumentLoc();
2307 }
2308
2309 if (NonTypeTemplateParmDecl *NonTypeParm
2310 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2311 if (!NonTypeParm->hasDefaultArgument())
2312 return TemplateArgumentLoc();
2313
John McCall60d7b3a2010-08-24 06:29:42 +00002314 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002315 TemplateLoc,
2316 RAngleLoc,
2317 NonTypeParm,
2318 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002319 if (Arg.isInvalid())
2320 return TemplateArgumentLoc();
2321
2322 Expr *ArgE = Arg.takeAs<Expr>();
2323 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2324 }
2325
2326 TemplateTemplateParmDecl *TempTempParm
2327 = cast<TemplateTemplateParmDecl>(Param);
2328 if (!TempTempParm->hasDefaultArgument())
2329 return TemplateArgumentLoc();
2330
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002331
Douglas Gregor1d752d72011-03-02 18:46:51 +00002332 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002333 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002334 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002335 RAngleLoc,
2336 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002337 Converted,
2338 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002339 if (TName.isNull())
2340 return TemplateArgumentLoc();
2341
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002343 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002344 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2345}
2346
Douglas Gregore7526412009-11-11 19:31:23 +00002347/// \brief Check that the given template argument corresponds to the given
2348/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002349///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002350/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002351/// checked.
2352///
2353/// \param Arg The template argument.
2354///
2355/// \param Template The template in which the template argument resides.
2356///
2357/// \param TemplateLoc The location of the template name for the template
2358/// whose argument list we're matching.
2359///
2360/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2361/// the template argument list.
2362///
2363/// \param ArgumentPackIndex The index into the argument pack where this
2364/// argument will be placed. Only valid if the parameter is a parameter pack.
2365///
2366/// \param Converted The checked, converted argument will be added to the
2367/// end of this small vector.
2368///
2369/// \param CTAK Describes how we arrived at this particular template argument:
2370/// explicitly written, deduced, etc.
2371///
2372/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002373bool Sema::CheckTemplateArgument(NamedDecl *Param,
2374 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002375 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002376 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002377 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002378 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002379 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002380 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002381 // Check template type parameters.
2382 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002383 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002384
Douglas Gregord9e15302009-11-11 19:41:09 +00002385 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002386 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002387 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002388 // with the template arguments we've seen thus far. But if the
2389 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002390 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002391 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2392 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002393
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002394 if (NTTPType->isDependentType() &&
2395 !isa<TemplateTemplateParmDecl>(Template) &&
2396 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002397 // Do substitution on the type of the non-type template parameter.
2398 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002399 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002400 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002401
2402 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002403 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002404 NTTPType = SubstType(NTTPType,
2405 MultiLevelTemplateArgumentList(TemplateArgs),
2406 NTTP->getLocation(),
2407 NTTP->getDeclName());
2408 // If that worked, check the non-type template parameter type
2409 // for validity.
2410 if (!NTTPType.isNull())
2411 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2412 NTTP->getLocation());
2413 if (NTTPType.isNull())
2414 return true;
2415 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002416
Douglas Gregore7526412009-11-11 19:31:23 +00002417 switch (Arg.getArgument().getKind()) {
2418 case TemplateArgument::Null:
2419 assert(false && "Should never see a NULL template argument here");
2420 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002421
Douglas Gregore7526412009-11-11 19:31:23 +00002422 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002423 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002424 ExprResult Res =
2425 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2426 Result, CTAK);
2427 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002428 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002429
Douglas Gregor910f8002010-11-07 23:05:16 +00002430 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002431 break;
2432 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002433
Douglas Gregore7526412009-11-11 19:31:23 +00002434 case TemplateArgument::Declaration:
2435 case TemplateArgument::Integral:
2436 // We've already checked this template argument, so just copy
2437 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002438 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002439 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002440
Douglas Gregore7526412009-11-11 19:31:23 +00002441 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002442 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002443 // We were given a template template argument. It may not be ill-formed;
2444 // see below.
2445 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002446 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2447 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002448 // We have a template argument such as \c T::template X, which we
2449 // parsed as a template template argument. However, since we now
2450 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002451 // template name into an expression.
2452
2453 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2454 Arg.getTemplateNameLoc());
2455
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002456 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002457 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002458 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002459 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002460 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002461
Douglas Gregora7fc9012011-01-05 18:58:31 +00002462 // If we parsed the template argument as a pack expansion, create a
2463 // pack expansion expression.
2464 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002465 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2466 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002467 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002468 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002469
Douglas Gregore7526412009-11-11 19:31:23 +00002470 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002471 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2472 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002473 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002474
Douglas Gregor910f8002010-11-07 23:05:16 +00002475 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002476 break;
2477 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002478
Douglas Gregore7526412009-11-11 19:31:23 +00002479 // We have a template argument that actually does refer to a class
2480 // template, template alias, or template template parameter, and
2481 // therefore cannot be a non-type template argument.
2482 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2483 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002484
Douglas Gregore7526412009-11-11 19:31:23 +00002485 Diag(Param->getLocation(), diag::note_template_param_here);
2486 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002487
Douglas Gregore7526412009-11-11 19:31:23 +00002488 case TemplateArgument::Type: {
2489 // We have a non-type template parameter but the template
2490 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002491
Douglas Gregore7526412009-11-11 19:31:23 +00002492 // C++ [temp.arg]p2:
2493 // In a template-argument, an ambiguity between a type-id and
2494 // an expression is resolved to a type-id, regardless of the
2495 // form of the corresponding template-parameter.
2496 //
2497 // We warn specifically about this case, since it can be rather
2498 // confusing for users.
2499 QualType T = Arg.getArgument().getAsType();
2500 SourceRange SR = Arg.getSourceRange();
2501 if (T->isFunctionType())
2502 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2503 else
2504 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2505 Diag(Param->getLocation(), diag::note_template_param_here);
2506 return true;
2507 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002508
Douglas Gregore7526412009-11-11 19:31:23 +00002509 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002510 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002511 break;
2512 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002513
Douglas Gregore7526412009-11-11 19:31:23 +00002514 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002515 }
2516
2517
Douglas Gregore7526412009-11-11 19:31:23 +00002518 // Check template template parameters.
2519 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002520
Douglas Gregore7526412009-11-11 19:31:23 +00002521 // Substitute into the template parameter list of the template
2522 // template parameter, since previously-supplied template arguments
2523 // may appear within the template template parameter.
2524 {
2525 // Set up a template instantiation context.
2526 LocalInstantiationScope Scope(*this);
2527 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002528 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002529 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002530
2531 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002532 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002533 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002534 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002535 MultiLevelTemplateArgumentList(TemplateArgs)));
2536 if (!TempParm)
2537 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002538 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002539
Douglas Gregore7526412009-11-11 19:31:23 +00002540 switch (Arg.getArgument().getKind()) {
2541 case TemplateArgument::Null:
2542 assert(false && "Should never see a NULL template argument here");
2543 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002544
Douglas Gregore7526412009-11-11 19:31:23 +00002545 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002546 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002547 if (CheckTemplateArgument(TempParm, Arg))
2548 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002549
Douglas Gregor910f8002010-11-07 23:05:16 +00002550 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002551 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002552
Douglas Gregore7526412009-11-11 19:31:23 +00002553 case TemplateArgument::Expression:
2554 case TemplateArgument::Type:
2555 // We have a template template parameter but the template
2556 // argument does not refer to a template.
2557 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2558 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002559
Douglas Gregore7526412009-11-11 19:31:23 +00002560 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002561 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002562 "Declaration argument with template template parameter");
2563 break;
2564 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002565 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002566 "Integral argument with template template parameter");
2567 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002568
Douglas Gregore7526412009-11-11 19:31:23 +00002569 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002570 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002571 break;
2572 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002573
Douglas Gregore7526412009-11-11 19:31:23 +00002574 return false;
2575}
2576
Douglas Gregorc15cb382009-02-09 23:23:08 +00002577/// \brief Check that the given template argument list is well-formed
2578/// for specializing the given template.
2579bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2580 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002581 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002582 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002583 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002584 TemplateParameterList *Params = Template->getTemplateParameters();
2585 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002586 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002587 bool Invalid = false;
2588
John McCalld5532b62009-11-23 01:53:49 +00002589 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2590
Mike Stump1eb44332009-09-09 15:08:12 +00002591 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002592 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002593
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002594 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002595 (NumArgs < Params->getMinRequiredArguments() &&
2596 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002597 // FIXME: point at either the first arg beyond what we can handle,
2598 // or the '>', depending on whether we have too many or too few
2599 // arguments.
2600 SourceRange Range;
2601 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002602 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002603 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2604 << (NumArgs > NumParams)
2605 << (isa<ClassTemplateDecl>(Template)? 0 :
2606 isa<FunctionTemplateDecl>(Template)? 1 :
2607 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2608 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002609 Diag(Template->getLocation(), diag::note_template_decl_here)
2610 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002611 Invalid = true;
2612 }
Mike Stump1eb44332009-09-09 15:08:12 +00002613
2614 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002615 // [...] The type and form of each template-argument specified in
2616 // a template-id shall match the type and form specified for the
2617 // corresponding parameter declared by the template in its
2618 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002619 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002620 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2621 TemplateParameterList::iterator Param = Params->begin(),
2622 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002623 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002624 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002625 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002626 if (ArgIdx > NumArgs && PartialTemplateArgs)
2627 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Douglas Gregorf35f8282009-11-11 21:54:23 +00002629 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002630 // If we have an expanded parameter pack, make sure we don't have too
2631 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002632 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002633 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002634 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002635 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2636 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2637 << true
2638 << (isa<ClassTemplateDecl>(Template)? 0 :
2639 isa<FunctionTemplateDecl>(Template)? 1 :
2640 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2641 << Template;
2642 Diag(Template->getLocation(), diag::note_template_decl_here)
2643 << Params->getSourceRange();
2644 return true;
2645 }
2646 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002647
Douglas Gregorf35f8282009-11-11 21:54:23 +00002648 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002649 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2650 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002651 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002652 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653
Douglas Gregor14be16b2010-12-20 16:57:52 +00002654 if ((*Param)->isTemplateParameterPack()) {
2655 // The template parameter was a template parameter pack, so take the
2656 // deduced argument and place it on the argument pack. Note that we
2657 // stay on the same template parameter so that we can deduce more
2658 // arguments.
2659 ArgumentPack.push_back(Converted.back());
2660 Converted.pop_back();
2661 } else {
2662 // Move to the next template parameter.
2663 ++Param;
2664 }
2665 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002666 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002667 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002668
2669 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002670 // arguments, just break out now and we'll fill in the argument pack below.
2671 if ((*Param)->isTemplateParameterPack())
2672 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002673
Douglas Gregorf35f8282009-11-11 21:54:23 +00002674 // We have a default template argument that we will use.
2675 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002676
Douglas Gregorf35f8282009-11-11 21:54:23 +00002677 // Retrieve the default template argument from the template
2678 // parameter. For each kind of template parameter, we substitute the
2679 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002680 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002681 // the default argument.
2682 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2683 if (!TTP->hasDefaultArgument()) {
2684 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2685 break;
2686 }
2687
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002688 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002689 Template,
2690 TemplateLoc,
2691 RAngleLoc,
2692 TTP,
2693 Converted);
2694 if (!ArgType)
2695 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696
Douglas Gregorf35f8282009-11-11 21:54:23 +00002697 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2698 ArgType);
2699 } else if (NonTypeTemplateParmDecl *NTTP
2700 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2701 if (!NTTP->hasDefaultArgument()) {
2702 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2703 break;
2704 }
2705
John McCall60d7b3a2010-08-24 06:29:42 +00002706 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002707 TemplateLoc,
2708 RAngleLoc,
2709 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002710 Converted);
2711 if (E.isInvalid())
2712 return true;
2713
2714 Expr *Ex = E.takeAs<Expr>();
2715 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2716 } else {
2717 TemplateTemplateParmDecl *TempParm
2718 = cast<TemplateTemplateParmDecl>(*Param);
2719
2720 if (!TempParm->hasDefaultArgument()) {
2721 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2722 break;
2723 }
2724
Douglas Gregor1d752d72011-03-02 18:46:51 +00002725 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002726 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727 TemplateLoc,
2728 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002729 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002730 Converted,
2731 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002732 if (Name.isNull())
2733 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002734
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002735 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2736 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002737 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002738
Douglas Gregorf35f8282009-11-11 21:54:23 +00002739 // Introduce an instantiation record that describes where we are using
2740 // the default template argument.
2741 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002742 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002743 SourceRange(TemplateLoc, RAngleLoc));
2744
Douglas Gregorf35f8282009-11-11 21:54:23 +00002745 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002746 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002747 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002748 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002749
Douglas Gregor67714232011-03-03 02:41:12 +00002750 // Core issue 150 (assumed resolution): if this is a template template
2751 // parameter, keep track of the default template arguments from the
2752 // template definition.
2753 if (isTemplateTemplateParameter)
2754 TemplateArgs.addArgument(Arg);
2755
Douglas Gregor14be16b2010-12-20 16:57:52 +00002756 // Move to the next template parameter and argument.
2757 ++Param;
2758 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002759 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002760
Douglas Gregor14be16b2010-12-20 16:57:52 +00002761 // Form argument packs for each of the parameter packs remaining.
2762 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002763 // If we're checking a partial list of template arguments, don't fill
2764 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002765
2766 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002767 if (PartialTemplateArgs && ArgumentPack.empty()) {
2768 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002769 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002770 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002771 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002772 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2773 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002774 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002775 ArgumentPack.clear();
2776 }
2777 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002778
Douglas Gregor14be16b2010-12-20 16:57:52 +00002779 ++Param;
2780 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002781
Douglas Gregorc15cb382009-02-09 23:23:08 +00002782 return Invalid;
2783}
2784
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002785namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002786 class UnnamedLocalNoLinkageFinder
2787 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002788 {
2789 Sema &S;
2790 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002791
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002792 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002793
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002794 public:
2795 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2796
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002797 bool Visit(QualType T) {
2798 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002799 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002800
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002801#define TYPE(Class, Parent) \
2802 bool Visit##Class##Type(const Class##Type *);
2803#define ABSTRACT_TYPE(Class, Parent) \
2804 bool Visit##Class##Type(const Class##Type *) { return false; }
2805#define NON_CANONICAL_TYPE(Class, Parent) \
2806 bool Visit##Class##Type(const Class##Type *) { return false; }
2807#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002808
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002809 bool VisitTagDecl(const TagDecl *Tag);
2810 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2811 };
2812}
2813
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002814bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002815 return false;
2816}
2817
2818bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2819 return Visit(T->getElementType());
2820}
2821
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002822bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002823 return Visit(T->getPointeeType());
2824}
2825
2826bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002827 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002828 return Visit(T->getPointeeType());
2829}
2830
2831bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002832 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002833 return Visit(T->getPointeeType());
2834}
2835
2836bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002837 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002838 return Visit(T->getPointeeType());
2839}
2840
2841bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002842 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002843 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2844}
2845
2846bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002847 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002848 return Visit(T->getElementType());
2849}
2850
2851bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002852 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002853 return Visit(T->getElementType());
2854}
2855
2856bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002857 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002858 return Visit(T->getElementType());
2859}
2860
2861bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002862 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002863 return Visit(T->getElementType());
2864}
2865
2866bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002867 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002868 return Visit(T->getElementType());
2869}
2870
2871bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2872 return Visit(T->getElementType());
2873}
2874
2875bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2876 return Visit(T->getElementType());
2877}
2878
2879bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2880 const FunctionProtoType* T) {
2881 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002882 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002883 A != AEnd; ++A) {
2884 if (Visit(*A))
2885 return true;
2886 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002887
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002888 return Visit(T->getResultType());
2889}
2890
2891bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2892 const FunctionNoProtoType* T) {
2893 return Visit(T->getResultType());
2894}
2895
2896bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2897 const UnresolvedUsingType*) {
2898 return false;
2899}
2900
2901bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2902 return false;
2903}
2904
2905bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2906 return Visit(T->getUnderlyingType());
2907}
2908
2909bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2910 return false;
2911}
2912
Richard Smith34b41d92011-02-20 03:19:35 +00002913bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2914 return Visit(T->getDeducedType());
2915}
2916
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002917bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2918 return VisitTagDecl(T->getDecl());
2919}
2920
2921bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2922 return VisitTagDecl(T->getDecl());
2923}
2924
2925bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2926 const TemplateTypeParmType*) {
2927 return false;
2928}
2929
Douglas Gregorc3069d62011-01-14 02:55:32 +00002930bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2931 const SubstTemplateTypeParmPackType *) {
2932 return false;
2933}
2934
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002935bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2936 const TemplateSpecializationType*) {
2937 return false;
2938}
2939
2940bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2941 const InjectedClassNameType* T) {
2942 return VisitTagDecl(T->getDecl());
2943}
2944
2945bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2946 const DependentNameType* T) {
2947 return VisitNestedNameSpecifier(T->getQualifier());
2948}
2949
2950bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2951 const DependentTemplateSpecializationType* T) {
2952 return VisitNestedNameSpecifier(T->getQualifier());
2953}
2954
Douglas Gregor7536dd52010-12-20 02:24:11 +00002955bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2956 const PackExpansionType* T) {
2957 return Visit(T->getPattern());
2958}
2959
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002960bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2961 return false;
2962}
2963
2964bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2965 const ObjCInterfaceType *) {
2966 return false;
2967}
2968
2969bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2970 const ObjCObjectPointerType *) {
2971 return false;
2972}
2973
2974bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2975 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2976 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2977 << S.Context.getTypeDeclType(Tag) << SR;
2978 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002979 }
2980
Richard Smith162e1c12011-04-15 14:24:37 +00002981 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002982 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2983 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2984 return true;
2985 }
2986
2987 return false;
2988}
2989
2990bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2991 NestedNameSpecifier *NNS) {
2992 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2993 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002994
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002995 switch (NNS->getKind()) {
2996 case NestedNameSpecifier::Identifier:
2997 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002998 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002999 case NestedNameSpecifier::Global:
3000 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003001
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003002 case NestedNameSpecifier::TypeSpec:
3003 case NestedNameSpecifier::TypeSpecWithTemplate:
3004 return Visit(QualType(NNS->getAsType(), 0));
3005 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003006 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003007}
3008
3009
Douglas Gregorc15cb382009-02-09 23:23:08 +00003010/// \brief Check a template argument against its corresponding
3011/// template type parameter.
3012///
3013/// This routine implements the semantics of C++ [temp.arg.type]. It
3014/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003015bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003016 TypeSourceInfo *ArgInfo) {
3017 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003018 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003019 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003020
3021 if (Arg->isVariablyModifiedType()) {
3022 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003023 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003024 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003025 }
3026
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003027 // C++03 [temp.arg.type]p2:
3028 // A local type, a type with no linkage, an unnamed type or a type
3029 // compounded from any of these types shall not be used as a
3030 // template-argument for a template type-parameter.
3031 //
3032 // C++0x allows these, and even in C++03 we allow them as an extension with
3033 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003034 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003035 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3036 (void)Finder.Visit(Context.getCanonicalType(Arg));
3037 }
3038
Douglas Gregorc15cb382009-02-09 23:23:08 +00003039 return false;
3040}
3041
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003042/// \brief Checks whether the given template argument is the address
3043/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003044static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003045CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3046 NonTypeTemplateParmDecl *Param,
3047 QualType ParamType,
3048 Expr *ArgIn,
3049 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003050 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003051 Expr *Arg = ArgIn;
3052 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003053
3054 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003055 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003056 Arg = Cast->getSubExpr();
3057
3058 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003059 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003060 // A template-argument for a non-type, non-template
3061 // template-parameter shall be one of: [...]
3062 //
3063 // -- the address of an object or function with external
3064 // linkage, including function templates and function
3065 // template-ids but excluding non-static class members,
3066 // expressed as & id-expression where the & is optional if
3067 // the name refers to a function or array, or if the
3068 // corresponding template-parameter is a reference; or
3069 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003070
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003071 // In C++98/03 mode, give an extension warning on any extra parentheses.
3072 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3073 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003074 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003075 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003076 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003077 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003078 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003079 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003080 }
3081
3082 Arg = Parens->getSubExpr();
3083 }
3084
Douglas Gregorb7a09262010-04-01 18:32:35 +00003085 bool AddressTaken = false;
3086 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003087 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003088 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003089 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003090 AddressTaken = true;
3091 AddrOpLoc = UnOp->getOperatorLoc();
3092 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003093 } else
3094 DRE = dyn_cast<DeclRefExpr>(Arg);
3095
Douglas Gregorb7a09262010-04-01 18:32:35 +00003096 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003097 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3098 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003099 S.Diag(Param->getLocation(), diag::note_template_param_here);
3100 return true;
3101 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003102
3103 // Stop checking the precise nature of the argument if it is value dependent,
3104 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003105 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003106 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003107 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003108 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003109
Douglas Gregorb7a09262010-04-01 18:32:35 +00003110 if (!isa<ValueDecl>(DRE->getDecl())) {
3111 S.Diag(Arg->getSourceRange().getBegin(),
3112 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003113 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003114 S.Diag(Param->getLocation(), diag::note_template_param_here);
3115 return true;
3116 }
3117
3118 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003119
3120 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003121 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3122 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003123 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003124 S.Diag(Param->getLocation(), diag::note_template_param_here);
3125 return true;
3126 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003127
3128 // Cannot refer to non-static member functions
3129 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003130 if (!Method->isStatic()) {
3131 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003132 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003133 S.Diag(Param->getLocation(), diag::note_template_param_here);
3134 return true;
3135 }
Mike Stump1eb44332009-09-09 15:08:12 +00003136
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003137 // Functions must have external linkage.
3138 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003139 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003140 S.Diag(Arg->getSourceRange().getBegin(),
3141 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003142 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003143 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003144 << true;
3145 return true;
3146 }
3147
3148 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003149 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003150
Douglas Gregorb7a09262010-04-01 18:32:35 +00003151 // If the template parameter has pointer type, the function decays.
3152 if (ParamType->isPointerType() && !AddressTaken)
3153 ArgType = S.Context.getPointerType(Func->getType());
3154 else if (AddressTaken && ParamType->isReferenceType()) {
3155 // If we originally had an address-of operator, but the
3156 // parameter has reference type, complain and (if things look
3157 // like they will work) drop the address-of operator.
3158 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3159 ParamType.getNonReferenceType())) {
3160 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3161 << ParamType;
3162 S.Diag(Param->getLocation(), diag::note_template_param_here);
3163 return true;
3164 }
3165
3166 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3167 << ParamType
3168 << FixItHint::CreateRemoval(AddrOpLoc);
3169 S.Diag(Param->getLocation(), diag::note_template_param_here);
3170
3171 ArgType = Func->getType();
3172 }
3173 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003174 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003175 S.Diag(Arg->getSourceRange().getBegin(),
3176 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003177 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003178 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003179 << true;
3180 return true;
3181 }
3182
Douglas Gregorb7a09262010-04-01 18:32:35 +00003183 // A value of reference type is not an object.
3184 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003185 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003186 diag::err_template_arg_reference_var)
3187 << Var->getType() << Arg->getSourceRange();
3188 S.Diag(Param->getLocation(), diag::note_template_param_here);
3189 return true;
3190 }
3191
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003192 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003193 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003194
3195 // If the template parameter has pointer type, we must have taken
3196 // the address of this object.
3197 if (ParamType->isReferenceType()) {
3198 if (AddressTaken) {
3199 // If we originally had an address-of operator, but the
3200 // parameter has reference type, complain and (if things look
3201 // like they will work) drop the address-of operator.
3202 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3203 ParamType.getNonReferenceType())) {
3204 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3205 << ParamType;
3206 S.Diag(Param->getLocation(), diag::note_template_param_here);
3207 return true;
3208 }
3209
3210 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3211 << ParamType
3212 << FixItHint::CreateRemoval(AddrOpLoc);
3213 S.Diag(Param->getLocation(), diag::note_template_param_here);
3214
3215 ArgType = Var->getType();
3216 }
3217 } else if (!AddressTaken && ParamType->isPointerType()) {
3218 if (Var->getType()->isArrayType()) {
3219 // Array-to-pointer decay.
3220 ArgType = S.Context.getArrayDecayedType(Var->getType());
3221 } else {
3222 // If the template parameter has pointer type but the address of
3223 // this object was not taken, complain and (possibly) recover by
3224 // taking the address of the entity.
3225 ArgType = S.Context.getPointerType(Var->getType());
3226 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3227 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3228 << ParamType;
3229 S.Diag(Param->getLocation(), diag::note_template_param_here);
3230 return true;
3231 }
3232
3233 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3234 << ParamType
3235 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3236
3237 S.Diag(Param->getLocation(), diag::note_template_param_here);
3238 }
3239 }
3240 } else {
3241 // We found something else, but we don't know specifically what it is.
3242 S.Diag(Arg->getSourceRange().getBegin(),
3243 diag::err_template_arg_not_object_or_func)
3244 << Arg->getSourceRange();
3245 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3246 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003247 }
Mike Stump1eb44332009-09-09 15:08:12 +00003248
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003249 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003250 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003251 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003252 // For pointer-to-object types, qualification conversions are
3253 // permitted.
3254 } else {
3255 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3256 if (!ParamRef->getPointeeType()->isFunctionType()) {
3257 // C++ [temp.arg.nontype]p5b3:
3258 // For a non-type template-parameter of type reference to
3259 // object, no conversions apply. The type referred to by the
3260 // reference may be more cv-qualified than the (otherwise
3261 // identical) type of the template- argument. The
3262 // template-parameter is bound directly to the
3263 // template-argument, which shall be an lvalue.
3264
3265 // FIXME: Other qualifiers?
3266 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3267 unsigned ArgQuals = ArgType.getCVRQualifiers();
3268
3269 if ((ParamQuals | ArgQuals) != ParamQuals) {
3270 S.Diag(Arg->getSourceRange().getBegin(),
3271 diag::err_template_arg_ref_bind_ignores_quals)
3272 << ParamType << Arg->getType()
3273 << Arg->getSourceRange();
3274 S.Diag(Param->getLocation(), diag::note_template_param_here);
3275 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003276 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003277 }
3278 }
3279
3280 // At this point, the template argument refers to an object or
3281 // function with external linkage. We now need to check whether the
3282 // argument and parameter types are compatible.
3283 if (!S.Context.hasSameUnqualifiedType(ArgType,
3284 ParamType.getNonReferenceType())) {
3285 // We can't perform this conversion or binding.
3286 if (ParamType->isReferenceType())
3287 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3288 << ParamType << Arg->getType() << Arg->getSourceRange();
3289 else
3290 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3291 << Arg->getType() << ParamType << Arg->getSourceRange();
3292 S.Diag(Param->getLocation(), diag::note_template_param_here);
3293 return true;
3294 }
3295 }
3296
3297 // Create the template argument.
3298 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003299 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003300 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003301}
3302
3303/// \brief Checks whether the given template argument is a pointer to
3304/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003305bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003306 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003307 bool Invalid = false;
3308
3309 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003310 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003311 Arg = Cast->getSubExpr();
3312
3313 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003314 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003315 // A template-argument for a non-type, non-template
3316 // template-parameter shall be one of: [...]
3317 //
3318 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003319 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003320
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003321 // In C++98/03 mode, give an extension warning on any extra parentheses.
3322 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3323 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003324 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003325 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003326 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003327 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003328 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003329 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003330 }
3331
3332 Arg = Parens->getSubExpr();
3333 }
3334
Douglas Gregorcaddba02009-11-12 18:38:13 +00003335 // A pointer-to-member constant written &Class::member.
3336 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003337 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003338 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3339 if (DRE && !DRE->getQualifier())
3340 DRE = 0;
3341 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003342 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003343 // A constant of pointer-to-member type.
3344 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3345 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3346 if (VD->getType()->isMemberPointerType()) {
3347 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003348 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003349 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3350 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003351 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003352 else
3353 Converted = TemplateArgument(VD->getCanonicalDecl());
3354 return Invalid;
3355 }
3356 }
3357 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003358
Douglas Gregorcaddba02009-11-12 18:38:13 +00003359 DRE = 0;
3360 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003361
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003362 if (!DRE)
3363 return Diag(Arg->getSourceRange().getBegin(),
3364 diag::err_template_arg_not_pointer_to_member_form)
3365 << Arg->getSourceRange();
3366
3367 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3368 assert((isa<FieldDecl>(DRE->getDecl()) ||
3369 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3370 "Only non-static member pointers can make it here");
3371
3372 // Okay: this is the address of a non-static member, and therefore
3373 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003374 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003375 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003376 else
3377 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003378 return Invalid;
3379 }
3380
3381 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003382 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003383 diag::err_template_arg_not_pointer_to_member_form)
3384 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003385 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003386 diag::note_template_arg_refers_here);
3387 return true;
3388}
3389
Douglas Gregorc15cb382009-02-09 23:23:08 +00003390/// \brief Check a template argument against its corresponding
3391/// non-type template parameter.
3392///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003393/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003394/// If an error occurred, it returns ExprError(); otherwise, it
3395/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003396/// InstantiatedParamType is the type of the non-type template
3397/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003398ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3399 QualType InstantiatedParamType, Expr *Arg,
3400 TemplateArgument &Converted,
3401 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003402 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3403
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003404 // If either the parameter has a dependent type or the argument is
3405 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003406 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3407 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003408 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003409 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003410 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003411
3412 // C++ [temp.arg.nontype]p5:
3413 // The following conversions are performed on each expression used
3414 // as a non-type template-argument. If a non-type
3415 // template-argument cannot be converted to the type of the
3416 // corresponding template-parameter then the program is
3417 // ill-formed.
3418 //
3419 // -- for a non-type template-parameter of integral or
3420 // enumeration type, integral promotions (4.5) and integral
3421 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003422 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003423 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003424 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003425 // C++ [temp.arg.nontype]p1:
3426 // A template-argument for a non-type, non-template
3427 // template-parameter shall be one of:
3428 //
3429 // -- an integral constant-expression of integral or enumeration
3430 // type; or
3431 // -- the name of a non-type template-parameter; or
3432 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003433 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003434 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003435 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003436 diag::err_template_arg_not_integral_or_enumeral)
3437 << ArgType << Arg->getSourceRange();
3438 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003439 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003440 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003441 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003442 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3443 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003444 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003445 }
3446
Douglas Gregor02024a92010-03-28 02:42:43 +00003447 // From here on out, all we care about are the unqualified forms
3448 // of the parameter and argument types.
3449 ParamType = ParamType.getUnqualifiedType();
3450 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003451
3452 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003453 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003454 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003455 } else if (CTAK == CTAK_Deduced) {
3456 // C++ [temp.deduct.type]p17:
3457 // If, in the declaration of a function template with a non-type
3458 // template-parameter, the non-type template- parameter is used
3459 // in an expression in the function parameter-list and, if the
3460 // corresponding template-argument is deduced, the
3461 // template-argument type shall match the type of the
3462 // template-parameter exactly, except that a template-argument
3463 // deduced from an array bound may be of any integral type.
3464 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3465 << ArgType << ParamType;
3466 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003467 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003468 } else if (ParamType->isBooleanType()) {
3469 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003470 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003471 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3472 !ParamType->isEnumeralType()) {
3473 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003474 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003475 } else {
3476 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003477 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003478 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003479 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003480 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003481 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003482 }
3483
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003484 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003485 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003486 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003487
3488 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003489 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003490
3491 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003492 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003493 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003494 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003495 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003496 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003497
3498 // Complain if an unsigned parameter received a negative value.
3499 if (IntegerType->isUnsignedIntegerType()
3500 && (OldValue.isSigned() && OldValue.isNegative())) {
3501 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3502 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3503 << Arg->getSourceRange();
3504 Diag(Param->getLocation(), diag::note_template_param_here);
3505 }
3506
3507 // Complain if we overflowed the template parameter's type.
3508 unsigned RequiredBits;
3509 if (IntegerType->isUnsignedIntegerType())
3510 RequiredBits = OldValue.getActiveBits();
3511 else if (OldValue.isUnsigned())
3512 RequiredBits = OldValue.getActiveBits() + 1;
3513 else
3514 RequiredBits = OldValue.getMinSignedBits();
3515 if (RequiredBits > AllowedBits) {
3516 Diag(Arg->getSourceRange().getBegin(),
3517 diag::warn_template_arg_too_large)
3518 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3519 << Arg->getSourceRange();
3520 Diag(Param->getLocation(), diag::note_template_param_here);
3521 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003522 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003523
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003524 // Add the value of this argument to the list of converted
3525 // arguments. We use the bitwidth and signedness of the template
3526 // parameter.
3527 if (Arg->isValueDependent()) {
3528 // The argument is value-dependent. Create a new
3529 // TemplateArgument with the converted expression.
3530 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003531 return Owned(Arg);
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003532 }
3533
John McCall833ca992009-10-29 08:12:44 +00003534 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003535 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003536 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003537 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003538 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003539
John McCall6bb80172010-03-30 21:47:33 +00003540 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3541
Douglas Gregorb7a09262010-04-01 18:32:35 +00003542 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3543 // from a template argument of type std::nullptr_t to a non-type
3544 // template parameter of type pointer to object, pointer to
3545 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003546 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003547 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3548 Converted = TemplateArgument((NamedDecl *)0);
John Wiegley429bb272011-04-08 18:41:53 +00003549 return Owned(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003550 }
3551
Douglas Gregorb86b0572009-02-11 01:18:59 +00003552 // Handle pointer-to-function, reference-to-function, and
3553 // pointer-to-member-function all in (roughly) the same way.
3554 if (// -- For a non-type template-parameter of type pointer to
3555 // function, only the function-to-pointer conversion (4.3) is
3556 // applied. If the template-argument represents a set of
3557 // overloaded functions (or a pointer to such), the matching
3558 // function is selected from the set (13.4).
3559 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003560 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003561 // -- For a non-type template-parameter of type reference to
3562 // function, no conversions apply. If the template-argument
3563 // represents a set of overloaded functions, the matching
3564 // function is selected from the set (13.4).
3565 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003566 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003567 // -- For a non-type template-parameter of type pointer to
3568 // member function, no conversions apply. If the
3569 // template-argument represents a set of overloaded member
3570 // functions, the matching member function is selected from
3571 // the set (13.4).
3572 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003573 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003574 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003575
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003576 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003577 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003578 true,
3579 FoundResult)) {
3580 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003581 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003582
3583 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3584 ArgType = Arg->getType();
3585 } else
John Wiegley429bb272011-04-08 18:41:53 +00003586 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003587 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003588
John Wiegley429bb272011-04-08 18:41:53 +00003589 if (!ParamType->isMemberPointerType()) {
3590 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3591 ParamType,
3592 Arg, Converted))
3593 return ExprError();
3594 return Owned(Arg);
3595 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003596
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003597 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3598 false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003599 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003600 } else if (!Context.hasSameUnqualifiedType(ArgType,
3601 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003602 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003603 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003604 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003605 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003606 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003607 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003608 }
Mike Stump1eb44332009-09-09 15:08:12 +00003609
John Wiegley429bb272011-04-08 18:41:53 +00003610 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3611 return ExprError();
3612 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003613 }
3614
Chris Lattnerfe90de72009-02-20 21:37:53 +00003615 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003616 // -- for a non-type template-parameter of type pointer to
3617 // object, qualification conversions (4.4) and the
3618 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003619 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003620 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003621 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003622
John Wiegley429bb272011-04-08 18:41:53 +00003623 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3624 ParamType,
3625 Arg, Converted))
3626 return ExprError();
3627 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003628 }
Mike Stump1eb44332009-09-09 15:08:12 +00003629
Ted Kremenek6217b802009-07-29 21:53:49 +00003630 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003631 // -- For a non-type template-parameter of type reference to
3632 // object, no conversions apply. The type referred to by the
3633 // reference may be more cv-qualified than the (otherwise
3634 // identical) type of the template-argument. The
3635 // template-parameter is bound directly to the
3636 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003637 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003638 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003639
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003640 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003641 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3642 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003643 true,
3644 FoundResult)) {
3645 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003646 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003647
3648 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3649 ArgType = Arg->getType();
3650 } else
John Wiegley429bb272011-04-08 18:41:53 +00003651 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003652 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003653
John Wiegley429bb272011-04-08 18:41:53 +00003654 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3655 ParamType,
3656 Arg, Converted))
3657 return ExprError();
3658 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003659 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003660
3661 // -- For a non-type template-parameter of type pointer to data
3662 // member, qualification conversions (4.4) are applied.
3663 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3664
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003665 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003666 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003667 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003668 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003669 } else {
3670 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003671 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003672 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003673 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003674 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003675 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003676 }
3677
John Wiegley429bb272011-04-08 18:41:53 +00003678 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3679 return ExprError();
3680 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003681}
3682
3683/// \brief Check a template argument against its corresponding
3684/// template template parameter.
3685///
3686/// This routine implements the semantics of C++ [temp.arg.template].
3687/// It returns true if an error occurred, and false otherwise.
3688bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003689 const TemplateArgumentLoc &Arg) {
3690 TemplateName Name = Arg.getArgument().getAsTemplate();
3691 TemplateDecl *Template = Name.getAsTemplateDecl();
3692 if (!Template) {
3693 // Any dependent template name is fine.
3694 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3695 return false;
3696 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003697
3698 // C++ [temp.arg.template]p1:
3699 // A template-argument for a template template-parameter shall be
3700 // the name of a class template, expressed as id-expression. Only
3701 // primary class templates are considered when matching the
3702 // template template argument with the corresponding parameter;
3703 // partial specializations are not considered even if their
3704 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003705 //
3706 // Note that we also allow template template parameters here, which
3707 // will happen when we are dealing with, e.g., class template
3708 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003709 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003710 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003711 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003712 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003713 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003714 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003715 << Template;
3716 }
3717
3718 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3719 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003720 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003721 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003722 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003723}
3724
Douglas Gregor02024a92010-03-28 02:42:43 +00003725/// \brief Given a non-type template argument that refers to a
3726/// declaration and the type of its corresponding non-type template
3727/// parameter, produce an expression that properly refers to that
3728/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003729ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003730Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3731 QualType ParamType,
3732 SourceLocation Loc) {
3733 assert(Arg.getKind() == TemplateArgument::Declaration &&
3734 "Only declaration template arguments permitted here");
3735 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3736
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003737 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003738 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3739 // If the value is a class member, we might have a pointer-to-member.
3740 // Determine whether the non-type template template parameter is of
3741 // pointer-to-member type. If so, we need to build an appropriate
3742 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3743 // would refer to the member itself.
3744 if (ParamType->isMemberPointerType()) {
3745 QualType ClassType
3746 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3747 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003748 = NestedNameSpecifier::Create(Context, 0, false,
3749 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003750 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003751 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003752
3753 // The actual value-ness of this is unimportant, but for
3754 // internal consistency's sake, references to instance methods
3755 // are r-values.
3756 ExprValueKind VK = VK_LValue;
3757 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3758 VK = VK_RValue;
3759
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003760 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003761 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003762 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003763 Loc,
3764 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003765 if (RefExpr.isInvalid())
3766 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003767
John McCall2de56d12010-08-25 11:45:40 +00003768 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003769
Douglas Gregorc0c83002010-04-30 21:46:38 +00003770 // We might need to perform a trailing qualification conversion, since
3771 // the element type on the parameter could be more qualified than the
3772 // element type in the expression we constructed.
3773 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John Wiegley429bb272011-04-08 18:41:53 +00003774 ParamType.getUnqualifiedType(), false))
3775 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003776
Douglas Gregor02024a92010-03-28 02:42:43 +00003777 assert(!RefExpr.isInvalid() &&
3778 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003779 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003780 return move(RefExpr);
3781 }
3782 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003783
Douglas Gregor02024a92010-03-28 02:42:43 +00003784 QualType T = VD->getType().getNonReferenceType();
3785 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003786 // When the non-type template parameter is a pointer, take the
3787 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003788 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003789 if (RefExpr.isInvalid())
3790 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003791
3792 if (T->isFunctionType() || T->isArrayType()) {
3793 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00003794 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
3795 if (RefExpr.isInvalid())
3796 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003797
3798 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003799 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003800
Douglas Gregorb7a09262010-04-01 18:32:35 +00003801 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003802 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003803 }
3804
John McCallf89e55a2010-11-18 06:31:45 +00003805 ExprValueKind VK = VK_RValue;
3806
Douglas Gregor02024a92010-03-28 02:42:43 +00003807 // If the non-type template parameter has reference type, qualify the
3808 // resulting declaration reference with the extra qualifiers on the
3809 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003810 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3811 VK = VK_LValue;
3812 T = Context.getQualifiedType(T,
3813 TargetRef->getPointeeType().getQualifiers());
3814 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003815
John McCallf89e55a2010-11-18 06:31:45 +00003816 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003817}
3818
3819/// \brief Construct a new expression that refers to the given
3820/// integral template argument with the given source-location
3821/// information.
3822///
3823/// This routine takes care of the mapping from an integral template
3824/// argument (which may have any integral type) to the appropriate
3825/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003826ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003827Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3828 SourceLocation Loc) {
3829 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003830 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003831 QualType T = Arg.getIntegralType();
3832 if (T->isCharType() || T->isWideCharType())
3833 return Owned(new (Context) CharacterLiteral(
3834 Arg.getAsIntegral()->getZExtValue(),
3835 T->isWideCharType(),
3836 T,
3837 Loc));
3838 if (T->isBooleanType())
3839 return Owned(new (Context) CXXBoolLiteralExpr(
3840 Arg.getAsIntegral()->getBoolValue(),
3841 T,
3842 Loc));
3843
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003844 QualType BT;
3845 if (const EnumType *ET = T->getAs<EnumType>())
3846 BT = ET->getDecl()->getPromotionType();
3847 else
3848 BT = T;
3849
3850 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003851 if (T->isEnumeralType()) {
3852 // FIXME: This is a hack. We need a better way to handle substituted
3853 // non-type template parameters.
3854 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3855 E, 0,
3856 Context.getTrivialTypeSourceInfo(T, Loc),
3857 Loc, Loc);
3858 }
3859
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003860 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003861}
3862
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003863/// \brief Match two template parameters within template parameter lists.
3864static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3865 bool Complain,
3866 Sema::TemplateParameterListEqualKind Kind,
3867 SourceLocation TemplateArgLoc) {
3868 // Check the actual kind (type, non-type, template).
3869 if (Old->getKind() != New->getKind()) {
3870 if (Complain) {
3871 unsigned NextDiag = diag::err_template_param_different_kind;
3872 if (TemplateArgLoc.isValid()) {
3873 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3874 NextDiag = diag::note_template_param_different_kind;
3875 }
3876 S.Diag(New->getLocation(), NextDiag)
3877 << (Kind != Sema::TPL_TemplateMatch);
3878 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3879 << (Kind != Sema::TPL_TemplateMatch);
3880 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003881
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003882 return false;
3883 }
3884
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003885 // Check that both are parameter packs are neither are parameter packs.
3886 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003887 // template template parameter, the template template parameter can have
3888 // a parameter pack where the template template argument does not.
3889 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3890 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3891 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003892 if (Complain) {
3893 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3894 if (TemplateArgLoc.isValid()) {
3895 S.Diag(TemplateArgLoc,
3896 diag::err_template_arg_template_params_mismatch);
3897 NextDiag = diag::note_template_parameter_pack_non_pack;
3898 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003899
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003900 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3901 : isa<NonTypeTemplateParmDecl>(New)? 1
3902 : 2;
3903 S.Diag(New->getLocation(), NextDiag)
3904 << ParamKind << New->isParameterPack();
3905 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3906 << ParamKind << Old->isParameterPack();
3907 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003908
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003909 return false;
3910 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003911
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003912 // For non-type template parameters, check the type of the parameter.
3913 if (NonTypeTemplateParmDecl *OldNTTP
3914 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3915 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003916
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003917 // If we are matching a template template argument to a template
3918 // template parameter and one of the non-type template parameter types
3919 // is dependent, then we must wait until template instantiation time
3920 // to actually compare the arguments.
3921 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3922 (OldNTTP->getType()->isDependentType() ||
3923 NewNTTP->getType()->isDependentType()))
3924 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003925
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003926 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3927 if (Complain) {
3928 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3929 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003930 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003931 diag::err_template_arg_template_params_mismatch);
3932 NextDiag = diag::note_template_nontype_parm_different_type;
3933 }
3934 S.Diag(NewNTTP->getLocation(), NextDiag)
3935 << NewNTTP->getType()
3936 << (Kind != Sema::TPL_TemplateMatch);
3937 S.Diag(OldNTTP->getLocation(),
3938 diag::note_template_nontype_parm_prev_declaration)
3939 << OldNTTP->getType();
3940 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003941
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003942 return false;
3943 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003944
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003945 return true;
3946 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003947
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003948 // For template template parameters, check the template parameter types.
3949 // The template parameter lists of template template
3950 // parameters must agree.
3951 if (TemplateTemplateParmDecl *OldTTP
3952 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003953 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003954 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3955 OldTTP->getTemplateParameters(),
3956 Complain,
3957 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003958 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003959 : Kind),
3960 TemplateArgLoc);
3961 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003962
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003963 return true;
3964}
Douglas Gregor02024a92010-03-28 02:42:43 +00003965
Douglas Gregora0347822011-01-13 00:08:50 +00003966/// \brief Diagnose a known arity mismatch when comparing template argument
3967/// lists.
3968static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003969void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003970 TemplateParameterList *New,
3971 TemplateParameterList *Old,
3972 Sema::TemplateParameterListEqualKind Kind,
3973 SourceLocation TemplateArgLoc) {
3974 unsigned NextDiag = diag::err_template_param_list_different_arity;
3975 if (TemplateArgLoc.isValid()) {
3976 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3977 NextDiag = diag::note_template_param_list_different_arity;
3978 }
3979 S.Diag(New->getTemplateLoc(), NextDiag)
3980 << (New->size() > Old->size())
3981 << (Kind != Sema::TPL_TemplateMatch)
3982 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3983 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3984 << (Kind != Sema::TPL_TemplateMatch)
3985 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3986}
3987
Douglas Gregorddc29e12009-02-06 22:42:48 +00003988/// \brief Determine whether the given template parameter lists are
3989/// equivalent.
3990///
Mike Stump1eb44332009-09-09 15:08:12 +00003991/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003992/// source code as part of a new template declaration.
3993///
3994/// \param Old The old template parameter list, typically found via
3995/// name lookup of the template declared with this template parameter
3996/// list.
3997///
3998/// \param Complain If true, this routine will produce a diagnostic if
3999/// the template parameter lists are not equivalent.
4000///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004001/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004002///
4003/// \param TemplateArgLoc If this source location is valid, then we
4004/// are actually checking the template parameter list of a template
4005/// argument (New) against the template parameter list of its
4006/// corresponding template template parameter (Old). We produce
4007/// slightly different diagnostics in this scenario.
4008///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004009/// \returns True if the template parameter lists are equal, false
4010/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004011bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004012Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4013 TemplateParameterList *Old,
4014 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004015 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004016 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004017 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4018 if (Complain)
4019 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4020 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004021
4022 return false;
4023 }
4024
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004025 // C++0x [temp.arg.template]p3:
4026 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004027 // when each of the template parameters in the template-parameter-list of
4028 // the template-argument's corresponding class template or template alias
4029 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004030 // template-parameter-list of P. [...]
4031 TemplateParameterList::iterator NewParm = New->begin();
4032 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004033 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004034 OldParmEnd = Old->end();
4035 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004036 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4037 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004038 if (NewParm == NewParmEnd) {
4039 if (Complain)
4040 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4041 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004042
Douglas Gregora0347822011-01-13 00:08:50 +00004043 return false;
4044 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004045
Douglas Gregora0347822011-01-13 00:08:50 +00004046 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4047 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004048 return false;
4049
Douglas Gregora0347822011-01-13 00:08:50 +00004050 ++NewParm;
4051 continue;
4052 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004053
Douglas Gregora0347822011-01-13 00:08:50 +00004054 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004055 // [...] When P's template- parameter-list contains a template parameter
4056 // pack (14.5.3), the template parameter pack will match zero or more
4057 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004058 // template-parameter-list of A with the same type and form as the
4059 // template parameter pack in P (ignoring whether those template
4060 // parameters are template parameter packs).
4061 for (; NewParm != NewParmEnd; ++NewParm) {
4062 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4063 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004064 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004065 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004066 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004067
Douglas Gregora0347822011-01-13 00:08:50 +00004068 // Make sure we exhausted all of the arguments.
4069 if (NewParm != NewParmEnd) {
4070 if (Complain)
4071 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4072 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004073
Douglas Gregora0347822011-01-13 00:08:50 +00004074 return false;
4075 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004076
Douglas Gregorddc29e12009-02-06 22:42:48 +00004077 return true;
4078}
4079
4080/// \brief Check whether a template can be declared within this scope.
4081///
4082/// If the template declaration is valid in this scope, returns
4083/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004084bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004085Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004086 // Find the nearest enclosing declaration scope.
4087 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4088 (S->getFlags() & Scope::TemplateParamScope) != 0)
4089 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004090
Douglas Gregorddc29e12009-02-06 22:42:48 +00004091 // C++ [temp]p2:
4092 // A template-declaration can appear only as a namespace scope or
4093 // class scope declaration.
4094 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004095 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4096 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004097 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004098 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004099
Eli Friedman1503f772009-07-31 01:43:05 +00004100 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004101 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004102
4103 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4104 return false;
4105
Mike Stump1eb44332009-09-09 15:08:12 +00004106 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004107 diag::err_template_outside_namespace_or_class_scope)
4108 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004109}
Douglas Gregorcc636682009-02-17 23:15:12 +00004110
Douglas Gregord5cb8762009-10-07 00:13:32 +00004111/// \brief Determine what kind of template specialization the given declaration
4112/// is.
4113static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4114 if (!D)
4115 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004116
Douglas Gregorf6b11852009-10-08 15:14:33 +00004117 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4118 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004119 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4120 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004121 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4122 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004123
Douglas Gregord5cb8762009-10-07 00:13:32 +00004124 return TSK_Undeclared;
4125}
4126
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004127/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004128/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004129///
Douglas Gregor9302da62009-10-14 23:50:59 +00004130/// This routine determines whether a template specialization can be declared
4131/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004132///
4133/// \param S the semantic analysis object for which this check is being
4134/// performed.
4135///
4136/// \param Specialized the entity being specialized or instantiated, which
4137/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004138/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004139/// member class).
4140///
4141/// \param PrevDecl the previous declaration of this entity, if any.
4142///
4143/// \param Loc the location of the explicit specialization or instantiation of
4144/// this entity.
4145///
4146/// \param IsPartialSpecialization whether this is a partial specialization of
4147/// a class template.
4148///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004149/// \returns true if there was an error that we cannot recover from, false
4150/// otherwise.
4151static bool CheckTemplateSpecializationScope(Sema &S,
4152 NamedDecl *Specialized,
4153 NamedDecl *PrevDecl,
4154 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004155 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004156 // Keep these "kind" numbers in sync with the %select statements in the
4157 // various diagnostics emitted by this routine.
4158 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004159 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004160 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004161 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004162 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004163 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004164 EntityKind = 3;
4165 else if (isa<VarDecl>(Specialized))
4166 EntityKind = 4;
4167 else if (isa<RecordDecl>(Specialized))
4168 EntityKind = 5;
4169 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004170 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4171 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004172 return true;
4173 }
4174
Douglas Gregor88b70942009-02-25 22:02:03 +00004175 // C++ [temp.expl.spec]p2:
4176 // An explicit specialization shall be declared in the namespace
4177 // of which the template is a member, or, for member templates, in
4178 // the namespace of which the enclosing class or enclosing class
4179 // template is a member. An explicit specialization of a member
4180 // function, member class or static data member of a class
4181 // template shall be declared in the namespace of which the class
4182 // template is a member. Such a declaration may also be a
4183 // definition. If the declaration is not a definition, the
4184 // specialization may be defined later in the name- space in which
4185 // the explicit specialization was declared, or in a namespace
4186 // that encloses the one in which the explicit specialization was
4187 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004188 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004189 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004190 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004191 return true;
4192 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004193
Douglas Gregor0a407472009-10-07 17:30:37 +00004194 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4195 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004196 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004197 return true;
4198 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004199
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004200 // C++ [temp.class.spec]p6:
4201 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004202 // in any namespace scope in which its definition may be defined (14.5.1
4203 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004204 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004205 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004206 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004207 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004208 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004209 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4210 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004211 // C++ [temp.exp.spec]p2:
4212 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004213 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004214 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004215 // An explicit specialization of a member function, member class or
4216 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004217 // namespace of which the class template is a member.
4218 //
4219 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004220 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004221 // the specialized template.
4222 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4223 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004224 bool IsCPlusPlus0xExtension
4225 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004226 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004227 S.Diag(Loc, IsCPlusPlus0xExtension
4228 ? diag::ext_template_spec_decl_out_of_scope_global
4229 : diag::err_template_spec_decl_out_of_scope_global)
4230 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004231 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004232 S.Diag(Loc, IsCPlusPlus0xExtension
4233 ? diag::ext_template_spec_decl_out_of_scope
4234 : diag::err_template_spec_decl_out_of_scope)
4235 << EntityKind << Specialized
4236 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004237
Douglas Gregor9302da62009-10-14 23:50:59 +00004238 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4239 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004240 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004241 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004242
4243 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004244 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004245 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004246 // specializations of function templates, static data members, and member
4247 // functions, so we skip the check here for those kinds of entities.
4248 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004249 // Should we refactor that check, so that it occurs later?
4250 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004251 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4252 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004253 if (isa<TranslationUnitDecl>(SpecializedContext))
4254 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4255 << EntityKind << Specialized;
4256 else if (isa<NamespaceDecl>(SpecializedContext))
4257 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4258 << EntityKind << Specialized
4259 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004260
Douglas Gregor9302da62009-10-14 23:50:59 +00004261 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004262 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004263
Douglas Gregord5cb8762009-10-07 00:13:32 +00004264 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004265
Douglas Gregor88b70942009-02-25 22:02:03 +00004266 return false;
4267}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004268
Douglas Gregorbacb9492011-01-03 21:13:47 +00004269/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4270/// that checks non-type template partial specialization arguments.
4271static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4272 NonTypeTemplateParmDecl *Param,
4273 const TemplateArgument *Args,
4274 unsigned NumArgs) {
4275 for (unsigned I = 0; I != NumArgs; ++I) {
4276 if (Args[I].getKind() == TemplateArgument::Pack) {
4277 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004278 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004279 Args[I].pack_size()))
4280 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004281
Douglas Gregore94866f2009-06-12 21:21:02 +00004282 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004283 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004284
Douglas Gregorbacb9492011-01-03 21:13:47 +00004285 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004286 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004287 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004288 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004289
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004290 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004291 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4292 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004293
4294 // Strip off any implicit casts we added as part of type checking.
4295 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4296 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004297
Douglas Gregore94866f2009-06-12 21:21:02 +00004298 // C++ [temp.class.spec]p8:
4299 // A non-type argument is non-specialized if it is the name of a
4300 // non-type parameter. All other non-type arguments are
4301 // specialized.
4302 //
4303 // Below, we check the two conditions that only apply to
4304 // specialized non-type arguments, so skip any non-specialized
4305 // arguments.
4306 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004307 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004308 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004309
Douglas Gregore94866f2009-06-12 21:21:02 +00004310 // C++ [temp.class.spec]p9:
4311 // Within the argument list of a class template partial
4312 // specialization, the following restrictions apply:
4313 // -- A partially specialized non-type argument expression
4314 // shall not involve a template parameter of the partial
4315 // specialization except when the argument expression is a
4316 // simple identifier.
4317 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004318 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004319 diag::err_dependent_non_type_arg_in_partial_spec)
4320 << ArgExpr->getSourceRange();
4321 return true;
4322 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004323
Douglas Gregore94866f2009-06-12 21:21:02 +00004324 // -- The type of a template parameter corresponding to a
4325 // specialized non-type argument shall not be dependent on a
4326 // parameter of the specialization.
4327 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004328 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004329 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4330 << Param->getType()
4331 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004332 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004333 return true;
4334 }
4335 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004336
Douglas Gregorbacb9492011-01-03 21:13:47 +00004337 return false;
4338}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004339
Douglas Gregorbacb9492011-01-03 21:13:47 +00004340/// \brief Check the non-type template arguments of a class template
4341/// partial specialization according to C++ [temp.class.spec]p9.
4342///
4343/// \param TemplateParams the template parameters of the primary class
4344/// template.
4345///
4346/// \param TemplateArg the template arguments of the class template
4347/// partial specialization.
4348///
4349/// \returns true if there was an error, false otherwise.
4350static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4351 TemplateParameterList *TemplateParams,
4352 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4353 const TemplateArgument *ArgList = TemplateArgs.data();
4354
4355 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4356 NonTypeTemplateParmDecl *Param
4357 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4358 if (!Param)
4359 continue;
4360
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004361 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004362 &ArgList[I], 1))
4363 return true;
4364 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004365
4366 return false;
4367}
4368
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004369/// \brief Retrieve the previous declaration of the given declaration.
4370static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4371 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4372 return VD->getPreviousDeclaration();
4373 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4374 return FD->getPreviousDeclaration();
4375 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4376 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004377 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004378 return TD->getPreviousDeclaration();
4379 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4380 return FTD->getPreviousDeclaration();
4381 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4382 return CTD->getPreviousDeclaration();
4383 return 0;
4384}
4385
John McCalld226f652010-08-21 09:40:31 +00004386DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004387Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4388 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004389 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004390 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004391 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004392 SourceLocation TemplateNameLoc,
4393 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004394 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004395 SourceLocation RAngleLoc,
4396 AttributeList *Attr,
4397 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004398 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004399
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004400 // NOTE: KWLoc is the location of the tag keyword. This will instead
4401 // store the location of the outermost template keyword in the declaration.
4402 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4403 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4404
Douglas Gregorcc636682009-02-17 23:15:12 +00004405 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004406 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004407 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004408 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4409
4410 if (!ClassTemplate) {
4411 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004412 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004413 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4414 return true;
4415 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004416
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004417 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004418 bool isPartialSpecialization = false;
4419
Douglas Gregor88b70942009-02-25 22:02:03 +00004420 // Check the validity of the template headers that introduce this
4421 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004422 // FIXME: We probably shouldn't complain about these headers for
4423 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004424 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004425 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004426 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4427 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004428 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004429 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004430 isExplicitSpecialization,
4431 Invalid);
4432 if (Invalid)
4433 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004434
Douglas Gregor05396e22009-08-25 17:23:04 +00004435 if (TemplateParams && TemplateParams->size() > 0) {
4436 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004437
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004438 if (TUK == TUK_Friend) {
4439 Diag(KWLoc, diag::err_partial_specialization_friend)
4440 << SourceRange(LAngleLoc, RAngleLoc);
4441 return true;
4442 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004443
Douglas Gregor05396e22009-08-25 17:23:04 +00004444 // C++ [temp.class.spec]p10:
4445 // The template parameter list of a specialization shall not
4446 // contain default template argument values.
4447 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4448 Decl *Param = TemplateParams->getParam(I);
4449 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4450 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004451 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004452 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004453 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004454 }
4455 } else if (NonTypeTemplateParmDecl *NTTP
4456 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4457 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004458 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004459 diag::err_default_arg_in_partial_spec)
4460 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004461 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004462 }
4463 } else {
4464 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004465 if (TTP->hasDefaultArgument()) {
4466 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004467 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004468 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004469 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004470 }
4471 }
4472 }
Douglas Gregora735b202009-10-13 14:39:41 +00004473 } else if (TemplateParams) {
4474 if (TUK == TUK_Friend)
4475 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004476 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004477 SourceRange(TemplateParams->getTemplateLoc(),
4478 TemplateParams->getRAngleLoc()))
4479 << SourceRange(LAngleLoc, RAngleLoc);
4480 else
4481 isExplicitSpecialization = true;
4482 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004483 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004484 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004485 isExplicitSpecialization = true;
4486 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004487
Douglas Gregorcc636682009-02-17 23:15:12 +00004488 // Check that the specialization uses the same tag kind as the
4489 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004490 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4491 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004492 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004493 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004494 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004495 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004496 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004497 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004498 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004499 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004500 diag::note_previous_use);
4501 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4502 }
4503
Douglas Gregor40808ce2009-03-09 23:48:35 +00004504 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004505 TemplateArgumentListInfo TemplateArgs;
4506 TemplateArgs.setLAngleLoc(LAngleLoc);
4507 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004508 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004509
Douglas Gregor925910d2011-01-03 20:35:03 +00004510 // Check for unexpanded parameter packs in any of the template arguments.
4511 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004512 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004513 UPPC_PartialSpecialization))
4514 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004515
Douglas Gregorcc636682009-02-17 23:15:12 +00004516 // Check that the template argument list is well-formed for this
4517 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004518 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004519 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4520 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004521 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004522
Douglas Gregor910f8002010-11-07 23:05:16 +00004523 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004524 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004525
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004526 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004527 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004528 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004529 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004530 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004531 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004532 return true;
4533
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004534 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004535 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004536 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004537 TemplateArgs.size())) {
4538 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4539 << ClassTemplate->getDeclName();
4540 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004541 }
4542 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004543
Douglas Gregorcc636682009-02-17 23:15:12 +00004544 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004545 ClassTemplateSpecializationDecl *PrevDecl = 0;
4546
4547 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004548 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004549 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004550 = ClassTemplate->findPartialSpecialization(Converted.data(),
4551 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004552 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004553 else
4554 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004555 = ClassTemplate->findSpecialization(Converted.data(),
4556 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004557
4558 ClassTemplateSpecializationDecl *Specialization = 0;
4559
Douglas Gregor88b70942009-02-25 22:02:03 +00004560 // Check whether we can declare a class template specialization in
4561 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004562 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004563 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4564 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004565 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004566 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004567
Douglas Gregorb88e8882009-07-30 17:40:51 +00004568 // The canonical type
4569 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004570 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004571 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004572 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004573 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004574 // arguments was referenced but not declared, or we're only
4575 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004576 // declaration node as our own, updating its source location and
4577 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004578 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004579 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004580 if (TemplateParameterLists.size() > 0) {
4581 Specialization->setTemplateParameterListsInfo(Context,
4582 TemplateParameterLists.size(),
4583 (TemplateParameterList**) TemplateParameterLists.release());
4584 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004585 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004586 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004587 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004588 // Build the canonical type that describes the converted template
4589 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004590 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4591 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004592 Converted.data(),
4593 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004594
4595 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004596 ClassTemplate->getInjectedClassNameSpecialization())) {
4597 // C++ [temp.class.spec]p9b3:
4598 //
4599 // -- The argument list of the specialization shall not be identical
4600 // to the implicit argument list of the primary template.
4601 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4602 << (TUK == TUK_Definition)
4603 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4604 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4605 ClassTemplate->getIdentifier(),
4606 TemplateNameLoc,
4607 Attr,
4608 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004609 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004610 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004611 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004612 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004613
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004614 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004615 ClassTemplatePartialSpecializationDecl *PrevPartial
4616 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004617 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004618 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004619 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004620 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004621 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004622 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004623 TemplateParams,
4624 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004625 Converted.data(),
4626 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004627 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004628 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004629 PrevPartial,
4630 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004631 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004632 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004633 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004634 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004635 (TemplateParameterList**) TemplateParameterLists.release());
4636 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004637
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004638 if (!PrevPartial)
4639 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004640 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004641
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004642 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004643 // template specialization, make a note of that.
4644 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4645 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004646
Douglas Gregor031a5882009-06-13 00:26:55 +00004647 // Check that all of the template parameters of the class template
4648 // partial specialization are deducible from the template
4649 // arguments. If not, this class template partial specialization
4650 // will never be used.
4651 llvm::SmallVector<bool, 8> DeducibleParams;
4652 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004653 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004654 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004655 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004656 unsigned NumNonDeducible = 0;
4657 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4658 if (!DeducibleParams[I])
4659 ++NumNonDeducible;
4660
4661 if (NumNonDeducible) {
4662 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4663 << (NumNonDeducible > 1)
4664 << SourceRange(TemplateNameLoc, RAngleLoc);
4665 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4666 if (!DeducibleParams[I]) {
4667 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4668 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004669 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004670 diag::note_partial_spec_unused_parameter)
4671 << Param->getDeclName();
4672 else
Mike Stump1eb44332009-09-09 15:08:12 +00004673 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004674 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004675 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004676 }
4677 }
4678 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004679 } else {
4680 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004681 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004682 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004683 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004684 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004685 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004686 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004687 Converted.data(),
4688 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004689 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004690 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004691 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004692 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004693 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00004694 (TemplateParameterList**) TemplateParameterLists.release());
4695 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004696
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004697 if (!PrevDecl)
4698 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004699
4700 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004701 }
4702
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004703 // C++ [temp.expl.spec]p6:
4704 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004705 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004706 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004707 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004708 // use occurs; no diagnostic is required.
4709 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004710 bool Okay = false;
4711 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4712 // Is there any previous explicit specialization declaration?
4713 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4714 Okay = true;
4715 break;
4716 }
4717 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004718
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004719 if (!Okay) {
4720 SourceRange Range(TemplateNameLoc, RAngleLoc);
4721 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4722 << Context.getTypeDeclType(Specialization) << Range;
4723
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004724 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004725 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004726 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004727 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004728 return true;
4729 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004730 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004731
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004732 // If this is not a friend, note that this is an explicit specialization.
4733 if (TUK != TUK_Friend)
4734 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004735
4736 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004737 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004738 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004739 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004740 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004741 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004742 Diag(Def->getLocation(), diag::note_previous_definition);
4743 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004744 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004745 }
4746 }
4747
John McCall7f1b9872010-12-18 03:30:47 +00004748 if (Attr)
4749 ProcessDeclAttributeList(S, Specialization, Attr);
4750
Douglas Gregorfc705b82009-02-26 22:19:44 +00004751 // Build the fully-sugared type for this class template
4752 // specialization as the user wrote in the specialization
4753 // itself. This means that we'll pretty-print the type retrieved
4754 // from the specialization's declaration the way that the user
4755 // actually wrote the specialization, rather than formatting the
4756 // name based on the "canonical" representation used to store the
4757 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004758 TypeSourceInfo *WrittenTy
4759 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4760 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004761 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004762 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004763 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004764 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004765 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004766
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004767 // C++ [temp.expl.spec]p9:
4768 // A template explicit specialization is in the scope of the
4769 // namespace in which the template was defined.
4770 //
4771 // We actually implement this paragraph where we set the semantic
4772 // context (in the creation of the ClassTemplateSpecializationDecl),
4773 // but we also maintain the lexical context where the actual
4774 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004775 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004776
Douglas Gregorcc636682009-02-17 23:15:12 +00004777 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004778 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004779 Specialization->startDefinition();
4780
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004781 if (TUK == TUK_Friend) {
4782 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4783 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004784 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004785 /*FIXME:*/KWLoc);
4786 Friend->setAccess(AS_public);
4787 CurContext->addDecl(Friend);
4788 } else {
4789 // Add the specialization into its lexical context, so that it can
4790 // be seen when iterating through the list of declarations in that
4791 // context. However, specializations are not found by name lookup.
4792 CurContext->addDecl(Specialization);
4793 }
John McCalld226f652010-08-21 09:40:31 +00004794 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004795}
Douglas Gregord57959a2009-03-27 23:10:48 +00004796
John McCalld226f652010-08-21 09:40:31 +00004797Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004798 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004799 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004800 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4801}
4802
John McCalld226f652010-08-21 09:40:31 +00004803Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004804 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004805 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004806 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004807 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004808
Douglas Gregor52591bf2009-06-24 00:54:41 +00004809 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004810 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004811 }
Mike Stump1eb44332009-09-09 15:08:12 +00004812
Douglas Gregor52591bf2009-06-24 00:54:41 +00004813 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004814
John McCalld226f652010-08-21 09:40:31 +00004815 Decl *DP = HandleDeclarator(ParentScope, D,
4816 move(TemplateParameterLists),
4817 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004818 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004819 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004820 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004821 FunctionTemplate->getTemplatedDecl());
4822 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4823 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4824 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004825}
4826
John McCall75042392010-02-11 01:33:53 +00004827/// \brief Strips various properties off an implicit instantiation
4828/// that has just been explicitly specialized.
4829static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004830 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004831
4832 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4833 FD->setInlineSpecified(false);
4834 }
4835}
4836
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004837/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004838/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004839/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004840/// new specialization/instantiation will have any effect.
4841///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004842/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004843/// instantiation.
4844///
4845/// \param NewTSK the kind of the new explicit specialization or instantiation.
4846///
4847/// \param PrevDecl the previous declaration of the entity.
4848///
4849/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4850///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004851/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004852/// declaration was instantiated (either implicitly or explicitly).
4853///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004854/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004855/// specialization or instantiation has no effect and should be ignored.
4856///
4857/// \returns true if there was an error that should prevent the introduction of
4858/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004859bool
4860Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4861 TemplateSpecializationKind NewTSK,
4862 NamedDecl *PrevDecl,
4863 TemplateSpecializationKind PrevTSK,
4864 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004865 bool &HasNoEffect) {
4866 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004867
Douglas Gregor454885e2009-10-15 15:54:05 +00004868 switch (NewTSK) {
4869 case TSK_Undeclared:
4870 case TSK_ImplicitInstantiation:
4871 assert(false && "Don't check implicit instantiations here");
4872 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004873
Douglas Gregor454885e2009-10-15 15:54:05 +00004874 case TSK_ExplicitSpecialization:
4875 switch (PrevTSK) {
4876 case TSK_Undeclared:
4877 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004878 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004879 // explicitly specialized or has merely been mentioned without any
4880 // instantiation.
4881 return false;
4882
4883 case TSK_ImplicitInstantiation:
4884 if (PrevPointOfInstantiation.isInvalid()) {
4885 // The declaration itself has not actually been instantiated, so it is
4886 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004887 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004888 return false;
4889 }
4890 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004891
Douglas Gregor454885e2009-10-15 15:54:05 +00004892 case TSK_ExplicitInstantiationDeclaration:
4893 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004894 assert((PrevTSK == TSK_ImplicitInstantiation ||
4895 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004896 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004897
Douglas Gregor454885e2009-10-15 15:54:05 +00004898 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004899 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004900 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004901 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004902 // implicit instantiation to take place, in every translation unit in
4903 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004904 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4905 // Is there any previous explicit specialization declaration?
4906 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4907 return false;
4908 }
4909
Douglas Gregor0d035142009-10-27 18:42:08 +00004910 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004911 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004912 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004913 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004914
Douglas Gregor454885e2009-10-15 15:54:05 +00004915 return true;
4916 }
4917 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004918
Douglas Gregor454885e2009-10-15 15:54:05 +00004919 case TSK_ExplicitInstantiationDeclaration:
4920 switch (PrevTSK) {
4921 case TSK_ExplicitInstantiationDeclaration:
4922 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004923 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004924 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004925
Douglas Gregor454885e2009-10-15 15:54:05 +00004926 case TSK_Undeclared:
4927 case TSK_ImplicitInstantiation:
4928 // We're explicitly instantiating something that may have already been
4929 // implicitly instantiated; that's fine.
4930 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004931
Douglas Gregor454885e2009-10-15 15:54:05 +00004932 case TSK_ExplicitSpecialization:
4933 // C++0x [temp.explicit]p4:
4934 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004935 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004936 // specialization for that template, the explicit instantiation has no
4937 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004938 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004939 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004940
Douglas Gregor454885e2009-10-15 15:54:05 +00004941 case TSK_ExplicitInstantiationDefinition:
4942 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004943 // If an entity is the subject of both an explicit instantiation
4944 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004945 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004946 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004947 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004948 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004949 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004950 assert(PrevPointOfInstantiation.isValid() &&
4951 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004952 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004953 return false;
4954 }
4955 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004956
Douglas Gregor454885e2009-10-15 15:54:05 +00004957 case TSK_ExplicitInstantiationDefinition:
4958 switch (PrevTSK) {
4959 case TSK_Undeclared:
4960 case TSK_ImplicitInstantiation:
4961 // We're explicitly instantiating something that may have already been
4962 // implicitly instantiated; that's fine.
4963 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004964
Douglas Gregor454885e2009-10-15 15:54:05 +00004965 case TSK_ExplicitSpecialization:
4966 // C++ DR 259, C++0x [temp.explicit]p4:
4967 // For a given set of template parameters, if an explicit
4968 // instantiation of a template appears after a declaration of
4969 // an explicit specialization for that template, the explicit
4970 // instantiation has no effect.
4971 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004972 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004973 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004974 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004975 if (!getLangOptions().CPlusPlus0x) {
4976 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004977 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004978 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004979 diag::note_previous_template_specialization);
4980 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004981 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004982 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004983
Douglas Gregor454885e2009-10-15 15:54:05 +00004984 case TSK_ExplicitInstantiationDeclaration:
4985 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004986 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004987 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004988
Douglas Gregor454885e2009-10-15 15:54:05 +00004989 case TSK_ExplicitInstantiationDefinition:
4990 // C++0x [temp.spec]p5:
4991 // For a given template and a given set of template-arguments,
4992 // - an explicit instantiation definition shall appear at most once
4993 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004994 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004995 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004996 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004997 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004998 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004999 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005000 }
5001 break;
5002 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005003
Douglas Gregor454885e2009-10-15 15:54:05 +00005004 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005005
Douglas Gregor454885e2009-10-15 15:54:05 +00005006 return false;
5007}
5008
John McCallaf2094e2010-04-08 09:05:18 +00005009/// \brief Perform semantic analysis for the given dependent function
5010/// template specialization. The only possible way to get a dependent
5011/// function template specialization is with a friend declaration,
5012/// like so:
5013///
5014/// template <class T> void foo(T);
5015/// template <class T> class A {
5016/// friend void foo<>(T);
5017/// };
5018///
5019/// There really isn't any useful analysis we can do here, so we
5020/// just store the information.
5021bool
5022Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5023 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5024 LookupResult &Previous) {
5025 // Remove anything from Previous that isn't a function template in
5026 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005027 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005028 LookupResult::Filter F = Previous.makeFilter();
5029 while (F.hasNext()) {
5030 NamedDecl *D = F.next()->getUnderlyingDecl();
5031 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005032 !FDLookupContext->InEnclosingNamespaceSetOf(
5033 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005034 F.erase();
5035 }
5036 F.done();
5037
5038 // Should this be diagnosed here?
5039 if (Previous.empty()) return true;
5040
5041 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5042 ExplicitTemplateArgs);
5043 return false;
5044}
5045
Abramo Bagnarae03db982010-05-20 15:32:11 +00005046/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005047/// specialization.
5048///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005049/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005050/// explicit function template specialization. On successful completion,
5051/// the function declaration \p FD will become a function template
5052/// specialization.
5053///
5054/// \param FD the function declaration, which will be updated to become a
5055/// function template specialization.
5056///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005057/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5058/// if any. Note that this may be valid info even when 0 arguments are
5059/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5060/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005061///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005062/// \param PrevDecl the set of declarations that may be specialized by
5063/// this function specialization.
5064bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005065Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005066 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005067 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005068 // The set of function template specializations that could match this
5069 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005070 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005071
Sebastian Redl7a126a42010-08-31 00:36:30 +00005072 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005073 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5074 I != E; ++I) {
5075 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5076 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005077 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005078 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005079 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5080 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005081 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005082
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005083 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005084 // A trailing template-argument can be left unspecified in the
5085 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005086 // provided it can be deduced from the function argument type.
5087 // Perform template argument deduction to determine whether we may be
5088 // specializing this template.
5089 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005090 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005091 FunctionDecl *Specialization = 0;
5092 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005093 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005094 FD->getType(),
5095 Specialization,
5096 Info)) {
5097 // FIXME: Template argument deduction failed; record why it failed, so
5098 // that we can provide nifty diagnostics.
5099 (void)TDK;
5100 continue;
5101 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005102
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005103 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005104 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005105 }
5106 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005107
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005108 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005109 UnresolvedSetIterator Result
5110 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005111 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005112 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005113 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005114 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005115 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005116 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005117 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005118 return true;
John McCallc373d482010-01-27 01:50:18 +00005119
5120 // Ignore access information; it doesn't figure into redeclaration checking.
5121 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005122
5123 FunctionTemplateSpecializationInfo *SpecInfo
5124 = Specialization->getTemplateSpecializationInfo();
5125 assert(SpecInfo && "Function template specialization info missing?");
5126 {
5127 // Note: do not overwrite location info if previous template
5128 // specialization kind was explicit.
5129 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5130 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5131 Specialization->setLocation(FD->getLocation());
5132 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005133
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005134 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005135 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005136
5137 // If this is a friend declaration, then we're not really declaring
5138 // an explicit specialization.
5139 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005140
Douglas Gregord5cb8762009-10-07 00:13:32 +00005141 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005142 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005143 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005144 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005145 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005146 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005147 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005148
5149 // C++ [temp.expl.spec]p6:
5150 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005151 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005152 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005153 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005154 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005155 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005156 if (!isFriend &&
5157 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005158 TSK_ExplicitSpecialization,
5159 Specialization,
5160 SpecInfo->getTemplateSpecializationKind(),
5161 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005162 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005163 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005164
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005165 // Mark the prior declaration as an explicit specialization, so that later
5166 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005167 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005168 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005169 MarkUnusedFileScopedDecl(Specialization);
5170 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005171
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005172 // Turn the given function declaration into a function template
5173 // specialization, with the template arguments from the previous
5174 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005175 // Take copies of (semantic and syntactic) template argument lists.
5176 const TemplateArgumentList* TemplArgs = new (Context)
5177 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5178 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5179 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005180 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005181 TemplArgs, /*InsertPos=*/0,
5182 SpecInfo->getTemplateSpecializationKind(),
5183 TemplArgsAsWritten);
5184
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005185 // The "previous declaration" for this function template specialization is
5186 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005187 Previous.clear();
5188 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005189 return false;
5190}
5191
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005192/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005193/// specialization.
5194///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005195/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005196/// explicit member function specialization. On successful completion,
5197/// the function declaration \p FD will become a member function
5198/// specialization.
5199///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005200/// \param Member the member declaration, which will be updated to become a
5201/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005202///
John McCall68263142009-11-18 22:49:29 +00005203/// \param Previous the set of declarations, one of which may be specialized
5204/// by this function specialization; the set will be modified to contain the
5205/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005206bool
John McCall68263142009-11-18 22:49:29 +00005207Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005208 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005209
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005210 // Try to find the member we are instantiating.
5211 NamedDecl *Instantiation = 0;
5212 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005213 MemberSpecializationInfo *MSInfo = 0;
5214
John McCall68263142009-11-18 22:49:29 +00005215 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005216 // Nowhere to look anyway.
5217 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005218 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5219 I != E; ++I) {
5220 NamedDecl *D = (*I)->getUnderlyingDecl();
5221 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005222 if (Context.hasSameType(Function->getType(), Method->getType())) {
5223 Instantiation = Method;
5224 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005225 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005226 break;
5227 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005228 }
5229 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005230 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005231 VarDecl *PrevVar;
5232 if (Previous.isSingleResult() &&
5233 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005234 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005235 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005236 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005237 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005238 }
5239 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005240 CXXRecordDecl *PrevRecord;
5241 if (Previous.isSingleResult() &&
5242 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5243 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005244 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005245 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005246 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005247 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005248
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005249 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005250 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005251 // specializations are always out-of-line, the caller will complain about
5252 // this mismatch later.
5253 return false;
5254 }
John McCall77e8b112010-04-13 20:37:33 +00005255
5256 // If this is a friend, just bail out here before we start turning
5257 // things into explicit specializations.
5258 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5259 // Preserve instantiation information.
5260 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5261 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5262 cast<CXXMethodDecl>(InstantiatedFrom),
5263 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5264 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5265 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5266 cast<CXXRecordDecl>(InstantiatedFrom),
5267 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5268 }
5269
5270 Previous.clear();
5271 Previous.addDecl(Instantiation);
5272 return false;
5273 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005274
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005275 // Make sure that this is a specialization of a member.
5276 if (!InstantiatedFrom) {
5277 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5278 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005279 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5280 return true;
5281 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005282
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005283 // C++ [temp.expl.spec]p6:
5284 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005285 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005286 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005287 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005288 // use occurs; no diagnostic is required.
5289 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005290
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005291 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005292 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5293 TSK_ExplicitSpecialization,
5294 Instantiation,
5295 MSInfo->getTemplateSpecializationKind(),
5296 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005297 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005298 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005299
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005300 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005301 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005302 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005303 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005304 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005305 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005306
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005307 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005308 // the original declaration to note that it is an explicit specialization
5309 // (if it was previously an implicit instantiation). This latter step
5310 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005311 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005312 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5313 if (InstantiationFunction->getTemplateSpecializationKind() ==
5314 TSK_ImplicitInstantiation) {
5315 InstantiationFunction->setTemplateSpecializationKind(
5316 TSK_ExplicitSpecialization);
5317 InstantiationFunction->setLocation(Member->getLocation());
5318 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005319
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005320 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5321 cast<CXXMethodDecl>(InstantiatedFrom),
5322 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005323 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005324 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005325 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5326 if (InstantiationVar->getTemplateSpecializationKind() ==
5327 TSK_ImplicitInstantiation) {
5328 InstantiationVar->setTemplateSpecializationKind(
5329 TSK_ExplicitSpecialization);
5330 InstantiationVar->setLocation(Member->getLocation());
5331 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005332
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005333 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5334 cast<VarDecl>(InstantiatedFrom),
5335 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005336 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005337 } else {
5338 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005339 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5340 if (InstantiationClass->getTemplateSpecializationKind() ==
5341 TSK_ImplicitInstantiation) {
5342 InstantiationClass->setTemplateSpecializationKind(
5343 TSK_ExplicitSpecialization);
5344 InstantiationClass->setLocation(Member->getLocation());
5345 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005346
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005347 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005348 cast<CXXRecordDecl>(InstantiatedFrom),
5349 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005350 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005351
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005352 // Save the caller the trouble of having to figure out which declaration
5353 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005354 Previous.clear();
5355 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005356 return false;
5357}
5358
Douglas Gregor558c0322009-10-14 23:41:34 +00005359/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005360///
5361/// \returns true if a serious error occurs, false otherwise.
5362static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005363 SourceLocation InstLoc,
5364 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005365 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5366 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005367
Douglas Gregor669eed82010-07-13 00:10:04 +00005368 if (CurContext->isRecord()) {
5369 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5370 << D;
5371 return true;
5372 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005373
Douglas Gregor558c0322009-10-14 23:41:34 +00005374 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005375 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005376 // template.
5377 //
5378 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005379 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005380 !CurContext->Encloses(OrigContext)) {
5381 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005382 S.Diag(InstLoc,
5383 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005384 diag::err_explicit_instantiation_out_of_scope
5385 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005386 << D << NS;
5387 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005388 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005389 S.getLangOptions().CPlusPlus0x?
5390 diag::err_explicit_instantiation_must_be_global
5391 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005392 << D;
5393 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005394 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005395 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005396
Douglas Gregor558c0322009-10-14 23:41:34 +00005397 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005398 // If the name declared in the explicit instantiation is an unqualified
5399 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005400 // its template is declared or, if that namespace is inline (7.3.1), any
5401 // namespace from its enclosing namespace set.
5402 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005403 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005404
5405 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005406 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005407
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005408 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005409 S.getLangOptions().CPlusPlus0x?
5410 diag::err_explicit_instantiation_unqualified_wrong_namespace
5411 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005412 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005413 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005414 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005415}
5416
5417/// \brief Determine whether the given scope specifier has a template-id in it.
5418static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5419 if (!SS.isSet())
5420 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005421
Douglas Gregor558c0322009-10-14 23:41:34 +00005422 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005423 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005424 // or a static data member of a class template specialization, the name of
5425 // the class template specialization in the qualified-id for the member
5426 // name shall be a simple-template-id.
5427 //
5428 // C++98 has the same restriction, just worded differently.
5429 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5430 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005431 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005432 if (isa<TemplateSpecializationType>(T))
5433 return true;
5434
5435 return false;
5436}
5437
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005438// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005439DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005440Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005441 SourceLocation ExternLoc,
5442 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005443 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005444 SourceLocation KWLoc,
5445 const CXXScopeSpec &SS,
5446 TemplateTy TemplateD,
5447 SourceLocation TemplateNameLoc,
5448 SourceLocation LAngleLoc,
5449 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005450 SourceLocation RAngleLoc,
5451 AttributeList *Attr) {
5452 // Find the class template we're specializing
5453 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005454 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005455 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5456
5457 // Check that the specialization uses the same tag kind as the
5458 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005459 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5460 assert(Kind != TTK_Enum &&
5461 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005462 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005463 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005464 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005465 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005466 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005467 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005468 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005469 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005470 diag::note_previous_use);
5471 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5472 }
5473
Douglas Gregor558c0322009-10-14 23:41:34 +00005474 // C++0x [temp.explicit]p2:
5475 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005476 // definition and an explicit instantiation declaration. An explicit
5477 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005478 TemplateSpecializationKind TSK
5479 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5480 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005481
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005482 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005483 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005484 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005485
5486 // Check that the template argument list is well-formed for this
5487 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005488 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005489 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5490 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005491 return true;
5492
Douglas Gregor910f8002010-11-07 23:05:16 +00005493 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005494 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005495
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005496 // Find the class template specialization declaration that
5497 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005498 void *InsertPos = 0;
5499 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005500 = ClassTemplate->findSpecialization(Converted.data(),
5501 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005502
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005503 TemplateSpecializationKind PrevDecl_TSK
5504 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5505
Douglas Gregord5cb8762009-10-07 00:13:32 +00005506 // C++0x [temp.explicit]p2:
5507 // [...] An explicit instantiation shall appear in an enclosing
5508 // namespace of its template. [...]
5509 //
5510 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005511 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5512 SS.isSet()))
5513 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005514
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005515 ClassTemplateSpecializationDecl *Specialization = 0;
5516
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005517 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005518 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005519 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005520 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005521 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005522 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005523 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005524
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005525 // Even though HasNoEffect == true means that this explicit instantiation
5526 // has no effect on semantics, we go on to put its syntax in the AST.
5527
5528 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5529 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005530 // Since the only prior class template specialization with these
5531 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005532 // declaration node as our own, updating the source location
5533 // for the template name to reflect our new declaration.
5534 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005535 Specialization = PrevDecl;
5536 Specialization->setLocation(TemplateNameLoc);
5537 PrevDecl = 0;
5538 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005539 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005540
Douglas Gregor52604ab2009-09-11 21:19:12 +00005541 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005542 // Create a new class template specialization declaration node for
5543 // this explicit specialization.
5544 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005545 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005546 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005547 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005548 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005549 Converted.data(),
5550 Converted.size(),
5551 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005552 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005553
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005554 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005555 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005556 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005557 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005558 }
5559
5560 // Build the fully-sugared type for this explicit instantiation as
5561 // the user wrote in the explicit instantiation itself. This means
5562 // that we'll pretty-print the type retrieved from the
5563 // specialization's declaration the way that the user actually wrote
5564 // the explicit instantiation, rather than formatting the name based
5565 // on the "canonical" representation used to store the template
5566 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005567 TypeSourceInfo *WrittenTy
5568 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5569 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005570 Context.getTypeDeclType(Specialization));
5571 Specialization->setTypeAsWritten(WrittenTy);
5572 TemplateArgsIn.release();
5573
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005574 // Set source locations for keywords.
5575 Specialization->setExternLoc(ExternLoc);
5576 Specialization->setTemplateKeywordLoc(TemplateLoc);
5577
5578 // Add the explicit instantiation into its lexical context. However,
5579 // since explicit instantiations are never found by name lookup, we
5580 // just put it into the declaration context directly.
5581 Specialization->setLexicalDeclContext(CurContext);
5582 CurContext->addDecl(Specialization);
5583
5584 // Syntax is now OK, so return if it has no other effect on semantics.
5585 if (HasNoEffect) {
5586 // Set the template specialization kind.
5587 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005588 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005589 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005590
5591 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005592 // A definition of a class template or class member template
5593 // shall be in scope at the point of the explicit instantiation of
5594 // the class template or class member template.
5595 //
5596 // This check comes when we actually try to perform the
5597 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005598 ClassTemplateSpecializationDecl *Def
5599 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005600 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005601 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005602 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005603 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005604 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005605 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5606 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005607
Douglas Gregor0d035142009-10-27 18:42:08 +00005608 // Instantiate the members of this class template specialization.
5609 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005610 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005611 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005612 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5613
5614 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5615 // TSK_ExplicitInstantiationDefinition
5616 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5617 TSK == TSK_ExplicitInstantiationDefinition)
5618 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005619
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005620 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005621 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005622
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005623 // Set the template specialization kind.
5624 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005625 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005626}
5627
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005628// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005629DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005630Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005631 SourceLocation ExternLoc,
5632 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005633 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005634 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005635 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005636 IdentifierInfo *Name,
5637 SourceLocation NameLoc,
5638 AttributeList *Attr) {
5639
Douglas Gregor402abb52009-05-28 23:31:59 +00005640 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005641 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005642 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005643 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5644 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005645 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005646 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005647 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5648
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005649 if (!TagD)
5650 return true;
5651
John McCalld226f652010-08-21 09:40:31 +00005652 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005653 if (Tag->isEnum()) {
5654 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5655 << Context.getTypeDeclType(Tag);
5656 return true;
5657 }
5658
Douglas Gregord0c87372009-05-27 17:30:49 +00005659 if (Tag->isInvalidDecl())
5660 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005661
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005662 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5663 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5664 if (!Pattern) {
5665 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5666 << Context.getTypeDeclType(Record);
5667 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5668 return true;
5669 }
5670
Douglas Gregor558c0322009-10-14 23:41:34 +00005671 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005672 // If the explicit instantiation is for a class or member class, the
5673 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005674 // simple-template-id.
5675 //
5676 // C++98 has the same restriction, just worded differently.
5677 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005678 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005679 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005680
Douglas Gregor558c0322009-10-14 23:41:34 +00005681 // C++0x [temp.explicit]p2:
5682 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005683 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005684 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005685 TemplateSpecializationKind TSK
5686 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5687 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005688
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005689 // C++0x [temp.explicit]p2:
5690 // [...] An explicit instantiation shall appear in an enclosing
5691 // namespace of its template. [...]
5692 //
5693 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005694 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005695
Douglas Gregor454885e2009-10-15 15:54:05 +00005696 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005697 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005698 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005699 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005700 PrevDecl = Record;
5701 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005702 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005703 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005704 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005705 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005706 PrevDecl,
5707 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005708 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005709 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005710 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005711 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005712 return TagD;
5713 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005714
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005715 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005716 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005717 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005718 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005719 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005720 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005721 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005722 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005723 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005724 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5725 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005726 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5727 << Pattern;
5728 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005729 } else {
5730 if (InstantiateClass(NameLoc, Record, Def,
5731 getTemplateInstantiationArgs(Record),
5732 TSK))
5733 return true;
5734
Douglas Gregor952b0172010-02-11 01:04:33 +00005735 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005736 if (!RecordDef)
5737 return true;
5738 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005739 }
5740
Douglas Gregor0d035142009-10-27 18:42:08 +00005741 // Instantiate all of the members of the class.
5742 InstantiateClassMembers(NameLoc, RecordDef,
5743 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005744
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005745 if (TSK == TSK_ExplicitInstantiationDefinition)
5746 MarkVTableUsed(NameLoc, RecordDef, true);
5747
Mike Stump390b4cc2009-05-16 07:39:55 +00005748 // FIXME: We don't have any representation for explicit instantiations of
5749 // member classes. Such a representation is not needed for compilation, but it
5750 // should be available for clients that want to see all of the declarations in
5751 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005752 return TagD;
5753}
5754
John McCallf312b1e2010-08-26 23:41:50 +00005755DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5756 SourceLocation ExternLoc,
5757 SourceLocation TemplateLoc,
5758 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005759 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005760 // TODO: check if/when DNInfo should replace Name.
5761 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5762 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005763 if (!Name) {
5764 if (!D.isInvalidType())
5765 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5766 diag::err_explicit_instantiation_requires_name)
5767 << D.getDeclSpec().getSourceRange()
5768 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005769
Douglas Gregord5a423b2009-09-25 18:43:00 +00005770 return true;
5771 }
5772
5773 // The scope passed in may not be a decl scope. Zip up the scope tree until
5774 // we find one that is.
5775 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5776 (S->getFlags() & Scope::TemplateParamScope) != 0)
5777 S = S->getParent();
5778
5779 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005780 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5781 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005782 if (R.isNull())
5783 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005784
Douglas Gregord5a423b2009-09-25 18:43:00 +00005785 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5786 // Cannot explicitly instantiate a typedef.
5787 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5788 << Name;
5789 return true;
5790 }
5791
Douglas Gregor663b5a02009-10-14 20:14:33 +00005792 // C++0x [temp.explicit]p1:
5793 // [...] An explicit instantiation of a function template shall not use the
5794 // inline or constexpr specifiers.
5795 // Presumably, this also applies to member functions of class templates as
5796 // well.
5797 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005798 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005799 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005800 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005801
Douglas Gregor663b5a02009-10-14 20:14:33 +00005802 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005803
Douglas Gregor558c0322009-10-14 23:41:34 +00005804 // C++0x [temp.explicit]p2:
5805 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005806 // definition and an explicit instantiation declaration. An explicit
5807 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005808 TemplateSpecializationKind TSK
5809 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5810 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005811
Abramo Bagnara25777432010-08-11 22:01:17 +00005812 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005813 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005814
5815 if (!R->isFunctionType()) {
5816 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005817 // A [...] static data member of a class template can be explicitly
5818 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005819 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005820 if (Previous.isAmbiguous())
5821 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005822
John McCall1bcee0a2009-12-02 08:25:40 +00005823 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005824 if (!Prev || !Prev->isStaticDataMember()) {
5825 // We expect to see a data data member here.
5826 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5827 << Name;
5828 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5829 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005830 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005831 return true;
5832 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005833
Douglas Gregord5a423b2009-09-25 18:43:00 +00005834 if (!Prev->getInstantiatedFromStaticDataMember()) {
5835 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005836 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005837 diag::err_explicit_instantiation_data_member_not_instantiated)
5838 << Prev;
5839 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5840 // FIXME: Can we provide a note showing where this was declared?
5841 return true;
5842 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005843
Douglas Gregor558c0322009-10-14 23:41:34 +00005844 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005845 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005846 // or a static data member of a class template specialization, the name of
5847 // the class template specialization in the qualified-id for the member
5848 // name shall be a simple-template-id.
5849 //
5850 // C++98 has the same restriction, just worded differently.
5851 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005852 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005853 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005854 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005855
Douglas Gregor558c0322009-10-14 23:41:34 +00005856 // Check the scope of this explicit instantiation.
5857 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005858
Douglas Gregor454885e2009-10-15 15:54:05 +00005859 // Verify that it is okay to explicitly instantiate here.
5860 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5861 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005862 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005863 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005864 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005865 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005866 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005867 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005868 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005869 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005870
Douglas Gregord5a423b2009-09-25 18:43:00 +00005871 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005872 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005873 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005874 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005875
Douglas Gregord5a423b2009-09-25 18:43:00 +00005876 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005877 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005878 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005879
5880 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005881 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005882 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005883 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005884 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5885 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005886 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5887 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005888 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5889 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005890 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005891 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005892 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005893 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005894 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005895
Douglas Gregord5a423b2009-09-25 18:43:00 +00005896 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005897 // A [...] function [...] can be explicitly instantiated from its template.
5898 // A member function [...] of a class template can be explicitly
5899 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005900 // template.
John McCallc373d482010-01-27 01:50:18 +00005901 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005902 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5903 P != PEnd; ++P) {
5904 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005905 if (!HasExplicitTemplateArgs) {
5906 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5907 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5908 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005909
John McCallc373d482010-01-27 01:50:18 +00005910 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005911 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5912 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005913 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005914 }
5915 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005916
Douglas Gregord5a423b2009-09-25 18:43:00 +00005917 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5918 if (!FunTmpl)
5919 continue;
5920
John McCall5769d612010-02-08 23:07:23 +00005921 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005922 FunctionDecl *Specialization = 0;
5923 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005924 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005925 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005926 R, Specialization, Info)) {
5927 // FIXME: Keep track of almost-matches?
5928 (void)TDK;
5929 continue;
5930 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005931
John McCallc373d482010-01-27 01:50:18 +00005932 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005933 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005934
Douglas Gregord5a423b2009-09-25 18:43:00 +00005935 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005936 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005937 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005938 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005939 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5940 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5941 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005942
John McCallc373d482010-01-27 01:50:18 +00005943 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005944 return true;
John McCallc373d482010-01-27 01:50:18 +00005945
5946 // Ignore access control bits, we don't need them for redeclaration checking.
5947 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005948
Douglas Gregor0a897e32009-10-15 17:21:20 +00005949 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005950 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005951 diag::err_explicit_instantiation_member_function_not_instantiated)
5952 << Specialization
5953 << (Specialization->getTemplateSpecializationKind() ==
5954 TSK_ExplicitSpecialization);
5955 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5956 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005957 }
5958
Douglas Gregor0a897e32009-10-15 17:21:20 +00005959 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005960 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5961 PrevDecl = Specialization;
5962
Douglas Gregor0a897e32009-10-15 17:21:20 +00005963 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005964 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005965 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005966 PrevDecl,
5967 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005968 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005969 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005970 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005971
Douglas Gregor0a897e32009-10-15 17:21:20 +00005972 // FIXME: We may still want to build some representation of this
5973 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005974 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005975 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005976 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005977
5978 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005979
Douglas Gregor0a897e32009-10-15 17:21:20 +00005980 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005981 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005982
Douglas Gregor558c0322009-10-14 23:41:34 +00005983 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005984 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005985 // or a static data member of a class template specialization, the name of
5986 // the class template specialization in the qualified-id for the member
5987 // name shall be a simple-template-id.
5988 //
5989 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005990 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005991 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005992 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005993 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005994 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005995 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005996 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005997
Douglas Gregor558c0322009-10-14 23:41:34 +00005998 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005999 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006000 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006001 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006002 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006003
Douglas Gregord5a423b2009-09-25 18:43:00 +00006004 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006005 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006006}
6007
John McCallf312b1e2010-08-26 23:41:50 +00006008TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006009Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6010 const CXXScopeSpec &SS, IdentifierInfo *Name,
6011 SourceLocation TagLoc, SourceLocation NameLoc) {
6012 // This has to hold, because SS is expected to be defined.
6013 assert(Name && "Expected a name in a dependent tag");
6014
6015 NestedNameSpecifier *NNS
6016 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6017 if (!NNS)
6018 return true;
6019
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006020 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006021
Douglas Gregor48c89f42010-04-24 16:38:41 +00006022 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6023 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006024 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006025 return true;
6026 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006027
Douglas Gregor059101f2011-03-02 00:47:37 +00006028 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006029 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006030 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6031
6032 // Create type-source location information for this type.
6033 TypeLocBuilder TLB;
6034 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6035 TL.setKeywordLoc(TagLoc);
6036 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6037 TL.setNameLoc(NameLoc);
6038 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006039}
6040
John McCallf312b1e2010-08-26 23:41:50 +00006041TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006042Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6043 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006044 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006045 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006046 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006047
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006048 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6049 !getLangOptions().CPlusPlus0x)
6050 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006051 << FixItHint::CreateRemoval(TypenameLoc);
6052
Douglas Gregor2494dd02011-03-01 01:34:45 +00006053 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006054 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6055 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006056 if (T.isNull())
6057 return true;
John McCall63b43852010-04-29 23:50:39 +00006058
6059 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6060 if (isa<DependentNameType>(T)) {
6061 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006062 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006063 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006064 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006065 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006066 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006067 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006068 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006069 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006070 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006071
John McCallb3d87482010-08-24 05:47:05 +00006072 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006073}
6074
John McCallf312b1e2010-08-26 23:41:50 +00006075TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006076Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6077 const CXXScopeSpec &SS,
6078 SourceLocation TemplateLoc,
6079 TemplateTy TemplateIn,
6080 SourceLocation TemplateNameLoc,
6081 SourceLocation LAngleLoc,
6082 ASTTemplateArgsPtr TemplateArgsIn,
6083 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006084 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6085 !getLangOptions().CPlusPlus0x)
6086 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006087 << FixItHint::CreateRemoval(TypenameLoc);
6088
6089 // Translate the parser's template argument list in our AST format.
6090 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6091 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6092
6093 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006094 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6095 // Construct a dependent template specialization type.
6096 assert(DTN && "dependent template has non-dependent name?");
6097 assert(DTN->getQualifier()
6098 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6099 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6100 DTN->getQualifier(),
6101 DTN->getIdentifier(),
6102 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006103
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006104 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006105 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006106 DependentTemplateSpecializationTypeLoc SpecTL
6107 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006108 SpecTL.setLAngleLoc(LAngleLoc);
6109 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006110 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006111 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006112 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006113 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6114 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006115 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006116 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006117
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006118 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6119 if (T.isNull())
6120 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006121
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006122 // Provide source-location information for the template specialization
6123 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006124 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006125 TemplateSpecializationTypeLoc SpecTL
6126 = Builder.push<TemplateSpecializationTypeLoc>(T);
6127
6128 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006129 SpecTL.setLAngleLoc(LAngleLoc);
6130 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006131 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006132 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6133 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6134
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006135 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6136 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6137 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006138 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6139
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006140 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6141 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006142}
6143
Douglas Gregora02411e2011-02-27 22:46:49 +00006144
Douglas Gregord57959a2009-03-27 23:10:48 +00006145/// \brief Build the type that describes a C++ typename specifier,
6146/// e.g., "typename T::type".
6147QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006148Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6149 SourceLocation KeywordLoc,
6150 NestedNameSpecifierLoc QualifierLoc,
6151 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006152 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006153 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006154 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006155
John McCall77bb1aa2010-05-01 00:40:08 +00006156 DeclContext *Ctx = computeDeclContext(SS);
6157 if (!Ctx) {
6158 // If the nested-name-specifier is dependent and couldn't be
6159 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006160 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6161 return Context.getDependentNameType(Keyword,
6162 QualifierLoc.getNestedNameSpecifier(),
6163 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006164 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006165
John McCall77bb1aa2010-05-01 00:40:08 +00006166 // If the nested-name-specifier refers to the current instantiation,
6167 // the "typename" keyword itself is superfluous. In C++03, the
6168 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6169 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006170 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006171
John McCall77bb1aa2010-05-01 00:40:08 +00006172 if (RequireCompleteDeclContext(SS, Ctx))
6173 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006174
6175 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006176 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006177 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006178 unsigned DiagID = 0;
6179 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006180 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006181 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006182 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006183 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006184
6185 case LookupResult::FoundUnresolvedValue: {
6186 // We found a using declaration that is a value. Most likely, the using
6187 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006188 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006189 IILoc);
6190 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6191 << Name << Ctx << FullRange;
6192 if (UnresolvedUsingValueDecl *Using
6193 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006194 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006195 Diag(Loc, diag::note_using_value_decl_missing_typename)
6196 << FixItHint::CreateInsertion(Loc, "typename ");
6197 }
6198 }
6199 // Fall through to create a dependent typename type, from which we can recover
6200 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006201
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006202 case LookupResult::NotFoundInCurrentInstantiation:
6203 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006204 return Context.getDependentNameType(Keyword,
6205 QualifierLoc.getNestedNameSpecifier(),
6206 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006207
6208 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006209 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006210 // We found a type. Build an ElaboratedType, since the
6211 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006212 return Context.getElaboratedType(ETK_Typename,
6213 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006214 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006215 }
6216
6217 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006218 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006219 break;
6220
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006221
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006222 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006223 return QualType();
6224
Douglas Gregord57959a2009-03-27 23:10:48 +00006225 case LookupResult::FoundOverloaded:
6226 DiagID = diag::err_typename_nested_not_type;
6227 Referenced = *Result.begin();
6228 break;
6229
John McCall6e247262009-10-10 05:48:19 +00006230 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006231 return QualType();
6232 }
6233
6234 // If we get here, it's because name lookup did not find a
6235 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006236 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006237 IILoc);
6238 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006239 if (Referenced)
6240 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6241 << Name;
6242 return QualType();
6243}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006244
6245namespace {
6246 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006247 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006248 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006249 SourceLocation Loc;
6250 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006251
Douglas Gregor4a959d82009-08-06 16:20:37 +00006252 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006253 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006254
Mike Stump1eb44332009-09-09 15:08:12 +00006255 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006256 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006257 DeclarationName Entity)
6258 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006259 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006260
6261 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006262 /// transformed.
6263 ///
6264 /// For the purposes of type reconstruction, a type has already been
6265 /// transformed if it is NULL or if it is not dependent.
6266 bool AlreadyTransformed(QualType T) {
6267 return T.isNull() || !T->isDependentType();
6268 }
Mike Stump1eb44332009-09-09 15:08:12 +00006269
6270 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006271 /// rebuilt.
6272 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006273
Douglas Gregor4a959d82009-08-06 16:20:37 +00006274 /// \brief Returns the name of the entity whose type is being rebuilt.
6275 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006276
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006277 /// \brief Sets the "base" location and entity when that
6278 /// information is known based on another transformation.
6279 void setBase(SourceLocation Loc, DeclarationName Entity) {
6280 this->Loc = Loc;
6281 this->Entity = Entity;
6282 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006283 };
6284}
6285
Douglas Gregor4a959d82009-08-06 16:20:37 +00006286/// \brief Rebuilds a type within the context of the current instantiation.
6287///
Mike Stump1eb44332009-09-09 15:08:12 +00006288/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006289/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006290/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006291/// partial specialization thereof). This routine will rebuild that type now
6292/// that we have entered the declarator's scope, which may produce different
6293/// canonical types, e.g.,
6294///
6295/// \code
6296/// template<typename T>
6297/// struct X {
6298/// typedef T* pointer;
6299/// pointer data();
6300/// };
6301///
6302/// template<typename T>
6303/// typename X<T>::pointer X<T>::data() { ... }
6304/// \endcode
6305///
Douglas Gregor4714c122010-03-31 17:34:00 +00006306/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006307/// since we do not know that we can look into X<T> when we parsed the type.
6308/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006309/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006310/// as the canonical type of T*, allowing the return types of the out-of-line
6311/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006312TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6313 SourceLocation Loc,
6314 DeclarationName Name) {
6315 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006316 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006317
Douglas Gregor4a959d82009-08-06 16:20:37 +00006318 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6319 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006320}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006321
John McCall60d7b3a2010-08-24 06:29:42 +00006322ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006323 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6324 DeclarationName());
6325 return Rebuilder.TransformExpr(E);
6326}
6327
John McCall63b43852010-04-29 23:50:39 +00006328bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006329 if (SS.isInvalid())
6330 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006331
Douglas Gregor7e384942011-02-25 16:07:42 +00006332 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006333 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6334 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006335 NestedNameSpecifierLoc Rebuilt
6336 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6337 if (!Rebuilt)
6338 return true;
John McCall63b43852010-04-29 23:50:39 +00006339
Douglas Gregor7e384942011-02-25 16:07:42 +00006340 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006341 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006342}
6343
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006344/// \brief Produces a formatted string that describes the binding of
6345/// template parameters to template arguments.
6346std::string
6347Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6348 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006349 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006350}
6351
6352std::string
6353Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6354 const TemplateArgument *Args,
6355 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006356 llvm::SmallString<128> Str;
6357 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006358
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006359 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006360 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006361
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006362 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006363 if (I >= NumArgs)
6364 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006365
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006366 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006367 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006368 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006369 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006370
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006371 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006372 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006373 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006374 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006375 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006376
Douglas Gregor87dd6972010-12-20 16:52:59 +00006377 Out << " = ";
6378 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006379 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006380
6381 Out << ']';
6382 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006383}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006384
6385void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6386 if (!FD)
6387 return;
6388 FD->setLateTemplateParsed(Flag);
6389}
6390
6391bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6392 DeclContext *DC = CurContext;
6393
6394 while (DC) {
6395 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6396 const FunctionDecl *FD = RD->isLocalClass();
6397 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6398 } else if (DC->isTranslationUnit() || DC->isNamespace())
6399 return false;
6400
6401 DC = DC->getParent();
6402 }
6403 return false;
6404}