blob: aaadac2e5f443e79c87d9fb41e57d42706490214 [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
Douglas Gregor312eadb2011-04-24 05:37:28 +000079void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
Douglas Gregor312eadb2011-04-24 05:37:28 +000085 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
Richard Smith3e4c6c42011-05-05 21:57:07 +000095 // is used as a template-name, the reference refers to the class
96 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +000098 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
99 if (!ClassTemplates.insert(ClassTmpl)) {
100 filter.erase();
101 continue;
102 }
John McCall8ba66912010-08-13 07:02:08 +0000103
104 // FIXME: we promote access to public here as a workaround to
105 // the fact that LookupResult doesn't let us remember that we
106 // found this template through a particular injected class name,
107 // which means we end up doing nasty things to the invariants.
108 // Pretending that access is public is *much* safer.
109 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000110 }
John McCallf7a1a742009-11-24 19:00:30 +0000111 }
112 filter.done();
113}
114
Douglas Gregor312eadb2011-04-24 05:37:28 +0000115bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
116 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
117 if (isAcceptableTemplateName(Context, *I))
118 return true;
119
Douglas Gregor3b887352011-04-27 04:48:22 +0000120 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000121}
122
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000124 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000125 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000126 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000127 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000128 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000129 TemplateTy &TemplateResult,
130 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000131 assert(getLangOptions().CPlusPlus && "No template names in C!");
132
Douglas Gregor014e88d2009-11-03 23:16:33 +0000133 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000134 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000135
Douglas Gregor014e88d2009-11-03 23:16:33 +0000136 switch (Name.getKind()) {
137 case UnqualifiedId::IK_Identifier:
138 TName = DeclarationName(Name.Identifier);
139 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000140
Douglas Gregor014e88d2009-11-03 23:16:33 +0000141 case UnqualifiedId::IK_OperatorFunctionId:
142 TName = Context.DeclarationNames.getCXXOperatorName(
143 Name.OperatorFunctionId.Operator);
144 break;
145
Sean Hunte6252d12009-11-28 08:58:14 +0000146 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000147 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
148 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000149
Douglas Gregor014e88d2009-11-03 23:16:33 +0000150 default:
151 return TNK_Non_template;
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
John McCallb3d87482010-08-24 05:47:05 +0000154 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000156 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000157 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000158 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
159 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000160 if (R.empty()) return TNK_Non_template;
161 if (R.isAmbiguous()) {
162 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000163 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000164
165 // FIXME: we might have ambiguous templates, in which case we
166 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000167 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000168 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000169
John McCall0bd6feb2009-12-02 08:04:21 +0000170 TemplateName Template;
171 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
John McCall0bd6feb2009-12-02 08:04:21 +0000173 unsigned ResultCount = R.end() - R.begin();
174 if (ResultCount > 1) {
175 // We assume that we'll preserve the qualifier from a function
176 // template name in other ways.
177 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
178 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000179
180 // We'll do this lookup again later.
181 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000182 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000183 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
184
185 if (SS.isSet() && !SS.isInvalid()) {
186 NestedNameSpecifier *Qualifier
187 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000188 Template = Context.getQualifiedTemplateName(Qualifier,
189 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000190 } else {
191 Template = TemplateName(TD);
192 }
193
John McCallb8592062010-08-13 02:23:42 +0000194 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000195 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000196
197 // We'll do this lookup again later.
198 R.suppressDiagnostics();
199 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000200 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
201 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000202 TemplateKind = TNK_Type_template;
203 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
John McCall0bd6feb2009-12-02 08:04:21 +0000206 TemplateResult = TemplateTy::make(Template);
207 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000208}
209
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000210bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000211 SourceLocation IILoc,
212 Scope *S,
213 const CXXScopeSpec *SS,
214 TemplateTy &SuggestedTemplate,
215 TemplateNameKind &SuggestedKind) {
216 // We can't recover unless there's a dependent scope specifier preceding the
217 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000218 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000219 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
220 computeDeclContext(*SS))
221 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 // The code is missing a 'template' keyword prior to the dependent template
224 // name.
225 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
226 Diag(IILoc, diag::err_template_kw_missing)
227 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000228 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000229 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000230 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
231 SuggestedKind = TNK_Dependent_template_name;
232 return true;
233}
234
John McCallf7a1a742009-11-24 19:00:30 +0000235void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000236 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000237 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000238 bool EnteringContext,
239 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000240 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000241 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000242 DeclContext *LookupCtx = 0;
243 bool isDependent = false;
244 if (!ObjectType.isNull()) {
245 // This nested-name-specifier occurs in a member access expression, e.g.,
246 // x->B::f, and we are looking into the type of the object.
247 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
248 LookupCtx = computeDeclContext(ObjectType);
249 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000251 "Caller should have completed object type");
252 } else if (SS.isSet()) {
253 // This nested-name-specifier occurs after another nested-name-specifier,
254 // so long into the context associated with the prior nested-name-specifier.
255 LookupCtx = computeDeclContext(SS, EnteringContext);
256 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000257
John McCallf7a1a742009-11-24 19:00:30 +0000258 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000259 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000260 return;
261 }
262
263 bool ObjectTypeSearchedInScope = false;
264 if (LookupCtx) {
265 // Perform "qualified" name lookup into the declaration context we
266 // computed, which is either the type of the base of a member access
267 // expression or the declaration context associated with a prior
268 // nested-name-specifier.
269 LookupQualifiedName(Found, LookupCtx);
270
271 if (!ObjectType.isNull() && Found.empty()) {
272 // C++ [basic.lookup.classref]p1:
273 // In a class member access expression (5.2.5), if the . or -> token is
274 // immediately followed by an identifier followed by a <, the
275 // identifier must be looked up to determine whether the < is the
276 // beginning of a template argument list (14.2) or a less-than operator.
277 // The identifier is first looked up in the class of the object
278 // expression. If the identifier is not found, it is then looked up in
279 // the context of the entire postfix-expression and shall name a class
280 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000281 if (S) LookupName(Found, S);
282 ObjectTypeSearchedInScope = true;
283 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000284 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000285 // We cannot look into a dependent object type or nested nme
286 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000287 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000288 return;
289 } else {
290 // Perform unqualified name lookup in the current scope.
291 LookupName(Found, S);
292 }
293
Douglas Gregor2e933882010-01-12 17:06:20 +0000294 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000295 // If we did not find any names, attempt to correct any typos.
296 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000297 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000298 false, CTC_CXXCasts)) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000299 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000300 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000301 if (LookupCtx)
302 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
303 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000304 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000305 Found.getLookupName().getAsString());
306 else
307 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
308 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000309 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000310 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000311 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
312 Diag(Template->getLocation(), diag::note_previous_decl)
313 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000314 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000315 } else {
316 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000317 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000318 }
319 }
320
Douglas Gregor312eadb2011-04-24 05:37:28 +0000321 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000322 if (Found.empty()) {
323 if (isDependent)
324 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000325 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000326 }
John McCallf7a1a742009-11-24 19:00:30 +0000327
328 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
329 // C++ [basic.lookup.classref]p1:
330 // [...] If the lookup in the class of the object expression finds a
331 // template, the name is also looked up in the context of the entire
332 // postfix-expression and [...]
333 //
334 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
335 LookupOrdinaryName);
336 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000337 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000338
John McCallf7a1a742009-11-24 19:00:30 +0000339 if (FoundOuter.empty()) {
340 // - if the name is not found, the name found in the class of the
341 // object expression is used, otherwise
342 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
343 // - if the name is found in the context of the entire
344 // postfix-expression and does not name a class template, the name
345 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000346 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000347 // - if the name found is a class template, it must refer to the same
348 // entity as the one found in the class of the object expression,
349 // otherwise the program is ill-formed.
350 if (!Found.isSingleResult() ||
351 Found.getFoundDecl()->getCanonicalDecl()
352 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000353 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000354 diag::ext_nested_name_member_ref_lookup_ambiguous)
355 << Found.getLookupName()
356 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000357 Diag(Found.getRepresentativeDecl()->getLocation(),
358 diag::note_ambig_member_ref_object_type)
359 << ObjectType;
360 Diag(FoundOuter.getFoundDecl()->getLocation(),
361 diag::note_ambig_member_ref_scope);
362
363 // Recover by taking the template that we found in the object
364 // expression's type.
365 }
366 }
367 }
368}
369
John McCall2f841ba2009-12-02 03:53:29 +0000370/// ActOnDependentIdExpression - Handle a dependent id-expression that
371/// was just parsed. This is only possible with an explicit scope
372/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000373ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000374Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000375 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000376 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000377 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000378 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000379
John McCall2f841ba2009-12-02 03:53:29 +0000380 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000381 isa<CXXMethodDecl>(DC) &&
382 cast<CXXMethodDecl>(DC)->isInstance()) {
383 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000384
John McCallf7a1a742009-11-24 19:00:30 +0000385 // Since the 'this' expression is synthesized, we don't need to
386 // perform the double-lookup check.
387 NamedDecl *FirstQualifierInScope = 0;
388
John McCallaa81e162009-12-01 22:10:20 +0000389 return Owned(CXXDependentScopeMemberExpr::Create(Context,
390 /*This*/ 0, ThisType,
391 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000392 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000393 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000394 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000395 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000396 TemplateArgs));
397 }
398
Abramo Bagnara25777432010-08-11 22:01:17 +0000399 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000400}
401
John McCall60d7b3a2010-08-24 06:29:42 +0000402ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000403Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000404 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000405 const TemplateArgumentListInfo *TemplateArgs) {
406 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000407 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000408 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000409 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000410}
411
Douglas Gregor72c3f312008-12-05 18:15:24 +0000412/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
413/// that the template parameter 'PrevDecl' is being shadowed by a new
414/// declaration at location Loc. Returns true to indicate that this is
415/// an error, and false otherwise.
416bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000417 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000418
419 // Microsoft Visual C++ permits template parameters to be shadowed.
420 if (getLangOptions().Microsoft)
421 return false;
422
423 // C++ [temp.local]p4:
424 // A template-parameter shall not be redeclared within its
425 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000426 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000427 << cast<NamedDecl>(PrevDecl)->getDeclName();
428 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
429 return true;
430}
431
Douglas Gregor2943aed2009-03-03 04:44:36 +0000432/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000433/// the parameter D to reference the templated declaration and return a pointer
434/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000435TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
436 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
437 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000438 return Temp;
439 }
440 return 0;
441}
442
Douglas Gregorba68eca2011-01-05 17:40:24 +0000443ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
444 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000445 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000446 "Only template template arguments can be pack expansions here");
447 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
448 "Template template argument pack expansion without packs");
449 ParsedTemplateArgument Result(*this);
450 Result.EllipsisLoc = EllipsisLoc;
451 return Result;
452}
453
Douglas Gregor788cd062009-11-11 01:00:40 +0000454static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
455 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000456
Douglas Gregor788cd062009-11-11 01:00:40 +0000457 switch (Arg.getKind()) {
458 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000459 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000460 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000461 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000462 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000463 return TemplateArgumentLoc(TemplateArgument(T), DI);
464 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000465
Douglas Gregor788cd062009-11-11 01:00:40 +0000466 case ParsedTemplateArgument::NonType: {
467 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
468 return TemplateArgumentLoc(TemplateArgument(E), E);
469 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000470
Douglas Gregor788cd062009-11-11 01:00:40 +0000471 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000472 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000473 TemplateArgument TArg;
474 if (Arg.getEllipsisLoc().isValid())
475 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
476 else
477 TArg = Template;
478 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000479 Arg.getScopeSpec().getWithLocInContext(
480 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000481 Arg.getLocation(),
482 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000483 }
484 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000485
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000486 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000487 return TemplateArgumentLoc();
488}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000489
Douglas Gregor788cd062009-11-11 01:00:40 +0000490/// \brief Translates template arguments as provided by the parser
491/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000492void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
493 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000494 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000495 TemplateArgs.addArgument(translateTemplateArgument(*this,
496 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000497}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000498
Douglas Gregor72c3f312008-12-05 18:15:24 +0000499/// ActOnTypeParameter - Called when a C++ template type parameter
500/// (e.g., "typename T") has been parsed. Typename specifies whether
501/// the keyword "typename" was used to declare the type parameter
502/// (otherwise, "class" was used), and KeyLoc is the location of the
503/// "class" or "typename" keyword. ParamName is the name of the
504/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000505/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000506/// If the type parameter has a default argument, it will be added
507/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000508Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
509 SourceLocation EllipsisLoc,
510 SourceLocation KeyLoc,
511 IdentifierInfo *ParamName,
512 SourceLocation ParamNameLoc,
513 unsigned Depth, unsigned Position,
514 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000515 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000516 assert(S->isTemplateParamScope() &&
517 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000518 bool Invalid = false;
519
520 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000521 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000522 LookupOrdinaryName,
523 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000524 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000525 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000526 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000527 }
528
Douglas Gregorddc29e12009-02-06 22:42:48 +0000529 SourceLocation Loc = ParamNameLoc;
530 if (!ParamName)
531 Loc = KeyLoc;
532
Douglas Gregor72c3f312008-12-05 18:15:24 +0000533 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000534 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000535 KeyLoc, Loc, Depth, Position, ParamName,
536 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000537 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000538 if (Invalid)
539 Param->setInvalidDecl();
540
541 if (ParamName) {
542 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000543 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000544 IdResolver.AddDecl(Param);
545 }
546
Douglas Gregor61c4d282011-01-05 15:48:55 +0000547 // C++0x [temp.param]p9:
548 // A default template-argument may be specified for any kind of
549 // template-parameter that is not a template parameter pack.
550 if (DefaultArg && Ellipsis) {
551 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
552 DefaultArg = ParsedType();
553 }
554
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000555 // Handle the default argument, if provided.
556 if (DefaultArg) {
557 TypeSourceInfo *DefaultTInfo;
558 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000559
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000560 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000561
Douglas Gregor6f526752010-12-16 08:48:57 +0000562 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000563 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000564 UPPC_DefaultArgument))
565 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000567 // Check the template argument itself.
568 if (CheckTemplateArgument(Param, DefaultTInfo)) {
569 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000570 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000571 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000572
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000573 Param->setDefaultArgument(DefaultTInfo, false);
574 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000575
John McCalld226f652010-08-21 09:40:31 +0000576 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000577}
578
Douglas Gregor2943aed2009-03-03 04:44:36 +0000579/// \brief Check that the type of a non-type template parameter is
580/// well-formed.
581///
582/// \returns the (possibly-promoted) parameter type if valid;
583/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000584QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000585Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000586 // We don't allow variably-modified types as the type of non-type template
587 // parameters.
588 if (T->isVariablyModifiedType()) {
589 Diag(Loc, diag::err_variably_modified_nontype_template_param)
590 << T;
591 return QualType();
592 }
593
Douglas Gregor2943aed2009-03-03 04:44:36 +0000594 // C++ [temp.param]p4:
595 //
596 // A non-type template-parameter shall have one of the following
597 // (optionally cv-qualified) types:
598 //
599 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000600 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000601 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000602 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000603 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000604 T->isReferenceType() ||
605 // -- pointer to member.
606 T->isMemberPointerType() ||
607 // If T is a dependent type, we can't do the check now, so we
608 // assume that it is well-formed.
609 T->isDependentType())
610 return T;
611 // C++ [temp.param]p8:
612 //
613 // A non-type template-parameter of type "array of T" or
614 // "function returning T" is adjusted to be of type "pointer to
615 // T" or "pointer to function returning T", respectively.
616 else if (T->isArrayType())
617 // FIXME: Keep the type prior to promotion?
618 return Context.getArrayDecayedType(T);
619 else if (T->isFunctionType())
620 // FIXME: Keep the type prior to promotion?
621 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000622
Douglas Gregor2943aed2009-03-03 04:44:36 +0000623 Diag(Loc, diag::err_template_nontype_parm_bad_type)
624 << T;
625
626 return QualType();
627}
628
John McCalld226f652010-08-21 09:40:31 +0000629Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
630 unsigned Depth,
631 unsigned Position,
632 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000633 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000634 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
635 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000636
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000637 assert(S->isTemplateParamScope() &&
638 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000639 bool Invalid = false;
640
641 IdentifierInfo *ParamName = D.getIdentifier();
642 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000643 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000644 LookupOrdinaryName,
645 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000646 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000647 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000648 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000649 }
650
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000651 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
652 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000653 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000654 Invalid = true;
655 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000656
Douglas Gregor10738d32010-12-23 23:51:58 +0000657 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000658 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000659 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000660 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000661 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000662 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000663 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000664 Param->setAccess(AS_public);
665
Douglas Gregor72c3f312008-12-05 18:15:24 +0000666 if (Invalid)
667 Param->setInvalidDecl();
668
669 if (D.getIdentifier()) {
670 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000671 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000672 IdResolver.AddDecl(Param);
673 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000674
Douglas Gregor61c4d282011-01-05 15:48:55 +0000675 // C++0x [temp.param]p9:
676 // A default template-argument may be specified for any kind of
677 // template-parameter that is not a template parameter pack.
678 if (Default && IsParameterPack) {
679 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
680 Default = 0;
681 }
682
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000683 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000684 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000685 // Check for unexpanded parameter packs.
686 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
687 return Param;
688
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000689 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000690 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
691 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000692 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000693 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000694 }
John Wiegley429bb272011-04-08 18:41:53 +0000695 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000696
John McCall9ae2f072010-08-23 23:25:46 +0000697 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000698 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000699
John McCalld226f652010-08-21 09:40:31 +0000700 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000701}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000702
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000703/// ActOnTemplateTemplateParameter - Called when a C++ template template
704/// parameter (e.g. T in template <template <typename> class T> class array)
705/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000706Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
707 SourceLocation TmpLoc,
708 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000709 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000710 IdentifierInfo *Name,
711 SourceLocation NameLoc,
712 unsigned Depth,
713 unsigned Position,
714 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000715 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000716 assert(S->isTemplateParamScope() &&
717 "Template template parameter not in template parameter scope!");
718
719 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000720 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000721 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000722 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000723 NameLoc.isInvalid()? TmpLoc : NameLoc,
724 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000725 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000726 Param->setAccess(AS_public);
727
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000728 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000729 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000730 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000731 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000732 IdResolver.AddDecl(Param);
733 }
734
Douglas Gregor6f526752010-12-16 08:48:57 +0000735 if (Params->size() == 0) {
736 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
737 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
738 Param->setInvalidDecl();
739 }
740
Douglas Gregor61c4d282011-01-05 15:48:55 +0000741 // C++0x [temp.param]p9:
742 // A default template-argument may be specified for any kind of
743 // template-parameter that is not a template parameter pack.
744 if (IsParameterPack && !Default.isInvalid()) {
745 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
746 Default = ParsedTemplateArgument();
747 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000748
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000749 if (!Default.isInvalid()) {
750 // Check only that we have a template template argument. We don't want to
751 // try to check well-formedness now, because our template template parameter
752 // might have dependent types in its template parameters, which we wouldn't
753 // be able to match now.
754 //
755 // If none of the template template parameter's template arguments mention
756 // other template parameters, we could actually perform more checking here.
757 // However, it isn't worth doing.
758 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
759 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
760 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
761 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000762 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000763 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000764
Douglas Gregor6f526752010-12-16 08:48:57 +0000765 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000766 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000767 DefaultArg.getArgument().getAsTemplate(),
768 UPPC_DefaultArgument))
769 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000770
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000771 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000773
John McCalld226f652010-08-21 09:40:31 +0000774 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000775}
776
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000777/// ActOnTemplateParameterList - Builds a TemplateParameterList that
778/// contains the template parameters in Params/NumParams.
779Sema::TemplateParamsTy *
780Sema::ActOnTemplateParameterList(unsigned Depth,
781 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000782 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000783 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000784 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000785 SourceLocation RAngleLoc) {
786 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000787 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000788
Douglas Gregorddc29e12009-02-06 22:42:48 +0000789 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000790 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000791 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000792}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000793
John McCallb6217662010-03-15 10:12:16 +0000794static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
795 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000796 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000797}
798
John McCallf312b1e2010-08-26 23:41:50 +0000799DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000800Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000801 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000802 IdentifierInfo *Name, SourceLocation NameLoc,
803 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000804 TemplateParameterList *TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000805 AccessSpecifier AS,
806 unsigned NumOuterTemplateParamLists,
807 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000808 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000809 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000810 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000811 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000812
813 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000814 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000815 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000816
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000817 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
818 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000819
820 // There is no such thing as an unnamed class template.
821 if (!Name) {
822 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000823 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000824 }
825
826 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000827 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000828 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000829 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000830 if (SS.isNotEmpty() && !SS.isInvalid()) {
831 SemanticContext = computeDeclContext(SS, true);
832 if (!SemanticContext) {
833 // FIXME: Produce a reasonable diagnostic here
834 return true;
835 }
Mike Stump1eb44332009-09-09 15:08:12 +0000836
John McCall77bb1aa2010-05-01 00:40:08 +0000837 if (RequireCompleteDeclContext(SS, SemanticContext))
838 return true;
839
John McCalla24dc2e2009-11-17 02:14:36 +0000840 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000841 } else {
842 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000843 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Douglas Gregor57265e32010-04-12 16:00:01 +0000846 if (Previous.isAmbiguous())
847 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000848
Douglas Gregorddc29e12009-02-06 22:42:48 +0000849 NamedDecl *PrevDecl = 0;
850 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000851 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000852
Douglas Gregorddc29e12009-02-06 22:42:48 +0000853 // If there is a previous declaration with the same name, check
854 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000855 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000856 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000857
858 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000859 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000860 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000861 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000862 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
863 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000864 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000865 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
866 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
867 PrevClassTemplate
868 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
869 ->getSpecializedTemplate();
870 }
871 }
872
John McCall65c49462009-12-18 11:25:59 +0000873 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000874 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000875 // [...] When looking for a prior declaration of a class or a function
876 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000877 // function is neither a qualified name nor a template-id, scopes outside
878 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000879 if (!SS.isSet()) {
880 DeclContext *OutermostContext = CurContext;
881 while (!OutermostContext->isFileContext())
882 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000883
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000884 if (PrevDecl &&
885 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
886 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
887 SemanticContext = PrevDecl->getDeclContext();
888 } else {
889 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000890 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000891 // declaration.
892 PrevDecl = PrevClassTemplate = 0;
893 SemanticContext = OutermostContext;
894 }
John McCalle129d442009-12-17 23:21:11 +0000895 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000896
John McCalle129d442009-12-17 23:21:11 +0000897 if (CurContext->isDependentContext()) {
898 // If this is a dependent context, we don't want to link the friend
899 // class template to the template in scope, because that would perform
900 // checking of the template parameter lists that can't be performed
901 // until the outer context is instantiated.
902 PrevDecl = PrevClassTemplate = 0;
903 }
904 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
905 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000906
Douglas Gregorddc29e12009-02-06 22:42:48 +0000907 if (PrevClassTemplate) {
908 // Ensure that the template parameter lists are compatible.
909 if (!TemplateParameterListsAreEqual(TemplateParams,
910 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000911 /*Complain=*/true,
912 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000913 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914
915 // C++ [temp.class]p4:
916 // In a redeclaration, partial specialization, explicit
917 // specialization or explicit instantiation of a class template,
918 // the class-key shall agree in kind with the original class
919 // template declaration (7.1.5.3).
920 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000921 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000922 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000923 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000924 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000925 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000926 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000927 }
928
Douglas Gregorddc29e12009-02-06 22:42:48 +0000929 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000930 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000931 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000932 Diag(NameLoc, diag::err_redefinition) << Name;
933 Diag(Def->getLocation(), diag::note_previous_definition);
934 // FIXME: Would it make sense to try to "forget" the previous
935 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000936 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000937 }
938 }
939 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
940 // Maybe we will complain about the shadowed template parameter.
941 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
942 // Just pretend that we didn't see the previous declaration.
943 PrevDecl = 0;
944 } else if (PrevDecl) {
945 // C++ [temp]p5:
946 // A class template shall not have the same name as any other
947 // template, class, function, object, enumeration, enumerator,
948 // namespace, or type in the same scope (3.3), except as specified
949 // in (14.5.4).
950 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
951 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000952 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000953 }
954
Douglas Gregord684b002009-02-10 19:49:53 +0000955 // Check the template parameter list of this declaration, possibly
956 // merging in the template parameter list from the previous class
957 // template declaration.
958 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000959 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000960 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000961 SemanticContext->isRecord() &&
962 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000963 ? TPC_ClassTemplateMember
964 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000965 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Douglas Gregor57265e32010-04-12 16:00:01 +0000967 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000968 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000969 // template out-of-line.
970 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
971 !(TUK == TUK_Friend && CurContext->isDependentContext()))
972 Diag(NameLoc, diag::err_member_def_does_not_match)
973 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000974 }
975
Mike Stump1eb44332009-09-09 15:08:12 +0000976 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000977 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000978 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000979 PrevClassTemplate->getTemplatedDecl() : 0,
980 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000981 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000982 if (NumOuterTemplateParamLists > 0)
983 NewClass->setTemplateParameterListsInfo(Context,
984 NumOuterTemplateParamLists,
985 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000986
987 ClassTemplateDecl *NewTemplate
988 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
989 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000990 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000991 NewClass->setDescribedClassTemplate(NewTemplate);
992
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000993 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000994 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000995 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000996 assert(T->isDependentType() && "Class template type is not dependent?");
997 (void)T;
998
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000999 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001000 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001001 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001002 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1003 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001004
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001005 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001006 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001007 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Douglas Gregorddc29e12009-02-06 22:42:48 +00001009 // Set the lexical context of these templates
1010 NewClass->setLexicalDeclContext(CurContext);
1011 NewTemplate->setLexicalDeclContext(CurContext);
1012
John McCall0f434ec2009-07-31 02:45:11 +00001013 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001014 NewClass->startDefinition();
1015
1016 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001017 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001018
John McCall05b23ea2009-09-14 21:59:20 +00001019 if (TUK != TUK_Friend)
1020 PushOnScopeChains(NewTemplate, S);
1021 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001022 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001023 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001024 NewClass->setAccess(PrevClassTemplate->getAccess());
1025 }
John McCall05b23ea2009-09-14 21:59:20 +00001026
Douglas Gregord85bea22009-09-26 06:47:28 +00001027 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1028 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001029
John McCall05b23ea2009-09-14 21:59:20 +00001030 // Friend templates are visible in fairly strange ways.
1031 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001032 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001033 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1034 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1035 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001036 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001037 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001038
Douglas Gregord85bea22009-09-26 06:47:28 +00001039 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1040 NewClass->getLocation(),
1041 NewTemplate,
1042 /*FIXME:*/NewClass->getLocation());
1043 Friend->setAccess(AS_public);
1044 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001045 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001046
Douglas Gregord684b002009-02-10 19:49:53 +00001047 if (Invalid) {
1048 NewTemplate->setInvalidDecl();
1049 NewClass->setInvalidDecl();
1050 }
John McCalld226f652010-08-21 09:40:31 +00001051 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001052}
1053
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001054/// \brief Diagnose the presence of a default template argument on a
1055/// template parameter, which is ill-formed in certain contexts.
1056///
1057/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001058static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001059 Sema::TemplateParamListContext TPC,
1060 SourceLocation ParamLoc,
1061 SourceRange DefArgRange) {
1062 switch (TPC) {
1063 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001064 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001065 return false;
1066
1067 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001068 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001069 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001070 // A default template-argument shall not be specified in a
1071 // function template declaration or a function template
1072 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001073 // If a friend function template declaration specifies a default
1074 // template-argument, that declaration shall be a definition and shall be
1075 // the only declaration of the function template in the translation unit.
1076 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001077 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001078 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001079 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001080 << DefArgRange;
1081 return false;
1082
1083 case Sema::TPC_ClassTemplateMember:
1084 // C++0x [temp.param]p9:
1085 // A default template-argument shall not be specified in the
1086 // template-parameter-lists of the definition of a member of a
1087 // class template that appears outside of the member's class.
1088 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1089 << DefArgRange;
1090 return true;
1091
1092 case Sema::TPC_FriendFunctionTemplate:
1093 // C++ [temp.param]p9:
1094 // A default template-argument shall not be specified in a
1095 // friend template declaration.
1096 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1097 << DefArgRange;
1098 return true;
1099
1100 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1101 // for friend function templates if there is only a single
1102 // declaration (and it is a definition). Strange!
1103 }
1104
1105 return false;
1106}
1107
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001108/// \brief Check for unexpanded parameter packs within the template parameters
1109/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001110static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1111 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001112 TemplateParameterList *Params = TTP->getTemplateParameters();
1113 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1114 NamedDecl *P = Params->getParam(I);
1115 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001116 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001117 NTTP->getTypeSourceInfo(),
1118 Sema::UPPC_NonTypeTemplateParameterType))
1119 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001120
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001121 continue;
1122 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001123
1124 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001125 = dyn_cast<TemplateTemplateParmDecl>(P))
1126 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1127 return true;
1128 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001129
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001130 return false;
1131}
1132
Douglas Gregord684b002009-02-10 19:49:53 +00001133/// \brief Checks the validity of a template parameter list, possibly
1134/// considering the template parameter list from a previous
1135/// declaration.
1136///
1137/// If an "old" template parameter list is provided, it must be
1138/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1139/// template parameter list.
1140///
1141/// \param NewParams Template parameter list for a new template
1142/// declaration. This template parameter list will be updated with any
1143/// default arguments that are carried through from the previous
1144/// template parameter list.
1145///
1146/// \param OldParams If provided, template parameter list from a
1147/// previous declaration of the same template. Default template
1148/// arguments will be merged from the old template parameter list to
1149/// the new template parameter list.
1150///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001151/// \param TPC Describes the context in which we are checking the given
1152/// template parameter list.
1153///
Douglas Gregord684b002009-02-10 19:49:53 +00001154/// \returns true if an error occurred, false otherwise.
1155bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001156 TemplateParameterList *OldParams,
1157 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001158 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregord684b002009-02-10 19:49:53 +00001160 // C++ [temp.param]p10:
1161 // The set of default template-arguments available for use with a
1162 // template declaration or definition is obtained by merging the
1163 // default arguments from the definition (if in scope) and all
1164 // declarations in scope in the same way default function
1165 // arguments are (8.3.6).
1166 bool SawDefaultArgument = false;
1167 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001168
Anders Carlsson49d25572009-06-12 23:20:15 +00001169 bool SawParameterPack = false;
1170 SourceLocation ParameterPackLoc;
1171
Mike Stump1a35fde2009-02-11 23:03:27 +00001172 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001173 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001174 if (OldParams)
1175 OldParam = OldParams->begin();
1176
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001177 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001178 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1179 NewParamEnd = NewParams->end();
1180 NewParam != NewParamEnd; ++NewParam) {
1181 // Variables used to diagnose redundant default arguments
1182 bool RedundantDefaultArg = false;
1183 SourceLocation OldDefaultLoc;
1184 SourceLocation NewDefaultLoc;
1185
1186 // Variables used to diagnose missing default arguments
1187 bool MissingDefaultArg = false;
1188
Anders Carlsson49d25572009-06-12 23:20:15 +00001189 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001190 // If a template parameter of a primary class template or alias template
1191 // is a template parameter pack, it shall be the last template parameter.
1192 if (SawParameterPack &&
1193 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001194 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001195 diag::err_template_param_pack_must_be_last_template_parameter);
1196 Invalid = true;
1197 }
1198
Douglas Gregord684b002009-02-10 19:49:53 +00001199 if (TemplateTypeParmDecl *NewTypeParm
1200 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001201 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001202 if (NewTypeParm->hasDefaultArgument() &&
1203 DiagnoseDefaultTemplateArgument(*this, TPC,
1204 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001205 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001206 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001207 NewTypeParm->removeDefaultArgument();
1208
1209 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001210 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001211 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Anders Carlsson49d25572009-06-12 23:20:15 +00001213 if (NewTypeParm->isParameterPack()) {
1214 assert(!NewTypeParm->hasDefaultArgument() &&
1215 "Parameter packs can't have a default argument!");
1216 SawParameterPack = true;
1217 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001218 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001219 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001220 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1221 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1222 SawDefaultArgument = true;
1223 RedundantDefaultArg = true;
1224 PreviousDefaultArgLoc = NewDefaultLoc;
1225 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1226 // Merge the default argument from the old declaration to the
1227 // new declaration.
1228 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001229 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001230 true);
1231 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1232 } else if (NewTypeParm->hasDefaultArgument()) {
1233 SawDefaultArgument = true;
1234 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1235 } else if (SawDefaultArgument)
1236 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001237 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001238 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001239 // Check for unexpanded parameter packs.
1240 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001241 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001242 UPPC_NonTypeTemplateParameterType)) {
1243 Invalid = true;
1244 continue;
1245 }
1246
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001247 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001248 if (NewNonTypeParm->hasDefaultArgument() &&
1249 DiagnoseDefaultTemplateArgument(*this, TPC,
1250 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001251 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001252 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001253 }
1254
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001255 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001256 NonTypeTemplateParmDecl *OldNonTypeParm
1257 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001258 if (NewNonTypeParm->isParameterPack()) {
1259 assert(!NewNonTypeParm->hasDefaultArgument() &&
1260 "Parameter packs can't have a default argument!");
1261 SawParameterPack = true;
1262 ParameterPackLoc = NewNonTypeParm->getLocation();
1263 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001264 NewNonTypeParm->hasDefaultArgument()) {
1265 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1266 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1267 SawDefaultArgument = true;
1268 RedundantDefaultArg = true;
1269 PreviousDefaultArgLoc = NewDefaultLoc;
1270 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1271 // Merge the default argument from the old declaration to the
1272 // new declaration.
1273 SawDefaultArgument = true;
1274 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001275 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001276 // parameter.
1277 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001278 OldNonTypeParm->getDefaultArgument(),
1279 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001280 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1281 } else if (NewNonTypeParm->hasDefaultArgument()) {
1282 SawDefaultArgument = true;
1283 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1284 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001285 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001286 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001287 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001288 TemplateTemplateParmDecl *NewTemplateParm
1289 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001290
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001291 // Check for unexpanded parameter packs, recursively.
1292 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1293 Invalid = true;
1294 continue;
1295 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001296
1297 if (NewTemplateParm->hasDefaultArgument() &&
1298 DiagnoseDefaultTemplateArgument(*this, TPC,
1299 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001300 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001301 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001302
1303 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001304 TemplateTemplateParmDecl *OldTemplateParm
1305 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001306 if (NewTemplateParm->isParameterPack()) {
1307 assert(!NewTemplateParm->hasDefaultArgument() &&
1308 "Parameter packs can't have a default argument!");
1309 SawParameterPack = true;
1310 ParameterPackLoc = NewTemplateParm->getLocation();
1311 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001312 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001313 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1314 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001315 SawDefaultArgument = true;
1316 RedundantDefaultArg = true;
1317 PreviousDefaultArgLoc = NewDefaultLoc;
1318 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1319 // Merge the default argument from the old declaration to the
1320 // new declaration.
1321 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001322 // FIXME: We need to create a new kind of "default argument" expression
1323 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001324 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001325 OldTemplateParm->getDefaultArgument(),
1326 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001327 PreviousDefaultArgLoc
1328 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001329 } else if (NewTemplateParm->hasDefaultArgument()) {
1330 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001331 PreviousDefaultArgLoc
1332 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001333 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001334 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001335 }
1336
1337 if (RedundantDefaultArg) {
1338 // C++ [temp.param]p12:
1339 // A template-parameter shall not be given default arguments
1340 // by two different declarations in the same scope.
1341 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1342 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1343 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001344 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001345 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001346 // If a template-parameter of a class template has a default
1347 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001348 // have a default template-argument supplied or be a template parameter
1349 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001350 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001351 diag::err_template_param_default_arg_missing);
1352 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1353 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001354 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001355 }
1356
1357 // If we have an old template parameter list that we're merging
1358 // in, move on to the next parameter.
1359 if (OldParams)
1360 ++OldParam;
1361 }
1362
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001363 // We were missing some default arguments at the end of the list, so remove
1364 // all of the default arguments.
1365 if (RemoveDefaultArguments) {
1366 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1367 NewParamEnd = NewParams->end();
1368 NewParam != NewParamEnd; ++NewParam) {
1369 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1370 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001371 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001372 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1373 NTTP->removeDefaultArgument();
1374 else
1375 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1376 }
1377 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001378
Douglas Gregord684b002009-02-10 19:49:53 +00001379 return Invalid;
1380}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001381
John McCall4e2cbb22010-10-20 05:44:58 +00001382namespace {
1383
1384/// A class which looks for a use of a certain level of template
1385/// parameter.
1386struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1387 typedef RecursiveASTVisitor<DependencyChecker> super;
1388
1389 unsigned Depth;
1390 bool Match;
1391
1392 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1393 NamedDecl *ND = Params->getParam(0);
1394 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1395 Depth = PD->getDepth();
1396 } else if (NonTypeTemplateParmDecl *PD =
1397 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1398 Depth = PD->getDepth();
1399 } else {
1400 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1401 }
1402 }
1403
1404 bool Matches(unsigned ParmDepth) {
1405 if (ParmDepth >= Depth) {
1406 Match = true;
1407 return true;
1408 }
1409 return false;
1410 }
1411
1412 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1413 return !Matches(T->getDepth());
1414 }
1415
1416 bool TraverseTemplateName(TemplateName N) {
1417 if (TemplateTemplateParmDecl *PD =
1418 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1419 if (Matches(PD->getDepth())) return false;
1420 return super::TraverseTemplateName(N);
1421 }
1422
1423 bool VisitDeclRefExpr(DeclRefExpr *E) {
1424 if (NonTypeTemplateParmDecl *PD =
1425 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1426 if (PD->getDepth() == Depth) {
1427 Match = true;
1428 return false;
1429 }
1430 }
1431 return super::VisitDeclRefExpr(E);
1432 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001433
1434 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1435 return TraverseType(T->getInjectedSpecializationType());
1436 }
John McCall4e2cbb22010-10-20 05:44:58 +00001437};
1438}
1439
Douglas Gregorc8406492011-05-10 18:27:06 +00001440/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001441/// list.
1442static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001443DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001444 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001445 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001446 return Checker.Match;
1447}
1448
Douglas Gregorc8406492011-05-10 18:27:06 +00001449// Find the source range corresponding to the named type in the given
1450// nested-name-specifier, if any.
1451static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1452 QualType T,
1453 const CXXScopeSpec &SS) {
1454 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1455 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1456 if (const Type *CurType = NNS->getAsType()) {
1457 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1458 return NNSLoc.getTypeLoc().getSourceRange();
1459 } else
1460 break;
1461
1462 NNSLoc = NNSLoc.getPrefix();
1463 }
1464
1465 return SourceRange();
1466}
1467
Mike Stump1eb44332009-09-09 15:08:12 +00001468/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001469/// specifier, returning the template parameter list that applies to the
1470/// name.
1471///
1472/// \param DeclStartLoc the start of the declaration that has a scope
1473/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001474///
Douglas Gregorc8406492011-05-10 18:27:06 +00001475/// \param DeclLoc The location of the declaration itself.
1476///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001477/// \param SS the scope specifier that will be matched to the given template
1478/// parameter lists. This scope specifier precedes a qualified name that is
1479/// being declared.
1480///
1481/// \param ParamLists the template parameter lists, from the outermost to the
1482/// innermost template parameter lists.
1483///
1484/// \param NumParamLists the number of template parameter lists in ParamLists.
1485///
John McCall77e8b112010-04-13 20:37:33 +00001486/// \param IsFriend Whether to apply the slightly different rules for
1487/// matching template parameters to scope specifiers in friend
1488/// declarations.
1489///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001490/// \param IsExplicitSpecialization will be set true if the entity being
1491/// declared is an explicit specialization, false otherwise.
1492///
Mike Stump1eb44332009-09-09 15:08:12 +00001493/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001494/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001495/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001496/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001497/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001498/// itself a template).
1499TemplateParameterList *
1500Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001501 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001502 const CXXScopeSpec &SS,
1503 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001504 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001505 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001506 bool &IsExplicitSpecialization,
1507 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001508 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001509 Invalid = false;
1510
1511 // The sequence of nested types to which we will match up the template
1512 // parameter lists. We first build this list by starting with the type named
1513 // by the nested-name-specifier and walking out until we run out of types.
1514 llvm::SmallVector<QualType, 4> NestedTypes;
1515 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001516 if (SS.getScopeRep()) {
1517 if (CXXRecordDecl *Record
1518 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1519 T = Context.getTypeDeclType(Record);
1520 else
1521 T = QualType(SS.getScopeRep()->getAsType(), 0);
1522 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001523
1524 // If we found an explicit specialization that prevents us from needing
1525 // 'template<>' headers, this will be set to the location of that
1526 // explicit specialization.
1527 SourceLocation ExplicitSpecLoc;
1528
1529 while (!T.isNull()) {
1530 NestedTypes.push_back(T);
1531
1532 // Retrieve the parent of a record type.
1533 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1534 // If this type is an explicit specialization, we're done.
1535 if (ClassTemplateSpecializationDecl *Spec
1536 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1537 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1538 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1539 ExplicitSpecLoc = Spec->getLocation();
1540 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001541 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001542 } else if (Record->getTemplateSpecializationKind()
1543 == TSK_ExplicitSpecialization) {
1544 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001545 break;
1546 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001547
1548 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1549 T = Context.getTypeDeclType(Parent);
1550 else
1551 T = QualType();
1552 continue;
1553 }
1554
1555 if (const TemplateSpecializationType *TST
1556 = T->getAs<TemplateSpecializationType>()) {
1557 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1558 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1559 T = Context.getTypeDeclType(Parent);
1560 else
1561 T = QualType();
1562 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001563 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001564 }
1565
1566 // Look one step prior in a dependent template specialization type.
1567 if (const DependentTemplateSpecializationType *DependentTST
1568 = T->getAs<DependentTemplateSpecializationType>()) {
1569 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1570 T = QualType(NNS->getAsType(), 0);
1571 else
1572 T = QualType();
1573 continue;
1574 }
1575
1576 // Look one step prior in a dependent name type.
1577 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1578 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1579 T = QualType(NNS->getAsType(), 0);
1580 else
1581 T = QualType();
1582 continue;
1583 }
1584
1585 // Retrieve the parent of an enumeration type.
1586 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1587 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1588 // check here.
1589 EnumDecl *Enum = EnumT->getDecl();
1590
1591 // Get to the parent type.
1592 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1593 T = Context.getTypeDeclType(Parent);
1594 else
1595 T = QualType();
1596 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Douglas Gregorc8406492011-05-10 18:27:06 +00001599 T = QualType();
1600 }
1601 // Reverse the nested types list, since we want to traverse from the outermost
1602 // to the innermost while checking template-parameter-lists.
1603 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001604
Douglas Gregorc8406492011-05-10 18:27:06 +00001605 // C++0x [temp.expl.spec]p17:
1606 // A member or a member template may be nested within many
1607 // enclosing class templates. In an explicit specialization for
1608 // such a member, the member declaration shall be preceded by a
1609 // template<> for each enclosing class template that is
1610 // explicitly specialized.
1611 unsigned ParamIdx = 0;
1612 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1613 ++TypeIdx) {
1614 T = NestedTypes[TypeIdx];
1615
1616 // Whether we expect a 'template<>' header.
1617 bool NeedEmptyTemplateHeader = false;
1618
1619 // Whether we expect a template header with parameters.
1620 bool NeedNonemptyTemplateHeader = false;
1621
1622 // For a dependent type, the set of template parameters that we
1623 // expect to see.
1624 TemplateParameterList *ExpectedTemplateParams = 0;
1625
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001626 // C++0x [temp.expl.spec]p15:
1627 // A member or a member template may be nested within many enclosing
1628 // class templates. In an explicit specialization for such a member, the
1629 // member declaration shall be preceded by a template<> for each
1630 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001631 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1632 if (ClassTemplatePartialSpecializationDecl *Partial
1633 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1634 ExpectedTemplateParams = Partial->getTemplateParameters();
1635 NeedNonemptyTemplateHeader = true;
1636 } else if (Record->isDependentType()) {
1637 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001638 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001639 ->getTemplateParameters();
1640 NeedNonemptyTemplateHeader = true;
1641 }
1642 } else if (ClassTemplateSpecializationDecl *Spec
1643 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1644 // C++0x [temp.expl.spec]p4:
1645 // Members of an explicitly specialized class template are defined
1646 // in the same manner as members of normal classes, and not using
1647 // the template<> syntax.
1648 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1649 NeedEmptyTemplateHeader = true;
1650 else
1651 break;
1652 } else if (Record->getTemplateSpecializationKind()) {
1653 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001654 != TSK_ExplicitSpecialization &&
1655 TypeIdx == NumTypes - 1)
1656 IsExplicitSpecialization = true;
1657
1658 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001659 }
1660 } else if (const TemplateSpecializationType *TST
1661 = T->getAs<TemplateSpecializationType>()) {
1662 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1663 ExpectedTemplateParams = Template->getTemplateParameters();
1664 NeedNonemptyTemplateHeader = true;
1665 }
1666 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1667 // FIXME: We actually could/should check the template arguments here
1668 // against the corresponding template parameter list.
1669 NeedNonemptyTemplateHeader = false;
1670 }
1671
1672 if (NeedEmptyTemplateHeader) {
1673 // If we're on the last of the types, and we need a 'template<>' header
1674 // here, then it's an explicit specialization.
1675 if (TypeIdx == NumTypes - 1)
1676 IsExplicitSpecialization = true;
1677
1678 if (ParamIdx < NumParamLists) {
1679 if (ParamLists[ParamIdx]->size() > 0) {
1680 // The header has template parameters when it shouldn't. Complain.
1681 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1682 diag::err_template_param_list_matches_nontemplate)
1683 << T
1684 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1685 ParamLists[ParamIdx]->getRAngleLoc())
1686 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1687 Invalid = true;
1688 return 0;
1689 }
1690
1691 // Consume this template header.
1692 ++ParamIdx;
1693 continue;
1694 }
1695
1696 if (!IsFriend) {
1697 // We don't have a template header, but we should.
1698 SourceLocation ExpectedTemplateLoc;
1699 if (NumParamLists > 0)
1700 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1701 else
1702 ExpectedTemplateLoc = DeclStartLoc;
1703
1704 Diag(DeclLoc, diag::err_template_spec_needs_header)
1705 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1706 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1707 }
1708
1709 continue;
1710 }
1711
1712 if (NeedNonemptyTemplateHeader) {
1713 // In friend declarations we can have template-ids which don't
1714 // depend on the corresponding template parameter lists. But
1715 // assume that empty parameter lists are supposed to match this
1716 // template-id.
1717 if (IsFriend && T->isDependentType()) {
1718 if (ParamIdx < NumParamLists &&
1719 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1720 ExpectedTemplateParams = 0;
1721 else
1722 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001723 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001724
Douglas Gregorc8406492011-05-10 18:27:06 +00001725 if (ParamIdx < NumParamLists) {
1726 // Check the template parameter list, if we can.
1727 if (ExpectedTemplateParams &&
1728 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1729 ExpectedTemplateParams,
1730 true, TPL_TemplateMatch))
1731 Invalid = true;
1732
1733 if (!Invalid &&
1734 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1735 TPC_ClassTemplateMember))
1736 Invalid = true;
1737
1738 ++ParamIdx;
1739 continue;
1740 }
1741
1742 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1743 << T
1744 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1745 Invalid = true;
1746 continue;
1747 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001748 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001749
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001750 // If there were at least as many template-ids as there were template
1751 // parameter lists, then there are no template parameter lists remaining for
1752 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001753 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001754 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001756 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001757 if (ParamIdx < NumParamLists - 1) {
1758 bool HasAnyExplicitSpecHeader = false;
1759 bool AllExplicitSpecHeaders = true;
1760 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1761 if (ParamLists[I]->size() == 0)
1762 HasAnyExplicitSpecHeader = true;
1763 else
1764 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001765 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001766
1767 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1768 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1769 : diag::err_template_spec_extra_headers)
1770 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1771 ParamLists[NumParamLists - 2]->getRAngleLoc());
1772
1773 // If there was a specialization somewhere, such that 'template<>' is
1774 // not required, and there were any 'template<>' headers, note where the
1775 // specialization occurred.
1776 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1777 Diag(ExplicitSpecLoc,
1778 diag::note_explicit_template_spec_does_not_need_header)
1779 << NestedTypes.back();
1780
1781 // We have a template parameter list with no corresponding scope, which
1782 // means that the resulting template declaration can't be instantiated
1783 // properly (we'll end up with dependent nodes when we shouldn't).
1784 if (!AllExplicitSpecHeaders)
1785 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001786 }
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001788 // Return the last template parameter list, which corresponds to the
1789 // entity being declared.
1790 return ParamLists[NumParamLists - 1];
1791}
1792
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001793void Sema::NoteAllFoundTemplates(TemplateName Name) {
1794 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1795 Diag(Template->getLocation(), diag::note_template_declared_here)
1796 << (isa<FunctionTemplateDecl>(Template)? 0
1797 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001798 : isa<TypeAliasTemplateDecl>(Template)? 2
1799 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001800 << Template->getDeclName();
1801 return;
1802 }
1803
1804 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1805 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1806 IEnd = OST->end();
1807 I != IEnd; ++I)
1808 Diag((*I)->getLocation(), diag::note_template_declared_here)
1809 << 0 << (*I)->getDeclName();
1810
1811 return;
1812 }
1813}
1814
1815
Douglas Gregor7532dc62009-03-30 22:58:21 +00001816QualType Sema::CheckTemplateIdType(TemplateName Name,
1817 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001818 TemplateArgumentListInfo &TemplateArgs) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001819 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1820 if (DTN && DTN->isIdentifier())
1821 // When building a template-id where the template-name is dependent,
1822 // assume the template is a type template. Either our assumption is
1823 // correct, or the code is ill-formed and will be diagnosed when the
1824 // dependent name is substituted.
1825 return Context.getDependentTemplateSpecializationType(ETK_None,
1826 DTN->getQualifier(),
1827 DTN->getIdentifier(),
1828 TemplateArgs);
1829
Douglas Gregor7532dc62009-03-30 22:58:21 +00001830 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001831 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1832 // We might have a substituted template template parameter pack. If so,
1833 // build a template specialization type for it.
1834 if (Name.getAsSubstTemplateTemplateParmPack())
1835 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001836
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001837 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1838 << Name;
1839 NoteAllFoundTemplates(Name);
1840 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001841 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001842
Douglas Gregor40808ce2009-03-09 23:48:35 +00001843 // Check that the template argument list is well-formed for this
1844 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001845 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001846 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001847 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001848 return QualType();
1849
Douglas Gregor910f8002010-11-07 23:05:16 +00001850 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001851 "Converted template argument list is too short!");
1852
1853 QualType CanonType;
1854
Richard Smith3e4c6c42011-05-05 21:57:07 +00001855 if (TypeAliasTemplateDecl *AliasTemplate
1856 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1857 // Find the canonical type for this type alias template specialization.
1858 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1859 if (Pattern->isInvalidDecl())
1860 return QualType();
1861
1862 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1863 Converted.data(), Converted.size());
1864
1865 // Only substitute for the innermost template argument list.
1866 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001867 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001868 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1869 for (unsigned I = 0; I < Depth; ++I)
1870 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001871
1872 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1873 CanonType = SubstType(Pattern->getUnderlyingType(),
1874 TemplateArgLists, AliasTemplate->getLocation(),
1875 AliasTemplate->getDeclName());
1876 if (CanonType.isNull())
1877 return QualType();
1878 } else if (Name.isDependent() ||
1879 TemplateSpecializationType::anyDependentTemplateArguments(
1880 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001881 // This class template specialization is a dependent
1882 // type. Therefore, its canonical type is another class template
1883 // specialization type that contains all of the converted
1884 // arguments in canonical form. This ensures that, e.g., A<T> and
1885 // A<T, T> have identical types when A is declared as:
1886 //
1887 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001888 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001889 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001890 Converted.data(),
1891 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Douglas Gregor1275ae02009-07-28 23:00:59 +00001893 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001894 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001895 // In the future, we need to teach getTemplateSpecializationType to only
1896 // build the canonical type and return that to us.
1897 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001898
1899 // This might work out to be a current instantiation, in which
1900 // case the canonical type needs to be the InjectedClassNameType.
1901 //
1902 // TODO: in theory this could be a simple hashtable lookup; most
1903 // changes to CurContext don't change the set of current
1904 // instantiations.
1905 if (isa<ClassTemplateDecl>(Template)) {
1906 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1907 // If we get out to a namespace, we're done.
1908 if (Ctx->isFileContext()) break;
1909
1910 // If this isn't a record, keep looking.
1911 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1912 if (!Record) continue;
1913
1914 // Look for one of the two cases with InjectedClassNameTypes
1915 // and check whether it's the same template.
1916 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1917 !Record->getDescribedClassTemplate())
1918 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001919
John McCall31f17ec2010-04-27 00:57:59 +00001920 // Fetch the injected class name type and check whether its
1921 // injected type is equal to the type we just built.
1922 QualType ICNT = Context.getTypeDeclType(Record);
1923 QualType Injected = cast<InjectedClassNameType>(ICNT)
1924 ->getInjectedSpecializationType();
1925
1926 if (CanonType != Injected->getCanonicalTypeInternal())
1927 continue;
1928
1929 // If so, the canonical type of this TST is the injected
1930 // class name type of the record we just found.
1931 assert(ICNT.isCanonical());
1932 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001933 break;
1934 }
1935 }
Mike Stump1eb44332009-09-09 15:08:12 +00001936 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001937 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001938 // Find the class template specialization declaration that
1939 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001940 void *InsertPos = 0;
1941 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001942 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001943 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001944 if (!Decl) {
1945 // This is the first time we have referenced this class template
1946 // specialization. Create the canonical declaration and add it to
1947 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001948 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001949 ClassTemplate->getTemplatedDecl()->getTagKind(),
1950 ClassTemplate->getDeclContext(),
1951 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001952 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001953 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001954 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001955 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001956 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001957 Decl->setLexicalDeclContext(CurContext);
1958 }
1959
1960 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001961 assert(isa<RecordType>(CanonType) &&
1962 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001963 }
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Douglas Gregor40808ce2009-03-09 23:48:35 +00001965 // Build the fully-sugared type for this class template
1966 // specialization, which refers back to the class template
1967 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001968 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001969}
1970
John McCallf312b1e2010-08-26 23:41:50 +00001971TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001972Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1973 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001974 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001975 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001976 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001977 if (SS.isInvalid())
1978 return true;
1979
Douglas Gregor7532dc62009-03-30 22:58:21 +00001980 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001981
Douglas Gregor40808ce2009-03-09 23:48:35 +00001982 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001983 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001984 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001985
Douglas Gregora88f09f2011-02-28 17:23:35 +00001986 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1987 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1988 DTN->getQualifier(),
1989 DTN->getIdentifier(),
1990 TemplateArgs);
1991
1992 // Build type-source information.
1993 TypeLocBuilder TLB;
1994 DependentTemplateSpecializationTypeLoc SpecTL
1995 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001996 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001997 SpecTL.setNameLoc(TemplateLoc);
1998 SpecTL.setLAngleLoc(LAngleLoc);
1999 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002000 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002001 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2002 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2003 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2004 }
2005
John McCalld5532b62009-11-23 01:53:49 +00002006 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002007 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002008
2009 if (Result.isNull())
2010 return true;
2011
Douglas Gregor059101f2011-03-02 00:47:37 +00002012 // Build type-source information.
2013 TypeLocBuilder TLB;
2014 TemplateSpecializationTypeLoc SpecTL
2015 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2016 SpecTL.setTemplateNameLoc(TemplateLoc);
2017 SpecTL.setLAngleLoc(LAngleLoc);
2018 SpecTL.setRAngleLoc(RAngleLoc);
2019 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2020 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002021
Douglas Gregor059101f2011-03-02 00:47:37 +00002022 if (SS.isNotEmpty()) {
2023 // Create an elaborated-type-specifier containing the nested-name-specifier.
2024 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2025 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2026 ElabTL.setKeywordLoc(SourceLocation());
2027 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2028 }
2029
2030 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002031}
John McCallf1bbbb42009-09-04 01:14:41 +00002032
Douglas Gregor059101f2011-03-02 00:47:37 +00002033TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002034 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002035 SourceLocation TagLoc,
2036 CXXScopeSpec &SS,
2037 TemplateTy TemplateD,
2038 SourceLocation TemplateLoc,
2039 SourceLocation LAngleLoc,
2040 ASTTemplateArgsPtr TemplateArgsIn,
2041 SourceLocation RAngleLoc) {
2042 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2043
2044 // Translate the parser's template argument list in our AST format.
2045 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2046 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2047
2048 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002049 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002050 ElaboratedTypeKeyword Keyword
2051 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Douglas Gregor059101f2011-03-02 00:47:37 +00002053 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2054 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2055 DTN->getQualifier(),
2056 DTN->getIdentifier(),
2057 TemplateArgs);
2058
2059 // Build type-source information.
2060 TypeLocBuilder TLB;
2061 DependentTemplateSpecializationTypeLoc SpecTL
2062 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2063 SpecTL.setKeywordLoc(TagLoc);
2064 SpecTL.setNameLoc(TemplateLoc);
2065 SpecTL.setLAngleLoc(LAngleLoc);
2066 SpecTL.setRAngleLoc(RAngleLoc);
2067 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2068 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2069 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2070 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2071 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002072
2073 if (TypeAliasTemplateDecl *TAT =
2074 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2075 // C++0x [dcl.type.elab]p2:
2076 // If the identifier resolves to a typedef-name or the simple-template-id
2077 // resolves to an alias template specialization, the
2078 // elaborated-type-specifier is ill-formed.
2079 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2080 Diag(TAT->getLocation(), diag::note_declared_at);
2081 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002082
2083 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2084 if (Result.isNull())
2085 return TypeResult();
2086
2087 // Check the tag kind
2088 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002089 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002090
John McCall6b2becf2009-09-08 17:47:29 +00002091 IdentifierInfo *Id = D->getIdentifier();
2092 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002093
John McCall6b2becf2009-09-08 17:47:29 +00002094 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
2095 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002096 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002097 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002098 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002099 }
2100 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002101
2102 // Provide source-location information for the template specialization.
2103 TypeLocBuilder TLB;
2104 TemplateSpecializationTypeLoc SpecTL
2105 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2106 SpecTL.setTemplateNameLoc(TemplateLoc);
2107 SpecTL.setLAngleLoc(LAngleLoc);
2108 SpecTL.setRAngleLoc(RAngleLoc);
2109 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2110 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002111
Douglas Gregor059101f2011-03-02 00:47:37 +00002112 // Construct an elaborated type containing the nested-name-specifier (if any)
2113 // and keyword.
2114 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2115 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2116 ElabTL.setKeywordLoc(TagLoc);
2117 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2118 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002119}
2120
John McCall60d7b3a2010-08-24 06:29:42 +00002121ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002122 LookupResult &R,
2123 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002124 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002125 // FIXME: Can we do any checking at this point? I guess we could check the
2126 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002127 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002128 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002129 // foo<int> could identify a single function unambiguously
2130 // This approach does NOT work, since f<int>(1);
2131 // gets resolved prior to resorting to overload resolution
2132 // i.e., template<class T> void f(double);
2133 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002134
2135 // These should be filtered out by our callers.
2136 assert(!R.empty() && "empty lookup results when building templateid");
2137 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2138
John McCallc373d482010-01-27 01:50:18 +00002139 // We don't want lookup warnings at this point.
2140 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002141
John McCallf7a1a742009-11-24 19:00:30 +00002142 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002143 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002144 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002145 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002146 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002147 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002148
2149 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002150}
2151
John McCallf7a1a742009-11-24 19:00:30 +00002152// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002153ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002154Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002155 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002156 const TemplateArgumentListInfo &TemplateArgs) {
2157 DeclContext *DC;
2158 if (!(DC = computeDeclContext(SS, false)) ||
2159 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002160 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002161 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002162
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002163 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002164 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002165 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2166 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002167
John McCallf7a1a742009-11-24 19:00:30 +00002168 if (R.isAmbiguous())
2169 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002170
John McCallf7a1a742009-11-24 19:00:30 +00002171 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002172 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2173 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002174 return ExprError();
2175 }
2176
2177 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002178 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2179 << (NestedNameSpecifier*) SS.getScopeRep()
2180 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002181 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2182 return ExprError();
2183 }
2184
2185 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002186}
2187
Douglas Gregorc45c2322009-03-31 00:43:58 +00002188/// \brief Form a dependent template name.
2189///
2190/// This action forms a dependent template name given the template
2191/// name and its (presumably dependent) scope specifier. For
2192/// example, given "MetaFun::template apply", the scope specifier \p
2193/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2194/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002195TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002196 SourceLocation TemplateKWLoc,
2197 CXXScopeSpec &SS,
2198 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002199 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002200 bool EnteringContext,
2201 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002202 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2203 !getLangOptions().CPlusPlus0x)
2204 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002205 << FixItHint::CreateRemoval(TemplateKWLoc);
2206
Douglas Gregor0707bc52010-01-19 16:01:07 +00002207 DeclContext *LookupCtx = 0;
2208 if (SS.isSet())
2209 LookupCtx = computeDeclContext(SS, EnteringContext);
2210 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002211 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002212 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002213 // C++0x [temp.names]p5:
2214 // If a name prefixed by the keyword template is not the name of
2215 // a template, the program is ill-formed. [Note: the keyword
2216 // template may not be applied to non-template members of class
2217 // templates. -end note ] [ Note: as is the case with the
2218 // typename prefix, the template prefix is allowed in cases
2219 // where it is not strictly necessary; i.e., when the
2220 // nested-name-specifier or the expression on the left of the ->
2221 // or . is not dependent on a template-parameter, or the use
2222 // does not appear in the scope of a template. -end note]
2223 //
2224 // Note: C++03 was more strict here, because it banned the use of
2225 // the "template" keyword prior to a template-name that was not a
2226 // dependent name. C++ DR468 relaxed this requirement (the
2227 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002228 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002229 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002230 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2231 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002232 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002233 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2234 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002235 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2236 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002237 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002238 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002239 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002240 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002241 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002242 << Name.getSourceRange()
2243 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002244 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002245 } else {
2246 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002247 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002248 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002249 }
2250
Mike Stump1eb44332009-09-09 15:08:12 +00002251 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002252 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002253
Douglas Gregor014e88d2009-11-03 23:16:33 +00002254 switch (Name.getKind()) {
2255 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002256 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002257 Name.Identifier));
2258 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002259
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002260 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002261 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002262 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002263 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002264
2265 case UnqualifiedId::IK_LiteralOperatorId:
2266 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2267
Douglas Gregor014e88d2009-11-03 23:16:33 +00002268 default:
2269 break;
2270 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002271
2272 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002273 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002274 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002275 << Name.getSourceRange()
2276 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002277 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002278}
2279
Mike Stump1eb44332009-09-09 15:08:12 +00002280bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002281 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002282 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002283 const TemplateArgument &Arg = AL.getArgument();
2284
Anders Carlsson436b1562009-06-13 00:33:33 +00002285 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002286 switch(Arg.getKind()) {
2287 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002288 // C++ [temp.arg.type]p1:
2289 // A template-argument for a template-parameter which is a
2290 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002291 break;
2292 case TemplateArgument::Template: {
2293 // We have a template type parameter but the template argument
2294 // is a template without any arguments.
2295 SourceRange SR = AL.getSourceRange();
2296 TemplateName Name = Arg.getAsTemplate();
2297 Diag(SR.getBegin(), diag::err_template_missing_args)
2298 << Name << SR;
2299 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2300 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002301
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002302 return true;
2303 }
2304 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002305 // We have a template type parameter but the template argument
2306 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002307 SourceRange SR = AL.getSourceRange();
2308 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002309 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002310
Anders Carlsson436b1562009-06-13 00:33:33 +00002311 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002312 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002313 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002314
John McCalla93c9342009-12-07 02:54:59 +00002315 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002316 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Anders Carlsson436b1562009-06-13 00:33:33 +00002318 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002319 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002320 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002321 return false;
2322}
2323
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002324/// \brief Substitute template arguments into the default template argument for
2325/// the given template type parameter.
2326///
2327/// \param SemaRef the semantic analysis object for which we are performing
2328/// the substitution.
2329///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002330/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002331/// for.
2332///
2333/// \param TemplateLoc the location of the template name that started the
2334/// template-id we are checking.
2335///
2336/// \param RAngleLoc the location of the right angle bracket ('>') that
2337/// terminates the template-id.
2338///
2339/// \param Param the template template parameter whose default we are
2340/// substituting into.
2341///
2342/// \param Converted the list of template arguments provided for template
2343/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002344/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002345static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002346SubstDefaultTemplateArgument(Sema &SemaRef,
2347 TemplateDecl *Template,
2348 SourceLocation TemplateLoc,
2349 SourceLocation RAngleLoc,
2350 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002351 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002352 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002353
2354 // If the argument type is dependent, instantiate it now based
2355 // on the previously-computed template arguments.
2356 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002357 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002358 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002359
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002360 MultiLevelTemplateArgumentList AllTemplateArgs
2361 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2362
2363 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002364 Template, Converted.data(),
2365 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002366 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002367
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002368 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2369 Param->getDefaultArgumentLoc(),
2370 Param->getDeclName());
2371 }
2372
2373 return ArgType;
2374}
2375
2376/// \brief Substitute template arguments into the default template argument for
2377/// the given non-type template parameter.
2378///
2379/// \param SemaRef the semantic analysis object for which we are performing
2380/// the substitution.
2381///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002382/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002383/// for.
2384///
2385/// \param TemplateLoc the location of the template name that started the
2386/// template-id we are checking.
2387///
2388/// \param RAngleLoc the location of the right angle bracket ('>') that
2389/// terminates the template-id.
2390///
Douglas Gregor788cd062009-11-11 01:00:40 +00002391/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002392/// substituting into.
2393///
2394/// \param Converted the list of template arguments provided for template
2395/// parameters that precede \p Param in the template parameter list.
2396///
2397/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002398static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002399SubstDefaultTemplateArgument(Sema &SemaRef,
2400 TemplateDecl *Template,
2401 SourceLocation TemplateLoc,
2402 SourceLocation RAngleLoc,
2403 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002404 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002405 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002406 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002407
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002408 MultiLevelTemplateArgumentList AllTemplateArgs
2409 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002410
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002411 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002412 Template, Converted.data(),
2413 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002414 SourceRange(TemplateLoc, RAngleLoc));
2415
2416 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2417}
2418
Douglas Gregor788cd062009-11-11 01:00:40 +00002419/// \brief Substitute template arguments into the default template argument for
2420/// the given template template parameter.
2421///
2422/// \param SemaRef the semantic analysis object for which we are performing
2423/// the substitution.
2424///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002425/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002426/// for.
2427///
2428/// \param TemplateLoc the location of the template name that started the
2429/// template-id we are checking.
2430///
2431/// \param RAngleLoc the location of the right angle bracket ('>') that
2432/// terminates the template-id.
2433///
2434/// \param Param the template template parameter whose default we are
2435/// substituting into.
2436///
2437/// \param Converted the list of template arguments provided for template
2438/// parameters that precede \p Param in the template parameter list.
2439///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002440/// \param QualifierLoc Will be set to the nested-name-specifier (with
2441/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002442///
Douglas Gregor788cd062009-11-11 01:00:40 +00002443/// \returns the substituted template argument, or NULL if an error occurred.
2444static TemplateName
2445SubstDefaultTemplateArgument(Sema &SemaRef,
2446 TemplateDecl *Template,
2447 SourceLocation TemplateLoc,
2448 SourceLocation RAngleLoc,
2449 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002450 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2451 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002452 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002453 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002454
Douglas Gregor788cd062009-11-11 01:00:40 +00002455 MultiLevelTemplateArgumentList AllTemplateArgs
2456 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002457
Douglas Gregor788cd062009-11-11 01:00:40 +00002458 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002459 Template, Converted.data(),
2460 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002461 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002462
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002463 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002464 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002465 if (QualifierLoc) {
2466 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2467 AllTemplateArgs);
2468 if (!QualifierLoc)
2469 return TemplateName();
2470 }
2471
Douglas Gregor1d752d72011-03-02 18:46:51 +00002472 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002473 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002474 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002475 AllTemplateArgs);
2476}
2477
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002478/// \brief If the given template parameter has a default template
2479/// argument, substitute into that default template argument and
2480/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002481TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002482Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2483 SourceLocation TemplateLoc,
2484 SourceLocation RAngleLoc,
2485 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002486 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2487 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002488 if (!TypeParm->hasDefaultArgument())
2489 return TemplateArgumentLoc();
2490
John McCalla93c9342009-12-07 02:54:59 +00002491 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002492 TemplateLoc,
2493 RAngleLoc,
2494 TypeParm,
2495 Converted);
2496 if (DI)
2497 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2498
2499 return TemplateArgumentLoc();
2500 }
2501
2502 if (NonTypeTemplateParmDecl *NonTypeParm
2503 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2504 if (!NonTypeParm->hasDefaultArgument())
2505 return TemplateArgumentLoc();
2506
John McCall60d7b3a2010-08-24 06:29:42 +00002507 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002508 TemplateLoc,
2509 RAngleLoc,
2510 NonTypeParm,
2511 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002512 if (Arg.isInvalid())
2513 return TemplateArgumentLoc();
2514
2515 Expr *ArgE = Arg.takeAs<Expr>();
2516 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2517 }
2518
2519 TemplateTemplateParmDecl *TempTempParm
2520 = cast<TemplateTemplateParmDecl>(Param);
2521 if (!TempTempParm->hasDefaultArgument())
2522 return TemplateArgumentLoc();
2523
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002524
Douglas Gregor1d752d72011-03-02 18:46:51 +00002525 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002526 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002527 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002528 RAngleLoc,
2529 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002530 Converted,
2531 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002532 if (TName.isNull())
2533 return TemplateArgumentLoc();
2534
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002536 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002537 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2538}
2539
Douglas Gregore7526412009-11-11 19:31:23 +00002540/// \brief Check that the given template argument corresponds to the given
2541/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002542///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002543/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002544/// checked.
2545///
2546/// \param Arg The template argument.
2547///
2548/// \param Template The template in which the template argument resides.
2549///
2550/// \param TemplateLoc The location of the template name for the template
2551/// whose argument list we're matching.
2552///
2553/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2554/// the template argument list.
2555///
2556/// \param ArgumentPackIndex The index into the argument pack where this
2557/// argument will be placed. Only valid if the parameter is a parameter pack.
2558///
2559/// \param Converted The checked, converted argument will be added to the
2560/// end of this small vector.
2561///
2562/// \param CTAK Describes how we arrived at this particular template argument:
2563/// explicitly written, deduced, etc.
2564///
2565/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002566bool Sema::CheckTemplateArgument(NamedDecl *Param,
2567 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002568 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002569 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002570 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002571 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002572 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002573 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002574 // Check template type parameters.
2575 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002576 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002577
Douglas Gregord9e15302009-11-11 19:41:09 +00002578 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002579 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002580 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002581 // with the template arguments we've seen thus far. But if the
2582 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002583 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002584 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2585 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002586
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002587 if (NTTPType->isDependentType() &&
2588 !isa<TemplateTemplateParmDecl>(Template) &&
2589 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002590 // Do substitution on the type of the non-type template parameter.
2591 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002592 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002593 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002594
2595 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002596 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002597 NTTPType = SubstType(NTTPType,
2598 MultiLevelTemplateArgumentList(TemplateArgs),
2599 NTTP->getLocation(),
2600 NTTP->getDeclName());
2601 // If that worked, check the non-type template parameter type
2602 // for validity.
2603 if (!NTTPType.isNull())
2604 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2605 NTTP->getLocation());
2606 if (NTTPType.isNull())
2607 return true;
2608 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002609
Douglas Gregore7526412009-11-11 19:31:23 +00002610 switch (Arg.getArgument().getKind()) {
2611 case TemplateArgument::Null:
2612 assert(false && "Should never see a NULL template argument here");
2613 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002614
Douglas Gregore7526412009-11-11 19:31:23 +00002615 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002616 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002617 ExprResult Res =
2618 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2619 Result, CTAK);
2620 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002621 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002622
Douglas Gregor910f8002010-11-07 23:05:16 +00002623 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002624 break;
2625 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002626
Douglas Gregore7526412009-11-11 19:31:23 +00002627 case TemplateArgument::Declaration:
2628 case TemplateArgument::Integral:
2629 // We've already checked this template argument, so just copy
2630 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002631 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002632 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002633
Douglas Gregore7526412009-11-11 19:31:23 +00002634 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002635 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002636 // We were given a template template argument. It may not be ill-formed;
2637 // see below.
2638 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002639 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2640 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002641 // We have a template argument such as \c T::template X, which we
2642 // parsed as a template template argument. However, since we now
2643 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002644 // template name into an expression.
2645
2646 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2647 Arg.getTemplateNameLoc());
2648
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002649 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002650 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002651 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002652 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002653 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002654
Douglas Gregora7fc9012011-01-05 18:58:31 +00002655 // If we parsed the template argument as a pack expansion, create a
2656 // pack expansion expression.
2657 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002658 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2659 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002660 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002661 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002662
Douglas Gregore7526412009-11-11 19:31:23 +00002663 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002664 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2665 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002666 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002667
Douglas Gregor910f8002010-11-07 23:05:16 +00002668 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002669 break;
2670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002671
Douglas Gregore7526412009-11-11 19:31:23 +00002672 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002673 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002674 // therefore cannot be a non-type template argument.
2675 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2676 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002677
Douglas Gregore7526412009-11-11 19:31:23 +00002678 Diag(Param->getLocation(), diag::note_template_param_here);
2679 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002680
Douglas Gregore7526412009-11-11 19:31:23 +00002681 case TemplateArgument::Type: {
2682 // We have a non-type template parameter but the template
2683 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002684
Douglas Gregore7526412009-11-11 19:31:23 +00002685 // C++ [temp.arg]p2:
2686 // In a template-argument, an ambiguity between a type-id and
2687 // an expression is resolved to a type-id, regardless of the
2688 // form of the corresponding template-parameter.
2689 //
2690 // We warn specifically about this case, since it can be rather
2691 // confusing for users.
2692 QualType T = Arg.getArgument().getAsType();
2693 SourceRange SR = Arg.getSourceRange();
2694 if (T->isFunctionType())
2695 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2696 else
2697 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2698 Diag(Param->getLocation(), diag::note_template_param_here);
2699 return true;
2700 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002701
Douglas Gregore7526412009-11-11 19:31:23 +00002702 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002703 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002704 break;
2705 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002706
Douglas Gregore7526412009-11-11 19:31:23 +00002707 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002708 }
2709
2710
Douglas Gregore7526412009-11-11 19:31:23 +00002711 // Check template template parameters.
2712 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002713
Douglas Gregore7526412009-11-11 19:31:23 +00002714 // Substitute into the template parameter list of the template
2715 // template parameter, since previously-supplied template arguments
2716 // may appear within the template template parameter.
2717 {
2718 // Set up a template instantiation context.
2719 LocalInstantiationScope Scope(*this);
2720 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002721 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002722 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002723
2724 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002725 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002726 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002728 MultiLevelTemplateArgumentList(TemplateArgs)));
2729 if (!TempParm)
2730 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002731 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732
Douglas Gregore7526412009-11-11 19:31:23 +00002733 switch (Arg.getArgument().getKind()) {
2734 case TemplateArgument::Null:
2735 assert(false && "Should never see a NULL template argument here");
2736 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002737
Douglas Gregore7526412009-11-11 19:31:23 +00002738 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002739 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002740 if (CheckTemplateArgument(TempParm, Arg))
2741 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002742
Douglas Gregor910f8002010-11-07 23:05:16 +00002743 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002744 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002745
Douglas Gregore7526412009-11-11 19:31:23 +00002746 case TemplateArgument::Expression:
2747 case TemplateArgument::Type:
2748 // We have a template template parameter but the template
2749 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002750 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2751 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002752 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002753
Douglas Gregore7526412009-11-11 19:31:23 +00002754 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002755 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002756 "Declaration argument with template template parameter");
2757 break;
2758 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002759 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002760 "Integral argument with template template parameter");
2761 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002762
Douglas Gregore7526412009-11-11 19:31:23 +00002763 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002764 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002765 break;
2766 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002767
Douglas Gregore7526412009-11-11 19:31:23 +00002768 return false;
2769}
2770
Douglas Gregorc15cb382009-02-09 23:23:08 +00002771/// \brief Check that the given template argument list is well-formed
2772/// for specializing the given template.
2773bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2774 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002775 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002776 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002777 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002778 TemplateParameterList *Params = Template->getTemplateParameters();
2779 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002780 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002781 bool Invalid = false;
2782
John McCalld5532b62009-11-23 01:53:49 +00002783 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2784
Mike Stump1eb44332009-09-09 15:08:12 +00002785 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002786 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002787
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002788 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002789 (NumArgs < Params->getMinRequiredArguments() &&
2790 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002791 // FIXME: point at either the first arg beyond what we can handle,
2792 // or the '>', depending on whether we have too many or too few
2793 // arguments.
2794 SourceRange Range;
2795 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002796 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002797 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2798 << (NumArgs > NumParams)
2799 << (isa<ClassTemplateDecl>(Template)? 0 :
2800 isa<FunctionTemplateDecl>(Template)? 1 :
2801 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2802 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002803 Diag(Template->getLocation(), diag::note_template_decl_here)
2804 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002805 Invalid = true;
2806 }
Mike Stump1eb44332009-09-09 15:08:12 +00002807
2808 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002809 // [...] The type and form of each template-argument specified in
2810 // a template-id shall match the type and form specified for the
2811 // corresponding parameter declared by the template in its
2812 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002813 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002814 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2815 TemplateParameterList::iterator Param = Params->begin(),
2816 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002817 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002818 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002819 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002820 if (ArgIdx > NumArgs && PartialTemplateArgs)
2821 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002822
Douglas Gregorf35f8282009-11-11 21:54:23 +00002823 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002824 // If we have an expanded parameter pack, make sure we don't have too
2825 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002826 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002827 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002828 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002829 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2830 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2831 << true
2832 << (isa<ClassTemplateDecl>(Template)? 0 :
2833 isa<FunctionTemplateDecl>(Template)? 1 :
2834 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2835 << Template;
2836 Diag(Template->getLocation(), diag::note_template_decl_here)
2837 << Params->getSourceRange();
2838 return true;
2839 }
2840 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002841
Douglas Gregorf35f8282009-11-11 21:54:23 +00002842 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002843 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2844 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002845 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002846 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002847
Douglas Gregor14be16b2010-12-20 16:57:52 +00002848 if ((*Param)->isTemplateParameterPack()) {
2849 // The template parameter was a template parameter pack, so take the
2850 // deduced argument and place it on the argument pack. Note that we
2851 // stay on the same template parameter so that we can deduce more
2852 // arguments.
2853 ArgumentPack.push_back(Converted.back());
2854 Converted.pop_back();
2855 } else {
2856 // Move to the next template parameter.
2857 ++Param;
2858 }
2859 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002860 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002861 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002862
2863 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002864 // arguments, just break out now and we'll fill in the argument pack below.
2865 if ((*Param)->isTemplateParameterPack())
2866 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002867
Douglas Gregorf35f8282009-11-11 21:54:23 +00002868 // We have a default template argument that we will use.
2869 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002870
Douglas Gregorf35f8282009-11-11 21:54:23 +00002871 // Retrieve the default template argument from the template
2872 // parameter. For each kind of template parameter, we substitute the
2873 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002874 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002875 // the default argument.
2876 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2877 if (!TTP->hasDefaultArgument()) {
2878 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2879 break;
2880 }
2881
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002882 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002883 Template,
2884 TemplateLoc,
2885 RAngleLoc,
2886 TTP,
2887 Converted);
2888 if (!ArgType)
2889 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002890
Douglas Gregorf35f8282009-11-11 21:54:23 +00002891 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2892 ArgType);
2893 } else if (NonTypeTemplateParmDecl *NTTP
2894 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2895 if (!NTTP->hasDefaultArgument()) {
2896 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2897 break;
2898 }
2899
John McCall60d7b3a2010-08-24 06:29:42 +00002900 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002901 TemplateLoc,
2902 RAngleLoc,
2903 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002904 Converted);
2905 if (E.isInvalid())
2906 return true;
2907
2908 Expr *Ex = E.takeAs<Expr>();
2909 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2910 } else {
2911 TemplateTemplateParmDecl *TempParm
2912 = cast<TemplateTemplateParmDecl>(*Param);
2913
2914 if (!TempParm->hasDefaultArgument()) {
2915 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2916 break;
2917 }
2918
Douglas Gregor1d752d72011-03-02 18:46:51 +00002919 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002920 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002921 TemplateLoc,
2922 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002923 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002924 Converted,
2925 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002926 if (Name.isNull())
2927 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002928
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002929 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2930 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002931 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002932
Douglas Gregorf35f8282009-11-11 21:54:23 +00002933 // Introduce an instantiation record that describes where we are using
2934 // the default template argument.
2935 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002936 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002937 SourceRange(TemplateLoc, RAngleLoc));
2938
Douglas Gregorf35f8282009-11-11 21:54:23 +00002939 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002940 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002941 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002942 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002943
Douglas Gregor67714232011-03-03 02:41:12 +00002944 // Core issue 150 (assumed resolution): if this is a template template
2945 // parameter, keep track of the default template arguments from the
2946 // template definition.
2947 if (isTemplateTemplateParameter)
2948 TemplateArgs.addArgument(Arg);
2949
Douglas Gregor14be16b2010-12-20 16:57:52 +00002950 // Move to the next template parameter and argument.
2951 ++Param;
2952 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002953 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002954
Douglas Gregor14be16b2010-12-20 16:57:52 +00002955 // Form argument packs for each of the parameter packs remaining.
2956 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002957 // If we're checking a partial list of template arguments, don't fill
2958 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002959
2960 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002961 if (PartialTemplateArgs && ArgumentPack.empty()) {
2962 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002963 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002964 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002965 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002966 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2967 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002968 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002969 ArgumentPack.clear();
2970 }
2971 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002972
Douglas Gregor14be16b2010-12-20 16:57:52 +00002973 ++Param;
2974 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002975
Douglas Gregorc15cb382009-02-09 23:23:08 +00002976 return Invalid;
2977}
2978
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002979namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002980 class UnnamedLocalNoLinkageFinder
2981 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002982 {
2983 Sema &S;
2984 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002985
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002986 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002987
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002988 public:
2989 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2990
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002991 bool Visit(QualType T) {
2992 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002993 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002994
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002995#define TYPE(Class, Parent) \
2996 bool Visit##Class##Type(const Class##Type *);
2997#define ABSTRACT_TYPE(Class, Parent) \
2998 bool Visit##Class##Type(const Class##Type *) { return false; }
2999#define NON_CANONICAL_TYPE(Class, Parent) \
3000 bool Visit##Class##Type(const Class##Type *) { return false; }
3001#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003002
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003003 bool VisitTagDecl(const TagDecl *Tag);
3004 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3005 };
3006}
3007
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003008bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003009 return false;
3010}
3011
3012bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3013 return Visit(T->getElementType());
3014}
3015
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003016bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003017 return Visit(T->getPointeeType());
3018}
3019
3020bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003021 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003022 return Visit(T->getPointeeType());
3023}
3024
3025bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003026 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003027 return Visit(T->getPointeeType());
3028}
3029
3030bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003032 return Visit(T->getPointeeType());
3033}
3034
3035bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003036 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003037 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3038}
3039
3040bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003041 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003042 return Visit(T->getElementType());
3043}
3044
3045bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003046 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003047 return Visit(T->getElementType());
3048}
3049
3050bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003051 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003052 return Visit(T->getElementType());
3053}
3054
3055bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003056 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003057 return Visit(T->getElementType());
3058}
3059
3060bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003061 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003062 return Visit(T->getElementType());
3063}
3064
3065bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3066 return Visit(T->getElementType());
3067}
3068
3069bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3070 return Visit(T->getElementType());
3071}
3072
3073bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3074 const FunctionProtoType* T) {
3075 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003076 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003077 A != AEnd; ++A) {
3078 if (Visit(*A))
3079 return true;
3080 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003081
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003082 return Visit(T->getResultType());
3083}
3084
3085bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3086 const FunctionNoProtoType* T) {
3087 return Visit(T->getResultType());
3088}
3089
3090bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3091 const UnresolvedUsingType*) {
3092 return false;
3093}
3094
3095bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3096 return false;
3097}
3098
3099bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3100 return Visit(T->getUnderlyingType());
3101}
3102
3103bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3104 return false;
3105}
3106
Richard Smith34b41d92011-02-20 03:19:35 +00003107bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3108 return Visit(T->getDeducedType());
3109}
3110
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003111bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3112 return VisitTagDecl(T->getDecl());
3113}
3114
3115bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3116 return VisitTagDecl(T->getDecl());
3117}
3118
3119bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3120 const TemplateTypeParmType*) {
3121 return false;
3122}
3123
Douglas Gregorc3069d62011-01-14 02:55:32 +00003124bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3125 const SubstTemplateTypeParmPackType *) {
3126 return false;
3127}
3128
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003129bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3130 const TemplateSpecializationType*) {
3131 return false;
3132}
3133
3134bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3135 const InjectedClassNameType* T) {
3136 return VisitTagDecl(T->getDecl());
3137}
3138
3139bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3140 const DependentNameType* T) {
3141 return VisitNestedNameSpecifier(T->getQualifier());
3142}
3143
3144bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3145 const DependentTemplateSpecializationType* T) {
3146 return VisitNestedNameSpecifier(T->getQualifier());
3147}
3148
Douglas Gregor7536dd52010-12-20 02:24:11 +00003149bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3150 const PackExpansionType* T) {
3151 return Visit(T->getPattern());
3152}
3153
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003154bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3155 return false;
3156}
3157
3158bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3159 const ObjCInterfaceType *) {
3160 return false;
3161}
3162
3163bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3164 const ObjCObjectPointerType *) {
3165 return false;
3166}
3167
3168bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3169 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3170 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3171 << S.Context.getTypeDeclType(Tag) << SR;
3172 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003173 }
3174
Richard Smith162e1c12011-04-15 14:24:37 +00003175 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003176 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3177 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3178 return true;
3179 }
3180
3181 return false;
3182}
3183
3184bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3185 NestedNameSpecifier *NNS) {
3186 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3187 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003188
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003189 switch (NNS->getKind()) {
3190 case NestedNameSpecifier::Identifier:
3191 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003192 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003193 case NestedNameSpecifier::Global:
3194 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003195
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003196 case NestedNameSpecifier::TypeSpec:
3197 case NestedNameSpecifier::TypeSpecWithTemplate:
3198 return Visit(QualType(NNS->getAsType(), 0));
3199 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003200 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003201}
3202
3203
Douglas Gregorc15cb382009-02-09 23:23:08 +00003204/// \brief Check a template argument against its corresponding
3205/// template type parameter.
3206///
3207/// This routine implements the semantics of C++ [temp.arg.type]. It
3208/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003209bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003210 TypeSourceInfo *ArgInfo) {
3211 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003212 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003213 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003214
3215 if (Arg->isVariablyModifiedType()) {
3216 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003217 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003218 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003219 }
3220
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003221 // C++03 [temp.arg.type]p2:
3222 // A local type, a type with no linkage, an unnamed type or a type
3223 // compounded from any of these types shall not be used as a
3224 // template-argument for a template type-parameter.
3225 //
3226 // C++0x allows these, and even in C++03 we allow them as an extension with
3227 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003228 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003229 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3230 (void)Finder.Visit(Context.getCanonicalType(Arg));
3231 }
3232
Douglas Gregorc15cb382009-02-09 23:23:08 +00003233 return false;
3234}
3235
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003236/// \brief Checks whether the given template argument is the address
3237/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003238static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003239CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3240 NonTypeTemplateParmDecl *Param,
3241 QualType ParamType,
3242 Expr *ArgIn,
3243 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003244 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003245 Expr *Arg = ArgIn;
3246 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003247
3248 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003249 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003250 Arg = Cast->getSubExpr();
3251
3252 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003253 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003254 // A template-argument for a non-type, non-template
3255 // template-parameter shall be one of: [...]
3256 //
3257 // -- the address of an object or function with external
3258 // linkage, including function templates and function
3259 // template-ids but excluding non-static class members,
3260 // expressed as & id-expression where the & is optional if
3261 // the name refers to a function or array, or if the
3262 // corresponding template-parameter is a reference; or
3263 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003264
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003265 // In C++98/03 mode, give an extension warning on any extra parentheses.
3266 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3267 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003268 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003269 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003270 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003271 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003272 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003273 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003274 }
3275
3276 Arg = Parens->getSubExpr();
3277 }
3278
Douglas Gregorb7a09262010-04-01 18:32:35 +00003279 bool AddressTaken = false;
3280 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003281 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003282 if (UnOp->getOpcode() == UO_AddrOf) {
Francois Pichet11f98d02011-04-28 05:12:34 +00003283 // Support &__uuidof(class_with_uuid) as a non-type template argument.
3284 // Very common in Microsoft COM headers.
3285 if (S.getLangOptions().Microsoft &&
3286 isa<CXXUuidofExpr>(UnOp->getSubExpr())) {
3287 Converted = TemplateArgument(ArgIn);
3288 return false;
3289 }
3290
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003291 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003292 AddressTaken = true;
3293 AddrOpLoc = UnOp->getOperatorLoc();
3294 }
Francois Picheta343a412011-04-29 09:08:14 +00003295 } else {
3296 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3297 Converted = TemplateArgument(ArgIn);
3298 return false;
3299 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003300 DRE = dyn_cast<DeclRefExpr>(Arg);
Francois Picheta343a412011-04-29 09:08:14 +00003301 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003302 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003303 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3304 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003305 S.Diag(Param->getLocation(), diag::note_template_param_here);
3306 return true;
3307 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003308
3309 // Stop checking the precise nature of the argument if it is value dependent,
3310 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003311 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003312 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003313 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003314 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003315
Douglas Gregorb7a09262010-04-01 18:32:35 +00003316 if (!isa<ValueDecl>(DRE->getDecl())) {
3317 S.Diag(Arg->getSourceRange().getBegin(),
3318 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003319 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003320 S.Diag(Param->getLocation(), diag::note_template_param_here);
3321 return true;
3322 }
3323
3324 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003325
3326 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003327 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3328 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003329 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003330 S.Diag(Param->getLocation(), diag::note_template_param_here);
3331 return true;
3332 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003333
3334 // Cannot refer to non-static member functions
3335 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003336 if (!Method->isStatic()) {
3337 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003338 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003339 S.Diag(Param->getLocation(), diag::note_template_param_here);
3340 return true;
3341 }
Mike Stump1eb44332009-09-09 15:08:12 +00003342
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003343 // Functions must have external linkage.
3344 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003345 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003346 S.Diag(Arg->getSourceRange().getBegin(),
3347 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003348 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003349 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003350 << true;
3351 return true;
3352 }
3353
3354 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003355 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003356
Douglas Gregorb7a09262010-04-01 18:32:35 +00003357 // If the template parameter has pointer type, the function decays.
3358 if (ParamType->isPointerType() && !AddressTaken)
3359 ArgType = S.Context.getPointerType(Func->getType());
3360 else if (AddressTaken && ParamType->isReferenceType()) {
3361 // If we originally had an address-of operator, but the
3362 // parameter has reference type, complain and (if things look
3363 // like they will work) drop the address-of operator.
3364 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3365 ParamType.getNonReferenceType())) {
3366 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3367 << ParamType;
3368 S.Diag(Param->getLocation(), diag::note_template_param_here);
3369 return true;
3370 }
3371
3372 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3373 << ParamType
3374 << FixItHint::CreateRemoval(AddrOpLoc);
3375 S.Diag(Param->getLocation(), diag::note_template_param_here);
3376
3377 ArgType = Func->getType();
3378 }
3379 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003380 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003381 S.Diag(Arg->getSourceRange().getBegin(),
3382 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003383 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003384 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003385 << true;
3386 return true;
3387 }
3388
Douglas Gregorb7a09262010-04-01 18:32:35 +00003389 // A value of reference type is not an object.
3390 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003391 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003392 diag::err_template_arg_reference_var)
3393 << Var->getType() << Arg->getSourceRange();
3394 S.Diag(Param->getLocation(), diag::note_template_param_here);
3395 return true;
3396 }
3397
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003398 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003399 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003400
3401 // If the template parameter has pointer type, we must have taken
3402 // the address of this object.
3403 if (ParamType->isReferenceType()) {
3404 if (AddressTaken) {
3405 // If we originally had an address-of operator, but the
3406 // parameter has reference type, complain and (if things look
3407 // like they will work) drop the address-of operator.
3408 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3409 ParamType.getNonReferenceType())) {
3410 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3411 << ParamType;
3412 S.Diag(Param->getLocation(), diag::note_template_param_here);
3413 return true;
3414 }
3415
3416 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3417 << ParamType
3418 << FixItHint::CreateRemoval(AddrOpLoc);
3419 S.Diag(Param->getLocation(), diag::note_template_param_here);
3420
3421 ArgType = Var->getType();
3422 }
3423 } else if (!AddressTaken && ParamType->isPointerType()) {
3424 if (Var->getType()->isArrayType()) {
3425 // Array-to-pointer decay.
3426 ArgType = S.Context.getArrayDecayedType(Var->getType());
3427 } else {
3428 // If the template parameter has pointer type but the address of
3429 // this object was not taken, complain and (possibly) recover by
3430 // taking the address of the entity.
3431 ArgType = S.Context.getPointerType(Var->getType());
3432 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3433 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3434 << ParamType;
3435 S.Diag(Param->getLocation(), diag::note_template_param_here);
3436 return true;
3437 }
3438
3439 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3440 << ParamType
3441 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3442
3443 S.Diag(Param->getLocation(), diag::note_template_param_here);
3444 }
3445 }
3446 } else {
3447 // We found something else, but we don't know specifically what it is.
3448 S.Diag(Arg->getSourceRange().getBegin(),
3449 diag::err_template_arg_not_object_or_func)
3450 << Arg->getSourceRange();
3451 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3452 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003453 }
Mike Stump1eb44332009-09-09 15:08:12 +00003454
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003455 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003456 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003457 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003458 // For pointer-to-object types, qualification conversions are
3459 // permitted.
3460 } else {
3461 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3462 if (!ParamRef->getPointeeType()->isFunctionType()) {
3463 // C++ [temp.arg.nontype]p5b3:
3464 // For a non-type template-parameter of type reference to
3465 // object, no conversions apply. The type referred to by the
3466 // reference may be more cv-qualified than the (otherwise
3467 // identical) type of the template- argument. The
3468 // template-parameter is bound directly to the
3469 // template-argument, which shall be an lvalue.
3470
3471 // FIXME: Other qualifiers?
3472 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3473 unsigned ArgQuals = ArgType.getCVRQualifiers();
3474
3475 if ((ParamQuals | ArgQuals) != ParamQuals) {
3476 S.Diag(Arg->getSourceRange().getBegin(),
3477 diag::err_template_arg_ref_bind_ignores_quals)
3478 << ParamType << Arg->getType()
3479 << Arg->getSourceRange();
3480 S.Diag(Param->getLocation(), diag::note_template_param_here);
3481 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003482 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003483 }
3484 }
3485
3486 // At this point, the template argument refers to an object or
3487 // function with external linkage. We now need to check whether the
3488 // argument and parameter types are compatible.
3489 if (!S.Context.hasSameUnqualifiedType(ArgType,
3490 ParamType.getNonReferenceType())) {
3491 // We can't perform this conversion or binding.
3492 if (ParamType->isReferenceType())
3493 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3494 << ParamType << Arg->getType() << Arg->getSourceRange();
3495 else
3496 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3497 << Arg->getType() << ParamType << Arg->getSourceRange();
3498 S.Diag(Param->getLocation(), diag::note_template_param_here);
3499 return true;
3500 }
3501 }
3502
3503 // Create the template argument.
3504 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003505 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003506 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003507}
3508
3509/// \brief Checks whether the given template argument is a pointer to
3510/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003511bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003512 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003513 bool Invalid = false;
3514
3515 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003516 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003517 Arg = Cast->getSubExpr();
3518
3519 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003520 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003521 // A template-argument for a non-type, non-template
3522 // template-parameter shall be one of: [...]
3523 //
3524 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003525 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003526
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003527 // In C++98/03 mode, give an extension warning on any extra parentheses.
3528 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3529 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003530 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003531 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003532 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003533 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003534 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003535 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003536 }
3537
3538 Arg = Parens->getSubExpr();
3539 }
3540
Douglas Gregorcaddba02009-11-12 18:38:13 +00003541 // A pointer-to-member constant written &Class::member.
3542 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003543 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003544 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3545 if (DRE && !DRE->getQualifier())
3546 DRE = 0;
3547 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003548 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003549 // A constant of pointer-to-member type.
3550 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3551 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3552 if (VD->getType()->isMemberPointerType()) {
3553 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003554 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003555 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3556 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003557 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003558 else
3559 Converted = TemplateArgument(VD->getCanonicalDecl());
3560 return Invalid;
3561 }
3562 }
3563 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003564
Douglas Gregorcaddba02009-11-12 18:38:13 +00003565 DRE = 0;
3566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003567
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003568 if (!DRE)
3569 return Diag(Arg->getSourceRange().getBegin(),
3570 diag::err_template_arg_not_pointer_to_member_form)
3571 << Arg->getSourceRange();
3572
3573 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3574 assert((isa<FieldDecl>(DRE->getDecl()) ||
3575 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3576 "Only non-static member pointers can make it here");
3577
3578 // Okay: this is the address of a non-static member, and therefore
3579 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003580 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003581 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003582 else
3583 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003584 return Invalid;
3585 }
3586
3587 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003588 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003589 diag::err_template_arg_not_pointer_to_member_form)
3590 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003591 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003592 diag::note_template_arg_refers_here);
3593 return true;
3594}
3595
Douglas Gregorc15cb382009-02-09 23:23:08 +00003596/// \brief Check a template argument against its corresponding
3597/// non-type template parameter.
3598///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003599/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003600/// If an error occurred, it returns ExprError(); otherwise, it
3601/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003602/// InstantiatedParamType is the type of the non-type template
3603/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003604ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3605 QualType InstantiatedParamType, Expr *Arg,
3606 TemplateArgument &Converted,
3607 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003608 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3609
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003610 // If either the parameter has a dependent type or the argument is
3611 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003612 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3613 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003614 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003615 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003616 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003617
3618 // C++ [temp.arg.nontype]p5:
3619 // The following conversions are performed on each expression used
3620 // as a non-type template-argument. If a non-type
3621 // template-argument cannot be converted to the type of the
3622 // corresponding template-parameter then the program is
3623 // ill-formed.
3624 //
3625 // -- for a non-type template-parameter of integral or
3626 // enumeration type, integral promotions (4.5) and integral
3627 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003628 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003629 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003630 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003631 // C++ [temp.arg.nontype]p1:
3632 // A template-argument for a non-type, non-template
3633 // template-parameter shall be one of:
3634 //
3635 // -- an integral constant-expression of integral or enumeration
3636 // type; or
3637 // -- the name of a non-type template-parameter; or
3638 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003639 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003640 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003641 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003642 diag::err_template_arg_not_integral_or_enumeral)
3643 << ArgType << Arg->getSourceRange();
3644 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003645 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003646 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003647 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003648 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3649 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003650 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003651 }
3652
Douglas Gregor02024a92010-03-28 02:42:43 +00003653 // From here on out, all we care about are the unqualified forms
3654 // of the parameter and argument types.
3655 ParamType = ParamType.getUnqualifiedType();
3656 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003657
3658 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003659 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003660 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003661 } else if (CTAK == CTAK_Deduced) {
3662 // C++ [temp.deduct.type]p17:
3663 // If, in the declaration of a function template with a non-type
3664 // template-parameter, the non-type template- parameter is used
3665 // in an expression in the function parameter-list and, if the
3666 // corresponding template-argument is deduced, the
3667 // template-argument type shall match the type of the
3668 // template-parameter exactly, except that a template-argument
3669 // deduced from an array bound may be of any integral type.
3670 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3671 << ArgType << ParamType;
3672 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003673 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003674 } else if (ParamType->isBooleanType()) {
3675 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003676 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003677 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3678 !ParamType->isEnumeralType()) {
3679 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003680 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003681 } else {
3682 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003683 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003684 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003685 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003686 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003687 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003688 }
3689
Douglas Gregorc7469372011-05-04 21:55:00 +00003690 // Add the value of this argument to the list of converted
3691 // arguments. We use the bitwidth and signedness of the template
3692 // parameter.
3693 if (Arg->isValueDependent()) {
3694 // The argument is value-dependent. Create a new
3695 // TemplateArgument with the converted expression.
3696 Converted = TemplateArgument(Arg);
3697 return Owned(Arg);
3698 }
3699
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003700 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003701 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003702 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003703
Douglas Gregorc7469372011-05-04 21:55:00 +00003704 if (ParamType->isBooleanType()) {
3705 // Value must be zero or one.
3706 Value = Value != 0;
3707 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3708 if (Value.getBitWidth() != AllowedBits)
3709 Value = Value.extOrTrunc(AllowedBits);
3710 Value.setIsSigned(IntegerType->isSignedIntegerType());
3711 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003712 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003713
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003714 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003715 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003716 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003717 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003718 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003719 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003720
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003721 // Complain if an unsigned parameter received a negative value.
3722 if (IntegerType->isUnsignedIntegerType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003723 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003724 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3725 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3726 << Arg->getSourceRange();
3727 Diag(Param->getLocation(), diag::note_template_param_here);
3728 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003729
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003730 // Complain if we overflowed the template parameter's type.
3731 unsigned RequiredBits;
3732 if (IntegerType->isUnsignedIntegerType())
3733 RequiredBits = OldValue.getActiveBits();
3734 else if (OldValue.isUnsigned())
3735 RequiredBits = OldValue.getActiveBits() + 1;
3736 else
3737 RequiredBits = OldValue.getMinSignedBits();
3738 if (RequiredBits > AllowedBits) {
3739 Diag(Arg->getSourceRange().getBegin(),
3740 diag::warn_template_arg_too_large)
3741 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3742 << Arg->getSourceRange();
3743 Diag(Param->getLocation(), diag::note_template_param_here);
3744 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003745 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003746
John McCall833ca992009-10-29 08:12:44 +00003747 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003748 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003749 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003750 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003751 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003752
John McCall6bb80172010-03-30 21:47:33 +00003753 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3754
Douglas Gregorb7a09262010-04-01 18:32:35 +00003755 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3756 // from a template argument of type std::nullptr_t to a non-type
3757 // template parameter of type pointer to object, pointer to
3758 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003759 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003760 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3761 Converted = TemplateArgument((NamedDecl *)0);
John Wiegley429bb272011-04-08 18:41:53 +00003762 return Owned(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003763 }
3764
Douglas Gregorb86b0572009-02-11 01:18:59 +00003765 // Handle pointer-to-function, reference-to-function, and
3766 // pointer-to-member-function all in (roughly) the same way.
3767 if (// -- For a non-type template-parameter of type pointer to
3768 // function, only the function-to-pointer conversion (4.3) is
3769 // applied. If the template-argument represents a set of
3770 // overloaded functions (or a pointer to such), the matching
3771 // function is selected from the set (13.4).
3772 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003773 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003774 // -- For a non-type template-parameter of type reference to
3775 // function, no conversions apply. If the template-argument
3776 // represents a set of overloaded functions, the matching
3777 // function is selected from the set (13.4).
3778 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003779 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003780 // -- For a non-type template-parameter of type pointer to
3781 // member function, no conversions apply. If the
3782 // template-argument represents a set of overloaded member
3783 // functions, the matching member function is selected from
3784 // the set (13.4).
3785 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003786 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003787 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003788
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003789 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003790 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003791 true,
3792 FoundResult)) {
3793 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003794 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003795
3796 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3797 ArgType = Arg->getType();
3798 } else
John Wiegley429bb272011-04-08 18:41:53 +00003799 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003800 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003801
John Wiegley429bb272011-04-08 18:41:53 +00003802 if (!ParamType->isMemberPointerType()) {
3803 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3804 ParamType,
3805 Arg, Converted))
3806 return ExprError();
3807 return Owned(Arg);
3808 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003809
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003810 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3811 false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003812 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003813 } else if (!Context.hasSameUnqualifiedType(ArgType,
3814 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003815 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003816 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003817 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003818 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003819 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003820 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003821 }
Mike Stump1eb44332009-09-09 15:08:12 +00003822
John Wiegley429bb272011-04-08 18:41:53 +00003823 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3824 return ExprError();
3825 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003826 }
3827
Chris Lattnerfe90de72009-02-20 21:37:53 +00003828 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003829 // -- for a non-type template-parameter of type pointer to
3830 // object, qualification conversions (4.4) and the
3831 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003832 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003833 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003834 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003835
John Wiegley429bb272011-04-08 18:41:53 +00003836 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3837 ParamType,
3838 Arg, Converted))
3839 return ExprError();
3840 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003841 }
Mike Stump1eb44332009-09-09 15:08:12 +00003842
Ted Kremenek6217b802009-07-29 21:53:49 +00003843 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003844 // -- For a non-type template-parameter of type reference to
3845 // object, no conversions apply. The type referred to by the
3846 // reference may be more cv-qualified than the (otherwise
3847 // identical) type of the template-argument. The
3848 // template-parameter is bound directly to the
3849 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003850 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003851 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003852
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003853 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003854 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3855 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003856 true,
3857 FoundResult)) {
3858 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003859 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003860
3861 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3862 ArgType = Arg->getType();
3863 } else
John Wiegley429bb272011-04-08 18:41:53 +00003864 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003865 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003866
John Wiegley429bb272011-04-08 18:41:53 +00003867 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3868 ParamType,
3869 Arg, Converted))
3870 return ExprError();
3871 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003872 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003873
3874 // -- For a non-type template-parameter of type pointer to data
3875 // member, qualification conversions (4.4) are applied.
3876 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3877
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003878 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003879 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003880 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003881 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003882 } else {
3883 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003884 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003885 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003886 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003887 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003888 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003889 }
3890
John Wiegley429bb272011-04-08 18:41:53 +00003891 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3892 return ExprError();
3893 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003894}
3895
3896/// \brief Check a template argument against its corresponding
3897/// template template parameter.
3898///
3899/// This routine implements the semantics of C++ [temp.arg.template].
3900/// It returns true if an error occurred, and false otherwise.
3901bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003902 const TemplateArgumentLoc &Arg) {
3903 TemplateName Name = Arg.getArgument().getAsTemplate();
3904 TemplateDecl *Template = Name.getAsTemplateDecl();
3905 if (!Template) {
3906 // Any dependent template name is fine.
3907 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3908 return false;
3909 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003910
Richard Smith3e4c6c42011-05-05 21:57:07 +00003911 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00003912 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00003913 // the name of a class template or an alias template, expressed as an
3914 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00003915 // primary class templates are considered when matching the
3916 // template template argument with the corresponding parameter;
3917 // partial specializations are not considered even if their
3918 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003919 //
3920 // Note that we also allow template template parameters here, which
3921 // will happen when we are dealing with, e.g., class template
3922 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003923 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00003924 !isa<TemplateTemplateParmDecl>(Template) &&
3925 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003926 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003927 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003928 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003929 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003930 << Template;
3931 }
3932
3933 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3934 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003935 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003936 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003937 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003938}
3939
Douglas Gregor02024a92010-03-28 02:42:43 +00003940/// \brief Given a non-type template argument that refers to a
3941/// declaration and the type of its corresponding non-type template
3942/// parameter, produce an expression that properly refers to that
3943/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003944ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003945Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3946 QualType ParamType,
3947 SourceLocation Loc) {
3948 assert(Arg.getKind() == TemplateArgument::Declaration &&
3949 "Only declaration template arguments permitted here");
3950 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3951
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003952 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003953 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3954 // If the value is a class member, we might have a pointer-to-member.
3955 // Determine whether the non-type template template parameter is of
3956 // pointer-to-member type. If so, we need to build an appropriate
3957 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3958 // would refer to the member itself.
3959 if (ParamType->isMemberPointerType()) {
3960 QualType ClassType
3961 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3962 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003963 = NestedNameSpecifier::Create(Context, 0, false,
3964 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003965 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003966 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003967
3968 // The actual value-ness of this is unimportant, but for
3969 // internal consistency's sake, references to instance methods
3970 // are r-values.
3971 ExprValueKind VK = VK_LValue;
3972 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3973 VK = VK_RValue;
3974
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003975 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003976 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003977 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003978 Loc,
3979 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003980 if (RefExpr.isInvalid())
3981 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003982
John McCall2de56d12010-08-25 11:45:40 +00003983 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003984
Douglas Gregorc0c83002010-04-30 21:46:38 +00003985 // We might need to perform a trailing qualification conversion, since
3986 // the element type on the parameter could be more qualified than the
3987 // element type in the expression we constructed.
3988 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John Wiegley429bb272011-04-08 18:41:53 +00003989 ParamType.getUnqualifiedType(), false))
3990 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003991
Douglas Gregor02024a92010-03-28 02:42:43 +00003992 assert(!RefExpr.isInvalid() &&
3993 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003994 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003995 return move(RefExpr);
3996 }
3997 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003998
Douglas Gregor02024a92010-03-28 02:42:43 +00003999 QualType T = VD->getType().getNonReferenceType();
4000 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004001 // When the non-type template parameter is a pointer, take the
4002 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004003 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004004 if (RefExpr.isInvalid())
4005 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004006
4007 if (T->isFunctionType() || T->isArrayType()) {
4008 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004009 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4010 if (RefExpr.isInvalid())
4011 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004012
4013 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004014 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004015
Douglas Gregorb7a09262010-04-01 18:32:35 +00004016 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004017 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004018 }
4019
John McCallf89e55a2010-11-18 06:31:45 +00004020 ExprValueKind VK = VK_RValue;
4021
Douglas Gregor02024a92010-03-28 02:42:43 +00004022 // If the non-type template parameter has reference type, qualify the
4023 // resulting declaration reference with the extra qualifiers on the
4024 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004025 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4026 VK = VK_LValue;
4027 T = Context.getQualifiedType(T,
4028 TargetRef->getPointeeType().getQualifiers());
4029 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004030
John McCallf89e55a2010-11-18 06:31:45 +00004031 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004032}
4033
4034/// \brief Construct a new expression that refers to the given
4035/// integral template argument with the given source-location
4036/// information.
4037///
4038/// This routine takes care of the mapping from an integral template
4039/// argument (which may have any integral type) to the appropriate
4040/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004041ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004042Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4043 SourceLocation Loc) {
4044 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004045 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004046 QualType T = Arg.getIntegralType();
4047 if (T->isCharType() || T->isWideCharType())
4048 return Owned(new (Context) CharacterLiteral(
4049 Arg.getAsIntegral()->getZExtValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004050 T->isWideCharType(), T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004051 if (T->isBooleanType())
4052 return Owned(new (Context) CXXBoolLiteralExpr(
4053 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004054 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004055
Chris Lattner223de242011-04-25 20:37:58 +00004056 // If this is an enum type that we're instantiating, we need to use an integer
4057 // type the same size as the enumerator. We don't want to build an
4058 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004059 QualType BT;
4060 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004061 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004062 else
4063 BT = T;
4064
4065 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00004066 if (T->isEnumeralType()) {
4067 // FIXME: This is a hack. We need a better way to handle substituted
4068 // non-type template parameters.
Chris Lattner223de242011-04-25 20:37:58 +00004069 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4070 Context.getTrivialTypeSourceInfo(T, Loc),
4071 Loc, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00004072 }
4073
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004074 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004075}
4076
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004077/// \brief Match two template parameters within template parameter lists.
4078static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4079 bool Complain,
4080 Sema::TemplateParameterListEqualKind Kind,
4081 SourceLocation TemplateArgLoc) {
4082 // Check the actual kind (type, non-type, template).
4083 if (Old->getKind() != New->getKind()) {
4084 if (Complain) {
4085 unsigned NextDiag = diag::err_template_param_different_kind;
4086 if (TemplateArgLoc.isValid()) {
4087 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4088 NextDiag = diag::note_template_param_different_kind;
4089 }
4090 S.Diag(New->getLocation(), NextDiag)
4091 << (Kind != Sema::TPL_TemplateMatch);
4092 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4093 << (Kind != Sema::TPL_TemplateMatch);
4094 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004095
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004096 return false;
4097 }
4098
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004099 // Check that both are parameter packs are neither are parameter packs.
4100 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004101 // template template parameter, the template template parameter can have
4102 // a parameter pack where the template template argument does not.
4103 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4104 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4105 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004106 if (Complain) {
4107 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4108 if (TemplateArgLoc.isValid()) {
4109 S.Diag(TemplateArgLoc,
4110 diag::err_template_arg_template_params_mismatch);
4111 NextDiag = diag::note_template_parameter_pack_non_pack;
4112 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004113
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004114 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4115 : isa<NonTypeTemplateParmDecl>(New)? 1
4116 : 2;
4117 S.Diag(New->getLocation(), NextDiag)
4118 << ParamKind << New->isParameterPack();
4119 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4120 << ParamKind << Old->isParameterPack();
4121 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004122
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004123 return false;
4124 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004125
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004126 // For non-type template parameters, check the type of the parameter.
4127 if (NonTypeTemplateParmDecl *OldNTTP
4128 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4129 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004130
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004131 // If we are matching a template template argument to a template
4132 // template parameter and one of the non-type template parameter types
4133 // is dependent, then we must wait until template instantiation time
4134 // to actually compare the arguments.
4135 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4136 (OldNTTP->getType()->isDependentType() ||
4137 NewNTTP->getType()->isDependentType()))
4138 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004139
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004140 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4141 if (Complain) {
4142 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4143 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004144 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004145 diag::err_template_arg_template_params_mismatch);
4146 NextDiag = diag::note_template_nontype_parm_different_type;
4147 }
4148 S.Diag(NewNTTP->getLocation(), NextDiag)
4149 << NewNTTP->getType()
4150 << (Kind != Sema::TPL_TemplateMatch);
4151 S.Diag(OldNTTP->getLocation(),
4152 diag::note_template_nontype_parm_prev_declaration)
4153 << OldNTTP->getType();
4154 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004155
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004156 return false;
4157 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004158
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004159 return true;
4160 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004161
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004162 // For template template parameters, check the template parameter types.
4163 // The template parameter lists of template template
4164 // parameters must agree.
4165 if (TemplateTemplateParmDecl *OldTTP
4166 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004167 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004168 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4169 OldTTP->getTemplateParameters(),
4170 Complain,
4171 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004172 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004173 : Kind),
4174 TemplateArgLoc);
4175 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004176
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004177 return true;
4178}
Douglas Gregor02024a92010-03-28 02:42:43 +00004179
Douglas Gregora0347822011-01-13 00:08:50 +00004180/// \brief Diagnose a known arity mismatch when comparing template argument
4181/// lists.
4182static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004183void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004184 TemplateParameterList *New,
4185 TemplateParameterList *Old,
4186 Sema::TemplateParameterListEqualKind Kind,
4187 SourceLocation TemplateArgLoc) {
4188 unsigned NextDiag = diag::err_template_param_list_different_arity;
4189 if (TemplateArgLoc.isValid()) {
4190 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4191 NextDiag = diag::note_template_param_list_different_arity;
4192 }
4193 S.Diag(New->getTemplateLoc(), NextDiag)
4194 << (New->size() > Old->size())
4195 << (Kind != Sema::TPL_TemplateMatch)
4196 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4197 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4198 << (Kind != Sema::TPL_TemplateMatch)
4199 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4200}
4201
Douglas Gregorddc29e12009-02-06 22:42:48 +00004202/// \brief Determine whether the given template parameter lists are
4203/// equivalent.
4204///
Mike Stump1eb44332009-09-09 15:08:12 +00004205/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004206/// source code as part of a new template declaration.
4207///
4208/// \param Old The old template parameter list, typically found via
4209/// name lookup of the template declared with this template parameter
4210/// list.
4211///
4212/// \param Complain If true, this routine will produce a diagnostic if
4213/// the template parameter lists are not equivalent.
4214///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004215/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004216///
4217/// \param TemplateArgLoc If this source location is valid, then we
4218/// are actually checking the template parameter list of a template
4219/// argument (New) against the template parameter list of its
4220/// corresponding template template parameter (Old). We produce
4221/// slightly different diagnostics in this scenario.
4222///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004223/// \returns True if the template parameter lists are equal, false
4224/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004225bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004226Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4227 TemplateParameterList *Old,
4228 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004229 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004230 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004231 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4232 if (Complain)
4233 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4234 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004235
4236 return false;
4237 }
4238
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004239 // C++0x [temp.arg.template]p3:
4240 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004241 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004242 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004243 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004244 // template-parameter-list of P. [...]
4245 TemplateParameterList::iterator NewParm = New->begin();
4246 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004247 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004248 OldParmEnd = Old->end();
4249 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004250 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4251 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004252 if (NewParm == NewParmEnd) {
4253 if (Complain)
4254 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4255 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004256
Douglas Gregora0347822011-01-13 00:08:50 +00004257 return false;
4258 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004259
Douglas Gregora0347822011-01-13 00:08:50 +00004260 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4261 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004262 return false;
4263
Douglas Gregora0347822011-01-13 00:08:50 +00004264 ++NewParm;
4265 continue;
4266 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004267
Douglas Gregora0347822011-01-13 00:08:50 +00004268 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004269 // [...] When P's template- parameter-list contains a template parameter
4270 // pack (14.5.3), the template parameter pack will match zero or more
4271 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004272 // template-parameter-list of A with the same type and form as the
4273 // template parameter pack in P (ignoring whether those template
4274 // parameters are template parameter packs).
4275 for (; NewParm != NewParmEnd; ++NewParm) {
4276 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4277 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004278 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004279 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004280 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004281
Douglas Gregora0347822011-01-13 00:08:50 +00004282 // Make sure we exhausted all of the arguments.
4283 if (NewParm != NewParmEnd) {
4284 if (Complain)
4285 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4286 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004287
Douglas Gregora0347822011-01-13 00:08:50 +00004288 return false;
4289 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004290
Douglas Gregorddc29e12009-02-06 22:42:48 +00004291 return true;
4292}
4293
4294/// \brief Check whether a template can be declared within this scope.
4295///
4296/// If the template declaration is valid in this scope, returns
4297/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004298bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004299Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004300 // Find the nearest enclosing declaration scope.
4301 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4302 (S->getFlags() & Scope::TemplateParamScope) != 0)
4303 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004304
Douglas Gregorddc29e12009-02-06 22:42:48 +00004305 // C++ [temp]p2:
4306 // A template-declaration can appear only as a namespace scope or
4307 // class scope declaration.
4308 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004309 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4310 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004311 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004312 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004313
Eli Friedman1503f772009-07-31 01:43:05 +00004314 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004315 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004316
4317 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4318 return false;
4319
Mike Stump1eb44332009-09-09 15:08:12 +00004320 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004321 diag::err_template_outside_namespace_or_class_scope)
4322 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004323}
Douglas Gregorcc636682009-02-17 23:15:12 +00004324
Douglas Gregord5cb8762009-10-07 00:13:32 +00004325/// \brief Determine what kind of template specialization the given declaration
4326/// is.
4327static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4328 if (!D)
4329 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004330
Douglas Gregorf6b11852009-10-08 15:14:33 +00004331 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4332 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004333 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4334 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004335 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4336 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004337
Douglas Gregord5cb8762009-10-07 00:13:32 +00004338 return TSK_Undeclared;
4339}
4340
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004341/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004342/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004343///
Douglas Gregor9302da62009-10-14 23:50:59 +00004344/// This routine determines whether a template specialization can be declared
4345/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004346///
4347/// \param S the semantic analysis object for which this check is being
4348/// performed.
4349///
4350/// \param Specialized the entity being specialized or instantiated, which
4351/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004352/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004353/// member class).
4354///
4355/// \param PrevDecl the previous declaration of this entity, if any.
4356///
4357/// \param Loc the location of the explicit specialization or instantiation of
4358/// this entity.
4359///
4360/// \param IsPartialSpecialization whether this is a partial specialization of
4361/// a class template.
4362///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004363/// \returns true if there was an error that we cannot recover from, false
4364/// otherwise.
4365static bool CheckTemplateSpecializationScope(Sema &S,
4366 NamedDecl *Specialized,
4367 NamedDecl *PrevDecl,
4368 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004369 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004370 // Keep these "kind" numbers in sync with the %select statements in the
4371 // various diagnostics emitted by this routine.
4372 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004373 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004374 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004375 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004376 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004377 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004378 EntityKind = 3;
4379 else if (isa<VarDecl>(Specialized))
4380 EntityKind = 4;
4381 else if (isa<RecordDecl>(Specialized))
4382 EntityKind = 5;
4383 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004384 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4385 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004386 return true;
4387 }
4388
Douglas Gregor88b70942009-02-25 22:02:03 +00004389 // C++ [temp.expl.spec]p2:
4390 // An explicit specialization shall be declared in the namespace
4391 // of which the template is a member, or, for member templates, in
4392 // the namespace of which the enclosing class or enclosing class
4393 // template is a member. An explicit specialization of a member
4394 // function, member class or static data member of a class
4395 // template shall be declared in the namespace of which the class
4396 // template is a member. Such a declaration may also be a
4397 // definition. If the declaration is not a definition, the
4398 // specialization may be defined later in the name- space in which
4399 // the explicit specialization was declared, or in a namespace
4400 // that encloses the one in which the explicit specialization was
4401 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004402 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004403 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004404 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004405 return true;
4406 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004407
Douglas Gregor0a407472009-10-07 17:30:37 +00004408 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichete1e96a62011-05-14 19:17:07 +00004409 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4410 << Specialized;
4411 return true;
Douglas Gregor0a407472009-10-07 17:30:37 +00004412 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004413
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004414 // C++ [temp.class.spec]p6:
4415 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004416 // in any namespace scope in which its definition may be defined (14.5.1
4417 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004418 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004419 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004420 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004421 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004422 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004423 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4424 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004425 // C++ [temp.exp.spec]p2:
4426 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004427 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004428 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004429 // An explicit specialization of a member function, member class or
4430 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004431 // namespace of which the class template is a member.
4432 //
4433 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004434 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004435 // the specialized template.
4436 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4437 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004438 bool IsCPlusPlus0xExtension
4439 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004440 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004441 S.Diag(Loc, IsCPlusPlus0xExtension
4442 ? diag::ext_template_spec_decl_out_of_scope_global
4443 : diag::err_template_spec_decl_out_of_scope_global)
4444 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004445 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004446 S.Diag(Loc, IsCPlusPlus0xExtension
4447 ? diag::ext_template_spec_decl_out_of_scope
4448 : diag::err_template_spec_decl_out_of_scope)
4449 << EntityKind << Specialized
4450 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004451
Douglas Gregor9302da62009-10-14 23:50:59 +00004452 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4453 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004454 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004455 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004456
4457 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004458 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004459 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004460 // specializations of function templates, static data members, and member
4461 // functions, so we skip the check here for those kinds of entities.
4462 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004463 // Should we refactor that check, so that it occurs later?
4464 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004465 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4466 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004467 if (isa<TranslationUnitDecl>(SpecializedContext))
4468 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4469 << EntityKind << Specialized;
4470 else if (isa<NamespaceDecl>(SpecializedContext))
4471 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4472 << EntityKind << Specialized
4473 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004474
Douglas Gregor9302da62009-10-14 23:50:59 +00004475 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004477
Douglas Gregord5cb8762009-10-07 00:13:32 +00004478 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004479
Douglas Gregor88b70942009-02-25 22:02:03 +00004480 return false;
4481}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004482
Douglas Gregorbacb9492011-01-03 21:13:47 +00004483/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4484/// that checks non-type template partial specialization arguments.
4485static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4486 NonTypeTemplateParmDecl *Param,
4487 const TemplateArgument *Args,
4488 unsigned NumArgs) {
4489 for (unsigned I = 0; I != NumArgs; ++I) {
4490 if (Args[I].getKind() == TemplateArgument::Pack) {
4491 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004492 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004493 Args[I].pack_size()))
4494 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004495
Douglas Gregore94866f2009-06-12 21:21:02 +00004496 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004497 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004498
Douglas Gregorbacb9492011-01-03 21:13:47 +00004499 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004500 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004501 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004502 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004503
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004504 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004505 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4506 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004507
4508 // Strip off any implicit casts we added as part of type checking.
4509 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4510 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004511
Douglas Gregore94866f2009-06-12 21:21:02 +00004512 // C++ [temp.class.spec]p8:
4513 // A non-type argument is non-specialized if it is the name of a
4514 // non-type parameter. All other non-type arguments are
4515 // specialized.
4516 //
4517 // Below, we check the two conditions that only apply to
4518 // specialized non-type arguments, so skip any non-specialized
4519 // arguments.
4520 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004521 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004522 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004523
Douglas Gregore94866f2009-06-12 21:21:02 +00004524 // C++ [temp.class.spec]p9:
4525 // Within the argument list of a class template partial
4526 // specialization, the following restrictions apply:
4527 // -- A partially specialized non-type argument expression
4528 // shall not involve a template parameter of the partial
4529 // specialization except when the argument expression is a
4530 // simple identifier.
4531 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004532 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004533 diag::err_dependent_non_type_arg_in_partial_spec)
4534 << ArgExpr->getSourceRange();
4535 return true;
4536 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004537
Douglas Gregore94866f2009-06-12 21:21:02 +00004538 // -- The type of a template parameter corresponding to a
4539 // specialized non-type argument shall not be dependent on a
4540 // parameter of the specialization.
4541 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004542 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004543 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4544 << Param->getType()
4545 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004546 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004547 return true;
4548 }
4549 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004550
Douglas Gregorbacb9492011-01-03 21:13:47 +00004551 return false;
4552}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004553
Douglas Gregorbacb9492011-01-03 21:13:47 +00004554/// \brief Check the non-type template arguments of a class template
4555/// partial specialization according to C++ [temp.class.spec]p9.
4556///
4557/// \param TemplateParams the template parameters of the primary class
4558/// template.
4559///
4560/// \param TemplateArg the template arguments of the class template
4561/// partial specialization.
4562///
4563/// \returns true if there was an error, false otherwise.
4564static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4565 TemplateParameterList *TemplateParams,
4566 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4567 const TemplateArgument *ArgList = TemplateArgs.data();
4568
4569 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4570 NonTypeTemplateParmDecl *Param
4571 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4572 if (!Param)
4573 continue;
4574
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004575 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004576 &ArgList[I], 1))
4577 return true;
4578 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004579
4580 return false;
4581}
4582
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004583/// \brief Retrieve the previous declaration of the given declaration.
4584static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4585 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4586 return VD->getPreviousDeclaration();
4587 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4588 return FD->getPreviousDeclaration();
4589 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4590 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004591 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004592 return TD->getPreviousDeclaration();
4593 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4594 return FTD->getPreviousDeclaration();
4595 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4596 return CTD->getPreviousDeclaration();
4597 return 0;
4598}
4599
John McCalld226f652010-08-21 09:40:31 +00004600DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004601Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4602 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004603 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004604 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004605 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004606 SourceLocation TemplateNameLoc,
4607 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004608 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004609 SourceLocation RAngleLoc,
4610 AttributeList *Attr,
4611 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004612 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004613
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004614 // NOTE: KWLoc is the location of the tag keyword. This will instead
4615 // store the location of the outermost template keyword in the declaration.
4616 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4617 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4618
Douglas Gregorcc636682009-02-17 23:15:12 +00004619 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004620 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004621 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004622 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4623
4624 if (!ClassTemplate) {
4625 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004626 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004627 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4628 return true;
4629 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004630
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004631 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004632 bool isPartialSpecialization = false;
4633
Douglas Gregor88b70942009-02-25 22:02:03 +00004634 // Check the validity of the template headers that introduce this
4635 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004636 // FIXME: We probably shouldn't complain about these headers for
4637 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004638 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004639 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004640 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4641 TemplateNameLoc,
4642 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004643 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004644 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004645 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004646 isExplicitSpecialization,
4647 Invalid);
4648 if (Invalid)
4649 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004650
Douglas Gregor05396e22009-08-25 17:23:04 +00004651 if (TemplateParams && TemplateParams->size() > 0) {
4652 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004653
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004654 if (TUK == TUK_Friend) {
4655 Diag(KWLoc, diag::err_partial_specialization_friend)
4656 << SourceRange(LAngleLoc, RAngleLoc);
4657 return true;
4658 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004659
Douglas Gregor05396e22009-08-25 17:23:04 +00004660 // C++ [temp.class.spec]p10:
4661 // The template parameter list of a specialization shall not
4662 // contain default template argument values.
4663 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4664 Decl *Param = TemplateParams->getParam(I);
4665 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4666 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004667 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004668 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004669 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004670 }
4671 } else if (NonTypeTemplateParmDecl *NTTP
4672 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4673 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004674 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004675 diag::err_default_arg_in_partial_spec)
4676 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004677 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004678 }
4679 } else {
4680 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004681 if (TTP->hasDefaultArgument()) {
4682 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004683 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004684 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004685 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004686 }
4687 }
4688 }
Douglas Gregora735b202009-10-13 14:39:41 +00004689 } else if (TemplateParams) {
4690 if (TUK == TUK_Friend)
4691 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004692 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004693 SourceRange(TemplateParams->getTemplateLoc(),
4694 TemplateParams->getRAngleLoc()))
4695 << SourceRange(LAngleLoc, RAngleLoc);
4696 else
4697 isExplicitSpecialization = true;
4698 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004699 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004700 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004701 isExplicitSpecialization = true;
4702 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004703
Douglas Gregorcc636682009-02-17 23:15:12 +00004704 // Check that the specialization uses the same tag kind as the
4705 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004706 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4707 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004708 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004709 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004710 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004711 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004712 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004713 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004714 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004715 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004716 diag::note_previous_use);
4717 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4718 }
4719
Douglas Gregor40808ce2009-03-09 23:48:35 +00004720 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004721 TemplateArgumentListInfo TemplateArgs;
4722 TemplateArgs.setLAngleLoc(LAngleLoc);
4723 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004724 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004725
Douglas Gregor925910d2011-01-03 20:35:03 +00004726 // Check for unexpanded parameter packs in any of the template arguments.
4727 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004728 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004729 UPPC_PartialSpecialization))
4730 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004731
Douglas Gregorcc636682009-02-17 23:15:12 +00004732 // Check that the template argument list is well-formed for this
4733 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004734 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004735 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4736 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004737 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004738
Douglas Gregor910f8002010-11-07 23:05:16 +00004739 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004740 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004741
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004742 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004743 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004744 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004745 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004746 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004747 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004748 return true;
4749
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004750 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004751 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004752 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004753 TemplateArgs.size())) {
4754 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4755 << ClassTemplate->getDeclName();
4756 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004757 }
4758 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004759
Douglas Gregorcc636682009-02-17 23:15:12 +00004760 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004761 ClassTemplateSpecializationDecl *PrevDecl = 0;
4762
4763 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004764 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004765 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004766 = ClassTemplate->findPartialSpecialization(Converted.data(),
4767 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004768 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004769 else
4770 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004771 = ClassTemplate->findSpecialization(Converted.data(),
4772 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004773
4774 ClassTemplateSpecializationDecl *Specialization = 0;
4775
Douglas Gregor88b70942009-02-25 22:02:03 +00004776 // Check whether we can declare a class template specialization in
4777 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004778 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004779 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4780 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004781 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004782 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004783
Douglas Gregorb88e8882009-07-30 17:40:51 +00004784 // The canonical type
4785 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004786 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004787 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004788 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004789 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004790 // arguments was referenced but not declared, or we're only
4791 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004792 // declaration node as our own, updating its source location and
4793 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004794 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004795 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004796 if (TemplateParameterLists.size() > 0) {
4797 Specialization->setTemplateParameterListsInfo(Context,
4798 TemplateParameterLists.size(),
4799 (TemplateParameterList**) TemplateParameterLists.release());
4800 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004801 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004802 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004803 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004804 // Build the canonical type that describes the converted template
4805 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004806 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4807 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004808 Converted.data(),
4809 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004810
4811 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004812 ClassTemplate->getInjectedClassNameSpecialization())) {
4813 // C++ [temp.class.spec]p9b3:
4814 //
4815 // -- The argument list of the specialization shall not be identical
4816 // to the implicit argument list of the primary template.
4817 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4818 << (TUK == TUK_Definition)
4819 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4820 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4821 ClassTemplate->getIdentifier(),
4822 TemplateNameLoc,
4823 Attr,
4824 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004825 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004826 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004827 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004828 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004829
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004830 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004831 ClassTemplatePartialSpecializationDecl *PrevPartial
4832 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004833 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004834 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004835 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004836 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004837 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004838 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004839 TemplateParams,
4840 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004841 Converted.data(),
4842 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004843 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004844 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004845 PrevPartial,
4846 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004847 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004848 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004849 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004850 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004851 (TemplateParameterList**) TemplateParameterLists.release());
4852 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004853
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004854 if (!PrevPartial)
4855 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004856 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004857
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004858 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004859 // template specialization, make a note of that.
4860 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4861 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004862
Douglas Gregor031a5882009-06-13 00:26:55 +00004863 // Check that all of the template parameters of the class template
4864 // partial specialization are deducible from the template
4865 // arguments. If not, this class template partial specialization
4866 // will never be used.
4867 llvm::SmallVector<bool, 8> DeducibleParams;
4868 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004869 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004870 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004871 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004872 unsigned NumNonDeducible = 0;
4873 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4874 if (!DeducibleParams[I])
4875 ++NumNonDeducible;
4876
4877 if (NumNonDeducible) {
4878 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4879 << (NumNonDeducible > 1)
4880 << SourceRange(TemplateNameLoc, RAngleLoc);
4881 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4882 if (!DeducibleParams[I]) {
4883 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4884 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004885 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004886 diag::note_partial_spec_unused_parameter)
4887 << Param->getDeclName();
4888 else
Mike Stump1eb44332009-09-09 15:08:12 +00004889 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004890 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004891 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004892 }
4893 }
4894 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004895 } else {
4896 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004897 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004898 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004899 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004900 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004901 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004902 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004903 Converted.data(),
4904 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004905 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004906 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004907 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004908 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004909 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00004910 (TemplateParameterList**) TemplateParameterLists.release());
4911 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004912
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004913 if (!PrevDecl)
4914 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004915
4916 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004917 }
4918
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004919 // C++ [temp.expl.spec]p6:
4920 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004921 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004922 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004923 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004924 // use occurs; no diagnostic is required.
4925 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004926 bool Okay = false;
4927 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4928 // Is there any previous explicit specialization declaration?
4929 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4930 Okay = true;
4931 break;
4932 }
4933 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004934
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004935 if (!Okay) {
4936 SourceRange Range(TemplateNameLoc, RAngleLoc);
4937 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4938 << Context.getTypeDeclType(Specialization) << Range;
4939
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004940 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004941 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004942 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004943 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004944 return true;
4945 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004946 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004947
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004948 // If this is not a friend, note that this is an explicit specialization.
4949 if (TUK != TUK_Friend)
4950 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004951
4952 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004953 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004954 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004955 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004956 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004957 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004958 Diag(Def->getLocation(), diag::note_previous_definition);
4959 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004960 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004961 }
4962 }
4963
John McCall7f1b9872010-12-18 03:30:47 +00004964 if (Attr)
4965 ProcessDeclAttributeList(S, Specialization, Attr);
4966
Douglas Gregorfc705b82009-02-26 22:19:44 +00004967 // Build the fully-sugared type for this class template
4968 // specialization as the user wrote in the specialization
4969 // itself. This means that we'll pretty-print the type retrieved
4970 // from the specialization's declaration the way that the user
4971 // actually wrote the specialization, rather than formatting the
4972 // name based on the "canonical" representation used to store the
4973 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004974 TypeSourceInfo *WrittenTy
4975 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4976 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004977 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004978 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004979 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004980 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004981 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004982
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004983 // C++ [temp.expl.spec]p9:
4984 // A template explicit specialization is in the scope of the
4985 // namespace in which the template was defined.
4986 //
4987 // We actually implement this paragraph where we set the semantic
4988 // context (in the creation of the ClassTemplateSpecializationDecl),
4989 // but we also maintain the lexical context where the actual
4990 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004991 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004992
Douglas Gregorcc636682009-02-17 23:15:12 +00004993 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004994 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004995 Specialization->startDefinition();
4996
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004997 if (TUK == TUK_Friend) {
4998 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4999 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005000 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005001 /*FIXME:*/KWLoc);
5002 Friend->setAccess(AS_public);
5003 CurContext->addDecl(Friend);
5004 } else {
5005 // Add the specialization into its lexical context, so that it can
5006 // be seen when iterating through the list of declarations in that
5007 // context. However, specializations are not found by name lookup.
5008 CurContext->addDecl(Specialization);
5009 }
John McCalld226f652010-08-21 09:40:31 +00005010 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005011}
Douglas Gregord57959a2009-03-27 23:10:48 +00005012
John McCalld226f652010-08-21 09:40:31 +00005013Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005014 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005015 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00005016 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
5017}
5018
John McCalld226f652010-08-21 09:40:31 +00005019Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005020 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005021 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005022 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005023 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005024
Douglas Gregor52591bf2009-06-24 00:54:41 +00005025 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005026 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005027 }
Mike Stump1eb44332009-09-09 15:08:12 +00005028
Douglas Gregor52591bf2009-06-24 00:54:41 +00005029 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005030
John McCalld226f652010-08-21 09:40:31 +00005031 Decl *DP = HandleDeclarator(ParentScope, D,
5032 move(TemplateParameterLists),
5033 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00005034 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005035 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005036 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005037 FunctionTemplate->getTemplatedDecl());
5038 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5039 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5040 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005041}
5042
John McCall75042392010-02-11 01:33:53 +00005043/// \brief Strips various properties off an implicit instantiation
5044/// that has just been explicitly specialized.
5045static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005046 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005047
5048 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5049 FD->setInlineSpecified(false);
5050 }
5051}
5052
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005053/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005054/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005055/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005056/// new specialization/instantiation will have any effect.
5057///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005058/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005059/// instantiation.
5060///
5061/// \param NewTSK the kind of the new explicit specialization or instantiation.
5062///
5063/// \param PrevDecl the previous declaration of the entity.
5064///
5065/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5066///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005067/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005068/// declaration was instantiated (either implicitly or explicitly).
5069///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005070/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005071/// specialization or instantiation has no effect and should be ignored.
5072///
5073/// \returns true if there was an error that should prevent the introduction of
5074/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005075bool
5076Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5077 TemplateSpecializationKind NewTSK,
5078 NamedDecl *PrevDecl,
5079 TemplateSpecializationKind PrevTSK,
5080 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005081 bool &HasNoEffect) {
5082 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005083
Douglas Gregor454885e2009-10-15 15:54:05 +00005084 switch (NewTSK) {
5085 case TSK_Undeclared:
5086 case TSK_ImplicitInstantiation:
5087 assert(false && "Don't check implicit instantiations here");
5088 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005089
Douglas Gregor454885e2009-10-15 15:54:05 +00005090 case TSK_ExplicitSpecialization:
5091 switch (PrevTSK) {
5092 case TSK_Undeclared:
5093 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005094 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005095 // explicitly specialized or has merely been mentioned without any
5096 // instantiation.
5097 return false;
5098
5099 case TSK_ImplicitInstantiation:
5100 if (PrevPointOfInstantiation.isInvalid()) {
5101 // The declaration itself has not actually been instantiated, so it is
5102 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005103 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005104 return false;
5105 }
5106 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005107
Douglas Gregor454885e2009-10-15 15:54:05 +00005108 case TSK_ExplicitInstantiationDeclaration:
5109 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005110 assert((PrevTSK == TSK_ImplicitInstantiation ||
5111 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005112 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005113
Douglas Gregor454885e2009-10-15 15:54:05 +00005114 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005115 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005116 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005117 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005118 // implicit instantiation to take place, in every translation unit in
5119 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005120 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5121 // Is there any previous explicit specialization declaration?
5122 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5123 return false;
5124 }
5125
Douglas Gregor0d035142009-10-27 18:42:08 +00005126 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005127 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005128 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005129 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005130
Douglas Gregor454885e2009-10-15 15:54:05 +00005131 return true;
5132 }
5133 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005134
Douglas Gregor454885e2009-10-15 15:54:05 +00005135 case TSK_ExplicitInstantiationDeclaration:
5136 switch (PrevTSK) {
5137 case TSK_ExplicitInstantiationDeclaration:
5138 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005139 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005140 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005141
Douglas Gregor454885e2009-10-15 15:54:05 +00005142 case TSK_Undeclared:
5143 case TSK_ImplicitInstantiation:
5144 // We're explicitly instantiating something that may have already been
5145 // implicitly instantiated; that's fine.
5146 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005147
Douglas Gregor454885e2009-10-15 15:54:05 +00005148 case TSK_ExplicitSpecialization:
5149 // C++0x [temp.explicit]p4:
5150 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005151 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005152 // specialization for that template, the explicit instantiation has no
5153 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005154 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005155 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005156
Douglas Gregor454885e2009-10-15 15:54:05 +00005157 case TSK_ExplicitInstantiationDefinition:
5158 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005159 // If an entity is the subject of both an explicit instantiation
5160 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005161 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005162 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005163 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005164 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005165 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005166 assert(PrevPointOfInstantiation.isValid() &&
5167 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005168 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005169 return false;
5170 }
5171 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005172
Douglas Gregor454885e2009-10-15 15:54:05 +00005173 case TSK_ExplicitInstantiationDefinition:
5174 switch (PrevTSK) {
5175 case TSK_Undeclared:
5176 case TSK_ImplicitInstantiation:
5177 // We're explicitly instantiating something that may have already been
5178 // implicitly instantiated; that's fine.
5179 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005180
Douglas Gregor454885e2009-10-15 15:54:05 +00005181 case TSK_ExplicitSpecialization:
5182 // C++ DR 259, C++0x [temp.explicit]p4:
5183 // For a given set of template parameters, if an explicit
5184 // instantiation of a template appears after a declaration of
5185 // an explicit specialization for that template, the explicit
5186 // instantiation has no effect.
5187 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005188 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005189 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005190 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005191 if (!getLangOptions().CPlusPlus0x) {
5192 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005193 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005194 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005195 diag::note_previous_template_specialization);
5196 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005197 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005198 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005199
Douglas Gregor454885e2009-10-15 15:54:05 +00005200 case TSK_ExplicitInstantiationDeclaration:
5201 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005202 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005203 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005204
Douglas Gregor454885e2009-10-15 15:54:05 +00005205 case TSK_ExplicitInstantiationDefinition:
5206 // C++0x [temp.spec]p5:
5207 // For a given template and a given set of template-arguments,
5208 // - an explicit instantiation definition shall appear at most once
5209 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005210 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005211 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005212 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005213 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005214 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005215 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005216 }
5217 break;
5218 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005219
Douglas Gregor454885e2009-10-15 15:54:05 +00005220 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005221
Douglas Gregor454885e2009-10-15 15:54:05 +00005222 return false;
5223}
5224
John McCallaf2094e2010-04-08 09:05:18 +00005225/// \brief Perform semantic analysis for the given dependent function
5226/// template specialization. The only possible way to get a dependent
5227/// function template specialization is with a friend declaration,
5228/// like so:
5229///
5230/// template <class T> void foo(T);
5231/// template <class T> class A {
5232/// friend void foo<>(T);
5233/// };
5234///
5235/// There really isn't any useful analysis we can do here, so we
5236/// just store the information.
5237bool
5238Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5239 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5240 LookupResult &Previous) {
5241 // Remove anything from Previous that isn't a function template in
5242 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005243 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005244 LookupResult::Filter F = Previous.makeFilter();
5245 while (F.hasNext()) {
5246 NamedDecl *D = F.next()->getUnderlyingDecl();
5247 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005248 !FDLookupContext->InEnclosingNamespaceSetOf(
5249 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005250 F.erase();
5251 }
5252 F.done();
5253
5254 // Should this be diagnosed here?
5255 if (Previous.empty()) return true;
5256
5257 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5258 ExplicitTemplateArgs);
5259 return false;
5260}
5261
Abramo Bagnarae03db982010-05-20 15:32:11 +00005262/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005263/// specialization.
5264///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005265/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005266/// explicit function template specialization. On successful completion,
5267/// the function declaration \p FD will become a function template
5268/// specialization.
5269///
5270/// \param FD the function declaration, which will be updated to become a
5271/// function template specialization.
5272///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005273/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5274/// if any. Note that this may be valid info even when 0 arguments are
5275/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5276/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005277///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005278/// \param PrevDecl the set of declarations that may be specialized by
5279/// this function specialization.
5280bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005281Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005282 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005283 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005284 // The set of function template specializations that could match this
5285 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005286 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005287
Sebastian Redl7a126a42010-08-31 00:36:30 +00005288 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005289 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5290 I != E; ++I) {
5291 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5292 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005293 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005294 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005295 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5296 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005297 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005298
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005299 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005300 // A trailing template-argument can be left unspecified in the
5301 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005302 // provided it can be deduced from the function argument type.
5303 // Perform template argument deduction to determine whether we may be
5304 // specializing this template.
5305 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005306 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005307 FunctionDecl *Specialization = 0;
5308 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005309 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005310 FD->getType(),
5311 Specialization,
5312 Info)) {
5313 // FIXME: Template argument deduction failed; record why it failed, so
5314 // that we can provide nifty diagnostics.
5315 (void)TDK;
5316 continue;
5317 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005318
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005319 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005320 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005321 }
5322 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005323
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005324 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005325 UnresolvedSetIterator Result
5326 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005327 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005328 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005329 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005330 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005331 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005332 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005333 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005334 return true;
John McCallc373d482010-01-27 01:50:18 +00005335
5336 // Ignore access information; it doesn't figure into redeclaration checking.
5337 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005338
5339 FunctionTemplateSpecializationInfo *SpecInfo
5340 = Specialization->getTemplateSpecializationInfo();
5341 assert(SpecInfo && "Function template specialization info missing?");
5342 {
5343 // Note: do not overwrite location info if previous template
5344 // specialization kind was explicit.
5345 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5346 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5347 Specialization->setLocation(FD->getLocation());
5348 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005349
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005350 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005351 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005352
5353 // If this is a friend declaration, then we're not really declaring
5354 // an explicit specialization.
5355 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005356
Douglas Gregord5cb8762009-10-07 00:13:32 +00005357 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005358 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005359 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005360 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005361 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005362 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005363 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005364
5365 // C++ [temp.expl.spec]p6:
5366 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005367 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005368 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005369 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005370 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005371 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005372 if (!isFriend &&
5373 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005374 TSK_ExplicitSpecialization,
5375 Specialization,
5376 SpecInfo->getTemplateSpecializationKind(),
5377 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005378 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005379 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005380
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005381 // Mark the prior declaration as an explicit specialization, so that later
5382 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005383 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005384 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005385 MarkUnusedFileScopedDecl(Specialization);
5386 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005387
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005388 // Turn the given function declaration into a function template
5389 // specialization, with the template arguments from the previous
5390 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005391 // Take copies of (semantic and syntactic) template argument lists.
5392 const TemplateArgumentList* TemplArgs = new (Context)
5393 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5394 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5395 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005396 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005397 TemplArgs, /*InsertPos=*/0,
5398 SpecInfo->getTemplateSpecializationKind(),
5399 TemplArgsAsWritten);
5400
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005401 // The "previous declaration" for this function template specialization is
5402 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005403 Previous.clear();
5404 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005405 return false;
5406}
5407
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005408/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005409/// specialization.
5410///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005411/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005412/// explicit member function specialization. On successful completion,
5413/// the function declaration \p FD will become a member function
5414/// specialization.
5415///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005416/// \param Member the member declaration, which will be updated to become a
5417/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005418///
John McCall68263142009-11-18 22:49:29 +00005419/// \param Previous the set of declarations, one of which may be specialized
5420/// by this function specialization; the set will be modified to contain the
5421/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005422bool
John McCall68263142009-11-18 22:49:29 +00005423Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005424 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005425
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005426 // Try to find the member we are instantiating.
5427 NamedDecl *Instantiation = 0;
5428 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005429 MemberSpecializationInfo *MSInfo = 0;
5430
John McCall68263142009-11-18 22:49:29 +00005431 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005432 // Nowhere to look anyway.
5433 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005434 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5435 I != E; ++I) {
5436 NamedDecl *D = (*I)->getUnderlyingDecl();
5437 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005438 if (Context.hasSameType(Function->getType(), Method->getType())) {
5439 Instantiation = Method;
5440 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005441 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005442 break;
5443 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005444 }
5445 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005446 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005447 VarDecl *PrevVar;
5448 if (Previous.isSingleResult() &&
5449 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005450 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005451 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005452 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005453 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005454 }
5455 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005456 CXXRecordDecl *PrevRecord;
5457 if (Previous.isSingleResult() &&
5458 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5459 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005460 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005461 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005462 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005463 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005464
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005465 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005466 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005467 // specializations are always out-of-line, the caller will complain about
5468 // this mismatch later.
5469 return false;
5470 }
John McCall77e8b112010-04-13 20:37:33 +00005471
5472 // If this is a friend, just bail out here before we start turning
5473 // things into explicit specializations.
5474 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5475 // Preserve instantiation information.
5476 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5477 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5478 cast<CXXMethodDecl>(InstantiatedFrom),
5479 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5480 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5481 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5482 cast<CXXRecordDecl>(InstantiatedFrom),
5483 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5484 }
5485
5486 Previous.clear();
5487 Previous.addDecl(Instantiation);
5488 return false;
5489 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005490
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005491 // Make sure that this is a specialization of a member.
5492 if (!InstantiatedFrom) {
5493 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5494 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005495 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5496 return true;
5497 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005498
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005499 // C++ [temp.expl.spec]p6:
5500 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005501 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005502 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005503 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005504 // use occurs; no diagnostic is required.
5505 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005506
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005507 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005508 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5509 TSK_ExplicitSpecialization,
5510 Instantiation,
5511 MSInfo->getTemplateSpecializationKind(),
5512 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005513 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005514 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005515
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005516 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005517 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005518 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005519 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005520 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005521 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005522
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005523 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005524 // the original declaration to note that it is an explicit specialization
5525 // (if it was previously an implicit instantiation). This latter step
5526 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005527 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005528 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5529 if (InstantiationFunction->getTemplateSpecializationKind() ==
5530 TSK_ImplicitInstantiation) {
5531 InstantiationFunction->setTemplateSpecializationKind(
5532 TSK_ExplicitSpecialization);
5533 InstantiationFunction->setLocation(Member->getLocation());
5534 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005535
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005536 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5537 cast<CXXMethodDecl>(InstantiatedFrom),
5538 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005539 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005540 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005541 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5542 if (InstantiationVar->getTemplateSpecializationKind() ==
5543 TSK_ImplicitInstantiation) {
5544 InstantiationVar->setTemplateSpecializationKind(
5545 TSK_ExplicitSpecialization);
5546 InstantiationVar->setLocation(Member->getLocation());
5547 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005548
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005549 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5550 cast<VarDecl>(InstantiatedFrom),
5551 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005552 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005553 } else {
5554 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005555 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5556 if (InstantiationClass->getTemplateSpecializationKind() ==
5557 TSK_ImplicitInstantiation) {
5558 InstantiationClass->setTemplateSpecializationKind(
5559 TSK_ExplicitSpecialization);
5560 InstantiationClass->setLocation(Member->getLocation());
5561 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005562
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005563 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005564 cast<CXXRecordDecl>(InstantiatedFrom),
5565 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005567
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005568 // Save the caller the trouble of having to figure out which declaration
5569 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005570 Previous.clear();
5571 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005572 return false;
5573}
5574
Douglas Gregor558c0322009-10-14 23:41:34 +00005575/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005576///
5577/// \returns true if a serious error occurs, false otherwise.
5578static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005579 SourceLocation InstLoc,
5580 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005581 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5582 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005583
Douglas Gregor669eed82010-07-13 00:10:04 +00005584 if (CurContext->isRecord()) {
5585 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5586 << D;
5587 return true;
5588 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005589
Douglas Gregor558c0322009-10-14 23:41:34 +00005590 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005591 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005592 // template.
5593 //
5594 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005595 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005596 !CurContext->Encloses(OrigContext)) {
5597 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005598 S.Diag(InstLoc,
5599 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005600 diag::err_explicit_instantiation_out_of_scope
5601 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005602 << D << NS;
5603 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005604 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005605 S.getLangOptions().CPlusPlus0x?
5606 diag::err_explicit_instantiation_must_be_global
5607 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005608 << D;
5609 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005610 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005611 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005612
Douglas Gregor558c0322009-10-14 23:41:34 +00005613 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005614 // If the name declared in the explicit instantiation is an unqualified
5615 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005616 // its template is declared or, if that namespace is inline (7.3.1), any
5617 // namespace from its enclosing namespace set.
5618 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005619 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005620
5621 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005622 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005623
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005624 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005625 S.getLangOptions().CPlusPlus0x?
5626 diag::err_explicit_instantiation_unqualified_wrong_namespace
5627 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005628 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005629 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005630 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005631}
5632
5633/// \brief Determine whether the given scope specifier has a template-id in it.
5634static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5635 if (!SS.isSet())
5636 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005637
Douglas Gregor558c0322009-10-14 23:41:34 +00005638 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005639 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005640 // or a static data member of a class template specialization, the name of
5641 // the class template specialization in the qualified-id for the member
5642 // name shall be a simple-template-id.
5643 //
5644 // C++98 has the same restriction, just worded differently.
5645 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5646 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005647 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005648 if (isa<TemplateSpecializationType>(T))
5649 return true;
5650
5651 return false;
5652}
5653
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005654// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005655DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005656Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005657 SourceLocation ExternLoc,
5658 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005659 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005660 SourceLocation KWLoc,
5661 const CXXScopeSpec &SS,
5662 TemplateTy TemplateD,
5663 SourceLocation TemplateNameLoc,
5664 SourceLocation LAngleLoc,
5665 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005666 SourceLocation RAngleLoc,
5667 AttributeList *Attr) {
5668 // Find the class template we're specializing
5669 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005670 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005671 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5672
5673 // Check that the specialization uses the same tag kind as the
5674 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005675 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5676 assert(Kind != TTK_Enum &&
5677 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005678 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005679 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005680 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005681 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005682 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005683 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005684 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005685 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005686 diag::note_previous_use);
5687 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5688 }
5689
Douglas Gregor558c0322009-10-14 23:41:34 +00005690 // C++0x [temp.explicit]p2:
5691 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005692 // definition and an explicit instantiation declaration. An explicit
5693 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005694 TemplateSpecializationKind TSK
5695 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5696 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005697
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005698 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005699 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005700 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005701
5702 // Check that the template argument list is well-formed for this
5703 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005704 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005705 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5706 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005707 return true;
5708
Douglas Gregor910f8002010-11-07 23:05:16 +00005709 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005710 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005711
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005712 // Find the class template specialization declaration that
5713 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005714 void *InsertPos = 0;
5715 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005716 = ClassTemplate->findSpecialization(Converted.data(),
5717 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005718
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005719 TemplateSpecializationKind PrevDecl_TSK
5720 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5721
Douglas Gregord5cb8762009-10-07 00:13:32 +00005722 // C++0x [temp.explicit]p2:
5723 // [...] An explicit instantiation shall appear in an enclosing
5724 // namespace of its template. [...]
5725 //
5726 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005727 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5728 SS.isSet()))
5729 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005730
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005731 ClassTemplateSpecializationDecl *Specialization = 0;
5732
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005733 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005734 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005735 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005736 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005737 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005738 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005739 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005740
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005741 // Even though HasNoEffect == true means that this explicit instantiation
5742 // has no effect on semantics, we go on to put its syntax in the AST.
5743
5744 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5745 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005746 // Since the only prior class template specialization with these
5747 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005748 // declaration node as our own, updating the source location
5749 // for the template name to reflect our new declaration.
5750 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005751 Specialization = PrevDecl;
5752 Specialization->setLocation(TemplateNameLoc);
5753 PrevDecl = 0;
5754 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005755 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005756
Douglas Gregor52604ab2009-09-11 21:19:12 +00005757 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005758 // Create a new class template specialization declaration node for
5759 // this explicit specialization.
5760 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005761 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005762 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005763 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005764 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005765 Converted.data(),
5766 Converted.size(),
5767 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005768 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005769
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005770 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005771 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005772 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005773 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005774 }
5775
5776 // Build the fully-sugared type for this explicit instantiation as
5777 // the user wrote in the explicit instantiation itself. This means
5778 // that we'll pretty-print the type retrieved from the
5779 // specialization's declaration the way that the user actually wrote
5780 // the explicit instantiation, rather than formatting the name based
5781 // on the "canonical" representation used to store the template
5782 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005783 TypeSourceInfo *WrittenTy
5784 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5785 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005786 Context.getTypeDeclType(Specialization));
5787 Specialization->setTypeAsWritten(WrittenTy);
5788 TemplateArgsIn.release();
5789
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005790 // Set source locations for keywords.
5791 Specialization->setExternLoc(ExternLoc);
5792 Specialization->setTemplateKeywordLoc(TemplateLoc);
5793
5794 // Add the explicit instantiation into its lexical context. However,
5795 // since explicit instantiations are never found by name lookup, we
5796 // just put it into the declaration context directly.
5797 Specialization->setLexicalDeclContext(CurContext);
5798 CurContext->addDecl(Specialization);
5799
5800 // Syntax is now OK, so return if it has no other effect on semantics.
5801 if (HasNoEffect) {
5802 // Set the template specialization kind.
5803 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005804 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005805 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005806
5807 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005808 // A definition of a class template or class member template
5809 // shall be in scope at the point of the explicit instantiation of
5810 // the class template or class member template.
5811 //
5812 // This check comes when we actually try to perform the
5813 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005814 ClassTemplateSpecializationDecl *Def
5815 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005816 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005817 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005818 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005819 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005820 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005821 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5822 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005823
Douglas Gregor0d035142009-10-27 18:42:08 +00005824 // Instantiate the members of this class template specialization.
5825 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005826 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005827 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005828 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5829
5830 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5831 // TSK_ExplicitInstantiationDefinition
5832 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5833 TSK == TSK_ExplicitInstantiationDefinition)
5834 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005835
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005836 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005837 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005838
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005839 // Set the template specialization kind.
5840 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005841 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005842}
5843
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005844// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005845DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005846Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005847 SourceLocation ExternLoc,
5848 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005849 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005850 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005851 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005852 IdentifierInfo *Name,
5853 SourceLocation NameLoc,
5854 AttributeList *Attr) {
5855
Douglas Gregor402abb52009-05-28 23:31:59 +00005856 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005857 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005858 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005859 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5860 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005861 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005862 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005863 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5864
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005865 if (!TagD)
5866 return true;
5867
John McCalld226f652010-08-21 09:40:31 +00005868 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005869 if (Tag->isEnum()) {
5870 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5871 << Context.getTypeDeclType(Tag);
5872 return true;
5873 }
5874
Douglas Gregord0c87372009-05-27 17:30:49 +00005875 if (Tag->isInvalidDecl())
5876 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005877
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005878 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5879 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5880 if (!Pattern) {
5881 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5882 << Context.getTypeDeclType(Record);
5883 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5884 return true;
5885 }
5886
Douglas Gregor558c0322009-10-14 23:41:34 +00005887 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005888 // If the explicit instantiation is for a class or member class, the
5889 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005890 // simple-template-id.
5891 //
5892 // C++98 has the same restriction, just worded differently.
5893 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005894 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005895 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005896
Douglas Gregor558c0322009-10-14 23:41:34 +00005897 // C++0x [temp.explicit]p2:
5898 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005899 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005900 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005901 TemplateSpecializationKind TSK
5902 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5903 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005904
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005905 // C++0x [temp.explicit]p2:
5906 // [...] An explicit instantiation shall appear in an enclosing
5907 // namespace of its template. [...]
5908 //
5909 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005910 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005911
Douglas Gregor454885e2009-10-15 15:54:05 +00005912 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005913 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005914 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005915 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005916 PrevDecl = Record;
5917 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005918 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005919 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005920 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005921 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005922 PrevDecl,
5923 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005924 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005925 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005926 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005927 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005928 return TagD;
5929 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005930
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005931 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005932 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005933 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005934 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005935 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005936 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005937 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005938 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005939 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005940 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5941 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005942 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5943 << Pattern;
5944 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005945 } else {
5946 if (InstantiateClass(NameLoc, Record, Def,
5947 getTemplateInstantiationArgs(Record),
5948 TSK))
5949 return true;
5950
Douglas Gregor952b0172010-02-11 01:04:33 +00005951 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005952 if (!RecordDef)
5953 return true;
5954 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005955 }
5956
Douglas Gregor0d035142009-10-27 18:42:08 +00005957 // Instantiate all of the members of the class.
5958 InstantiateClassMembers(NameLoc, RecordDef,
5959 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005960
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005961 if (TSK == TSK_ExplicitInstantiationDefinition)
5962 MarkVTableUsed(NameLoc, RecordDef, true);
5963
Mike Stump390b4cc2009-05-16 07:39:55 +00005964 // FIXME: We don't have any representation for explicit instantiations of
5965 // member classes. Such a representation is not needed for compilation, but it
5966 // should be available for clients that want to see all of the declarations in
5967 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005968 return TagD;
5969}
5970
John McCallf312b1e2010-08-26 23:41:50 +00005971DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5972 SourceLocation ExternLoc,
5973 SourceLocation TemplateLoc,
5974 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005975 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005976 // TODO: check if/when DNInfo should replace Name.
5977 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5978 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005979 if (!Name) {
5980 if (!D.isInvalidType())
5981 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5982 diag::err_explicit_instantiation_requires_name)
5983 << D.getDeclSpec().getSourceRange()
5984 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005985
Douglas Gregord5a423b2009-09-25 18:43:00 +00005986 return true;
5987 }
5988
5989 // The scope passed in may not be a decl scope. Zip up the scope tree until
5990 // we find one that is.
5991 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5992 (S->getFlags() & Scope::TemplateParamScope) != 0)
5993 S = S->getParent();
5994
5995 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005996 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5997 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005998 if (R.isNull())
5999 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006000
Douglas Gregord5a423b2009-09-25 18:43:00 +00006001 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
6002 // Cannot explicitly instantiate a typedef.
6003 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6004 << Name;
6005 return true;
6006 }
6007
Douglas Gregor663b5a02009-10-14 20:14:33 +00006008 // C++0x [temp.explicit]p1:
6009 // [...] An explicit instantiation of a function template shall not use the
6010 // inline or constexpr specifiers.
6011 // Presumably, this also applies to member functions of class templates as
6012 // well.
6013 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006014 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00006015 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00006016 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006017
Douglas Gregor663b5a02009-10-14 20:14:33 +00006018 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006019
Douglas Gregor558c0322009-10-14 23:41:34 +00006020 // C++0x [temp.explicit]p2:
6021 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006022 // definition and an explicit instantiation declaration. An explicit
6023 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006024 TemplateSpecializationKind TSK
6025 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6026 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006027
Abramo Bagnara25777432010-08-11 22:01:17 +00006028 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006029 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006030
6031 if (!R->isFunctionType()) {
6032 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006033 // A [...] static data member of a class template can be explicitly
6034 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006035 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006036 if (Previous.isAmbiguous())
6037 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006038
John McCall1bcee0a2009-12-02 08:25:40 +00006039 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006040 if (!Prev || !Prev->isStaticDataMember()) {
6041 // We expect to see a data data member here.
6042 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6043 << Name;
6044 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6045 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006046 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006047 return true;
6048 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006049
Douglas Gregord5a423b2009-09-25 18:43:00 +00006050 if (!Prev->getInstantiatedFromStaticDataMember()) {
6051 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006052 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006053 diag::err_explicit_instantiation_data_member_not_instantiated)
6054 << Prev;
6055 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6056 // FIXME: Can we provide a note showing where this was declared?
6057 return true;
6058 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006059
Douglas Gregor558c0322009-10-14 23:41:34 +00006060 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006061 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006062 // or a static data member of a class template specialization, the name of
6063 // the class template specialization in the qualified-id for the member
6064 // name shall be a simple-template-id.
6065 //
6066 // C++98 has the same restriction, just worded differently.
6067 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006068 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006069 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006070 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006071
Douglas Gregor558c0322009-10-14 23:41:34 +00006072 // Check the scope of this explicit instantiation.
6073 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006074
Douglas Gregor454885e2009-10-15 15:54:05 +00006075 // Verify that it is okay to explicitly instantiate here.
6076 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6077 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006078 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006079 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006080 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006081 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006082 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006083 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006084 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006085 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006086
Douglas Gregord5a423b2009-09-25 18:43:00 +00006087 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006088 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006089 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006090 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006091
Douglas Gregord5a423b2009-09-25 18:43:00 +00006092 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006093 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006094 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006095
6096 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006097 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006098 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006099 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006100 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6101 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006102 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6103 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006104 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6105 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006106 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006107 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006108 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006109 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006110 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006111
Douglas Gregord5a423b2009-09-25 18:43:00 +00006112 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006113 // A [...] function [...] can be explicitly instantiated from its template.
6114 // A member function [...] of a class template can be explicitly
6115 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006116 // template.
John McCallc373d482010-01-27 01:50:18 +00006117 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006118 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6119 P != PEnd; ++P) {
6120 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006121 if (!HasExplicitTemplateArgs) {
6122 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6123 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6124 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006125
John McCallc373d482010-01-27 01:50:18 +00006126 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006127 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6128 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006129 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006130 }
6131 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006132
Douglas Gregord5a423b2009-09-25 18:43:00 +00006133 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6134 if (!FunTmpl)
6135 continue;
6136
John McCall5769d612010-02-08 23:07:23 +00006137 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006138 FunctionDecl *Specialization = 0;
6139 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006140 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006141 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006142 R, Specialization, Info)) {
6143 // FIXME: Keep track of almost-matches?
6144 (void)TDK;
6145 continue;
6146 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006147
John McCallc373d482010-01-27 01:50:18 +00006148 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006149 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006150
Douglas Gregord5a423b2009-09-25 18:43:00 +00006151 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006152 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006153 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006154 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006155 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6156 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6157 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006158
John McCallc373d482010-01-27 01:50:18 +00006159 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006160 return true;
John McCallc373d482010-01-27 01:50:18 +00006161
6162 // Ignore access control bits, we don't need them for redeclaration checking.
6163 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006164
Douglas Gregor0a897e32009-10-15 17:21:20 +00006165 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006166 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006167 diag::err_explicit_instantiation_member_function_not_instantiated)
6168 << Specialization
6169 << (Specialization->getTemplateSpecializationKind() ==
6170 TSK_ExplicitSpecialization);
6171 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6172 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006173 }
6174
Douglas Gregor0a897e32009-10-15 17:21:20 +00006175 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006176 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6177 PrevDecl = Specialization;
6178
Douglas Gregor0a897e32009-10-15 17:21:20 +00006179 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006180 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006181 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006182 PrevDecl,
6183 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006184 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006185 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006186 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006187
Douglas Gregor0a897e32009-10-15 17:21:20 +00006188 // FIXME: We may still want to build some representation of this
6189 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006190 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006191 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006192 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006193
6194 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006195
Douglas Gregor0a897e32009-10-15 17:21:20 +00006196 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006197 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006198
Douglas Gregor558c0322009-10-14 23:41:34 +00006199 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006200 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006201 // or a static data member of a class template specialization, the name of
6202 // the class template specialization in the qualified-id for the member
6203 // name shall be a simple-template-id.
6204 //
6205 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006206 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006207 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006208 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006209 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006210 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006211 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006212 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006213
Douglas Gregor558c0322009-10-14 23:41:34 +00006214 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006215 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006216 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006217 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006218 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006219
Douglas Gregord5a423b2009-09-25 18:43:00 +00006220 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006221 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006222}
6223
John McCallf312b1e2010-08-26 23:41:50 +00006224TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006225Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6226 const CXXScopeSpec &SS, IdentifierInfo *Name,
6227 SourceLocation TagLoc, SourceLocation NameLoc) {
6228 // This has to hold, because SS is expected to be defined.
6229 assert(Name && "Expected a name in a dependent tag");
6230
6231 NestedNameSpecifier *NNS
6232 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6233 if (!NNS)
6234 return true;
6235
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006236 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006237
Douglas Gregor48c89f42010-04-24 16:38:41 +00006238 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6239 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006240 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006241 return true;
6242 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006243
Douglas Gregor059101f2011-03-02 00:47:37 +00006244 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006245 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006246 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6247
6248 // Create type-source location information for this type.
6249 TypeLocBuilder TLB;
6250 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6251 TL.setKeywordLoc(TagLoc);
6252 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6253 TL.setNameLoc(NameLoc);
6254 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006255}
6256
John McCallf312b1e2010-08-26 23:41:50 +00006257TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006258Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6259 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006260 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006261 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006262 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006263
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006264 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6265 !getLangOptions().CPlusPlus0x)
6266 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006267 << FixItHint::CreateRemoval(TypenameLoc);
6268
Douglas Gregor2494dd02011-03-01 01:34:45 +00006269 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006270 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6271 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006272 if (T.isNull())
6273 return true;
John McCall63b43852010-04-29 23:50:39 +00006274
6275 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6276 if (isa<DependentNameType>(T)) {
6277 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006278 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006279 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006280 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006281 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006282 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006283 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006284 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006285 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006286 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006287
John McCallb3d87482010-08-24 05:47:05 +00006288 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006289}
6290
John McCallf312b1e2010-08-26 23:41:50 +00006291TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006292Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6293 const CXXScopeSpec &SS,
6294 SourceLocation TemplateLoc,
6295 TemplateTy TemplateIn,
6296 SourceLocation TemplateNameLoc,
6297 SourceLocation LAngleLoc,
6298 ASTTemplateArgsPtr TemplateArgsIn,
6299 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006300 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6301 !getLangOptions().CPlusPlus0x)
6302 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006303 << FixItHint::CreateRemoval(TypenameLoc);
6304
6305 // Translate the parser's template argument list in our AST format.
6306 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6307 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6308
6309 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006310 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6311 // Construct a dependent template specialization type.
6312 assert(DTN && "dependent template has non-dependent name?");
6313 assert(DTN->getQualifier()
6314 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6315 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6316 DTN->getQualifier(),
6317 DTN->getIdentifier(),
6318 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006319
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006320 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006321 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006322 DependentTemplateSpecializationTypeLoc SpecTL
6323 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006324 SpecTL.setLAngleLoc(LAngleLoc);
6325 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006326 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006327 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006328 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006329 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6330 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006331 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006332 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006333
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006334 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6335 if (T.isNull())
6336 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006337
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006338 // Provide source-location information for the template specialization
6339 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006340 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006341 TemplateSpecializationTypeLoc SpecTL
6342 = Builder.push<TemplateSpecializationTypeLoc>(T);
6343
6344 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006345 SpecTL.setLAngleLoc(LAngleLoc);
6346 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006347 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006348 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6349 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6350
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006351 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6352 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6353 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006354 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6355
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006356 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6357 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006358}
6359
Douglas Gregora02411e2011-02-27 22:46:49 +00006360
Douglas Gregord57959a2009-03-27 23:10:48 +00006361/// \brief Build the type that describes a C++ typename specifier,
6362/// e.g., "typename T::type".
6363QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006364Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6365 SourceLocation KeywordLoc,
6366 NestedNameSpecifierLoc QualifierLoc,
6367 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006368 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006369 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006370 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006371
John McCall77bb1aa2010-05-01 00:40:08 +00006372 DeclContext *Ctx = computeDeclContext(SS);
6373 if (!Ctx) {
6374 // If the nested-name-specifier is dependent and couldn't be
6375 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006376 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6377 return Context.getDependentNameType(Keyword,
6378 QualifierLoc.getNestedNameSpecifier(),
6379 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006380 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006381
John McCall77bb1aa2010-05-01 00:40:08 +00006382 // If the nested-name-specifier refers to the current instantiation,
6383 // the "typename" keyword itself is superfluous. In C++03, the
6384 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6385 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006386 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006387
John McCall77bb1aa2010-05-01 00:40:08 +00006388 if (RequireCompleteDeclContext(SS, Ctx))
6389 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006390
6391 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006392 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006393 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006394 unsigned DiagID = 0;
6395 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006396 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006397 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006398 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006399 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006400
6401 case LookupResult::FoundUnresolvedValue: {
6402 // We found a using declaration that is a value. Most likely, the using
6403 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006404 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006405 IILoc);
6406 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6407 << Name << Ctx << FullRange;
6408 if (UnresolvedUsingValueDecl *Using
6409 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006410 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006411 Diag(Loc, diag::note_using_value_decl_missing_typename)
6412 << FixItHint::CreateInsertion(Loc, "typename ");
6413 }
6414 }
6415 // Fall through to create a dependent typename type, from which we can recover
6416 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006417
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006418 case LookupResult::NotFoundInCurrentInstantiation:
6419 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006420 return Context.getDependentNameType(Keyword,
6421 QualifierLoc.getNestedNameSpecifier(),
6422 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006423
6424 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006425 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006426 // We found a type. Build an ElaboratedType, since the
6427 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006428 return Context.getElaboratedType(ETK_Typename,
6429 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006430 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006431 }
6432
6433 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006434 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006435 break;
6436
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006437
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006438 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006439 return QualType();
6440
Douglas Gregord57959a2009-03-27 23:10:48 +00006441 case LookupResult::FoundOverloaded:
6442 DiagID = diag::err_typename_nested_not_type;
6443 Referenced = *Result.begin();
6444 break;
6445
John McCall6e247262009-10-10 05:48:19 +00006446 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006447 return QualType();
6448 }
6449
6450 // If we get here, it's because name lookup did not find a
6451 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006452 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006453 IILoc);
6454 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006455 if (Referenced)
6456 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6457 << Name;
6458 return QualType();
6459}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006460
6461namespace {
6462 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006463 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006464 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006465 SourceLocation Loc;
6466 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006467
Douglas Gregor4a959d82009-08-06 16:20:37 +00006468 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006469 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006470
Mike Stump1eb44332009-09-09 15:08:12 +00006471 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006472 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006473 DeclarationName Entity)
6474 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006475 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006476
6477 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006478 /// transformed.
6479 ///
6480 /// For the purposes of type reconstruction, a type has already been
6481 /// transformed if it is NULL or if it is not dependent.
6482 bool AlreadyTransformed(QualType T) {
6483 return T.isNull() || !T->isDependentType();
6484 }
Mike Stump1eb44332009-09-09 15:08:12 +00006485
6486 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006487 /// rebuilt.
6488 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006489
Douglas Gregor4a959d82009-08-06 16:20:37 +00006490 /// \brief Returns the name of the entity whose type is being rebuilt.
6491 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006492
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006493 /// \brief Sets the "base" location and entity when that
6494 /// information is known based on another transformation.
6495 void setBase(SourceLocation Loc, DeclarationName Entity) {
6496 this->Loc = Loc;
6497 this->Entity = Entity;
6498 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006499 };
6500}
6501
Douglas Gregor4a959d82009-08-06 16:20:37 +00006502/// \brief Rebuilds a type within the context of the current instantiation.
6503///
Mike Stump1eb44332009-09-09 15:08:12 +00006504/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006505/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006506/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006507/// partial specialization thereof). This routine will rebuild that type now
6508/// that we have entered the declarator's scope, which may produce different
6509/// canonical types, e.g.,
6510///
6511/// \code
6512/// template<typename T>
6513/// struct X {
6514/// typedef T* pointer;
6515/// pointer data();
6516/// };
6517///
6518/// template<typename T>
6519/// typename X<T>::pointer X<T>::data() { ... }
6520/// \endcode
6521///
Douglas Gregor4714c122010-03-31 17:34:00 +00006522/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006523/// since we do not know that we can look into X<T> when we parsed the type.
6524/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006525/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006526/// as the canonical type of T*, allowing the return types of the out-of-line
6527/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006528TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6529 SourceLocation Loc,
6530 DeclarationName Name) {
6531 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006532 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006533
Douglas Gregor4a959d82009-08-06 16:20:37 +00006534 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6535 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006536}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006537
John McCall60d7b3a2010-08-24 06:29:42 +00006538ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006539 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6540 DeclarationName());
6541 return Rebuilder.TransformExpr(E);
6542}
6543
John McCall63b43852010-04-29 23:50:39 +00006544bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006545 if (SS.isInvalid())
6546 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006547
Douglas Gregor7e384942011-02-25 16:07:42 +00006548 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006549 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6550 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006551 NestedNameSpecifierLoc Rebuilt
6552 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6553 if (!Rebuilt)
6554 return true;
John McCall63b43852010-04-29 23:50:39 +00006555
Douglas Gregor7e384942011-02-25 16:07:42 +00006556 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006557 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006558}
6559
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006560/// \brief Produces a formatted string that describes the binding of
6561/// template parameters to template arguments.
6562std::string
6563Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6564 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006565 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006566}
6567
6568std::string
6569Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6570 const TemplateArgument *Args,
6571 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006572 llvm::SmallString<128> Str;
6573 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006574
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006575 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006576 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006577
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006578 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006579 if (I >= NumArgs)
6580 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006581
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006582 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006583 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006584 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006585 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006586
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006587 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006588 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006589 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006590 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006591 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006592
Douglas Gregor87dd6972010-12-20 16:52:59 +00006593 Out << " = ";
6594 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006595 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006596
6597 Out << ']';
6598 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006599}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006600
6601void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6602 if (!FD)
6603 return;
6604 FD->setLateTemplateParsed(Flag);
6605}
6606
6607bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6608 DeclContext *DC = CurContext;
6609
6610 while (DC) {
6611 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6612 const FunctionDecl *FD = RD->isLocalClass();
6613 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6614 } else if (DC->isTranslationUnit() || DC->isNamespace())
6615 return false;
6616
6617 DC = DC->getParent();
6618 }
6619 return false;
6620}