blob: d978c389b237bd4c53360fa7fc6869d0821af775 [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() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000605 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000606 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000607 // -- std::nullptr_t.
608 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000609 // If T is a dependent type, we can't do the check now, so we
610 // assume that it is well-formed.
611 T->isDependentType())
612 return T;
613 // C++ [temp.param]p8:
614 //
615 // A non-type template-parameter of type "array of T" or
616 // "function returning T" is adjusted to be of type "pointer to
617 // T" or "pointer to function returning T", respectively.
618 else if (T->isArrayType())
619 // FIXME: Keep the type prior to promotion?
620 return Context.getArrayDecayedType(T);
621 else if (T->isFunctionType())
622 // FIXME: Keep the type prior to promotion?
623 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000624
Douglas Gregor2943aed2009-03-03 04:44:36 +0000625 Diag(Loc, diag::err_template_nontype_parm_bad_type)
626 << T;
627
628 return QualType();
629}
630
John McCalld226f652010-08-21 09:40:31 +0000631Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
632 unsigned Depth,
633 unsigned Position,
634 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000635 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000636 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
637 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000638
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000639 assert(S->isTemplateParamScope() &&
640 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000641 bool Invalid = false;
642
643 IdentifierInfo *ParamName = D.getIdentifier();
644 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000645 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000646 LookupOrdinaryName,
647 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000648 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000649 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000650 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000651 }
652
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000653 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
654 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000655 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000656 Invalid = true;
657 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000658
Douglas Gregor10738d32010-12-23 23:51:58 +0000659 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000660 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000661 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000662 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000663 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000664 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000665 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000666 Param->setAccess(AS_public);
667
Douglas Gregor72c3f312008-12-05 18:15:24 +0000668 if (Invalid)
669 Param->setInvalidDecl();
670
671 if (D.getIdentifier()) {
672 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000673 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000674 IdResolver.AddDecl(Param);
675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000676
Douglas Gregor61c4d282011-01-05 15:48:55 +0000677 // C++0x [temp.param]p9:
678 // A default template-argument may be specified for any kind of
679 // template-parameter that is not a template parameter pack.
680 if (Default && IsParameterPack) {
681 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
682 Default = 0;
683 }
684
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000685 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000686 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000687 // Check for unexpanded parameter packs.
688 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
689 return Param;
690
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000691 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000692 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
693 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000694 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000695 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000696 }
John Wiegley429bb272011-04-08 18:41:53 +0000697 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000698
John McCall9ae2f072010-08-23 23:25:46 +0000699 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000700 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000701
John McCalld226f652010-08-21 09:40:31 +0000702 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000703}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000704
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000705/// ActOnTemplateTemplateParameter - Called when a C++ template template
706/// parameter (e.g. T in template <template <typename> class T> class array)
707/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000708Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
709 SourceLocation TmpLoc,
710 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000711 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000712 IdentifierInfo *Name,
713 SourceLocation NameLoc,
714 unsigned Depth,
715 unsigned Position,
716 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000717 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000718 assert(S->isTemplateParamScope() &&
719 "Template template parameter not in template parameter scope!");
720
721 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000722 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000723 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000724 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000725 NameLoc.isInvalid()? TmpLoc : NameLoc,
726 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000727 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000728 Param->setAccess(AS_public);
729
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000730 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000731 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000732 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000733 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000734 IdResolver.AddDecl(Param);
735 }
736
Douglas Gregor6f526752010-12-16 08:48:57 +0000737 if (Params->size() == 0) {
738 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
739 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
740 Param->setInvalidDecl();
741 }
742
Douglas Gregor61c4d282011-01-05 15:48:55 +0000743 // C++0x [temp.param]p9:
744 // A default template-argument may be specified for any kind of
745 // template-parameter that is not a template parameter pack.
746 if (IsParameterPack && !Default.isInvalid()) {
747 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
748 Default = ParsedTemplateArgument();
749 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000750
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000751 if (!Default.isInvalid()) {
752 // Check only that we have a template template argument. We don't want to
753 // try to check well-formedness now, because our template template parameter
754 // might have dependent types in its template parameters, which we wouldn't
755 // be able to match now.
756 //
757 // If none of the template template parameter's template arguments mention
758 // other template parameters, we could actually perform more checking here.
759 // However, it isn't worth doing.
760 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
761 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
762 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
763 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000764 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000765 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000766
Douglas Gregor6f526752010-12-16 08:48:57 +0000767 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000768 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000769 DefaultArg.getArgument().getAsTemplate(),
770 UPPC_DefaultArgument))
771 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000772
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000773 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775
John McCalld226f652010-08-21 09:40:31 +0000776 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000777}
778
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000779/// ActOnTemplateParameterList - Builds a TemplateParameterList that
780/// contains the template parameters in Params/NumParams.
781Sema::TemplateParamsTy *
782Sema::ActOnTemplateParameterList(unsigned Depth,
783 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000784 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000785 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000786 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000787 SourceLocation RAngleLoc) {
788 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000789 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000790
Douglas Gregorddc29e12009-02-06 22:42:48 +0000791 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000792 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000793 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000794}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000795
John McCallb6217662010-03-15 10:12:16 +0000796static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
797 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000798 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000799}
800
John McCallf312b1e2010-08-26 23:41:50 +0000801DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000802Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000803 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000804 IdentifierInfo *Name, SourceLocation NameLoc,
805 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000806 TemplateParameterList *TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000807 AccessSpecifier AS,
808 unsigned NumOuterTemplateParamLists,
809 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000810 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000811 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000812 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000813 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000814
815 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000816 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000817 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000818
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000819 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
820 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000821
822 // There is no such thing as an unnamed class template.
823 if (!Name) {
824 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000825 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000826 }
827
828 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000829 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000830 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000831 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000832 if (SS.isNotEmpty() && !SS.isInvalid()) {
833 SemanticContext = computeDeclContext(SS, true);
834 if (!SemanticContext) {
835 // FIXME: Produce a reasonable diagnostic here
836 return true;
837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
John McCall77bb1aa2010-05-01 00:40:08 +0000839 if (RequireCompleteDeclContext(SS, SemanticContext))
840 return true;
841
John McCalla24dc2e2009-11-17 02:14:36 +0000842 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000843 } else {
844 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000845 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Douglas Gregor57265e32010-04-12 16:00:01 +0000848 if (Previous.isAmbiguous())
849 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000850
Douglas Gregorddc29e12009-02-06 22:42:48 +0000851 NamedDecl *PrevDecl = 0;
852 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000853 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000854
Douglas Gregorddc29e12009-02-06 22:42:48 +0000855 // If there is a previous declaration with the same name, check
856 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000857 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000858 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000859
860 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000861 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000862 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000863 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000864 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
865 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000866 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000867 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
868 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
869 PrevClassTemplate
870 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
871 ->getSpecializedTemplate();
872 }
873 }
874
John McCall65c49462009-12-18 11:25:59 +0000875 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000876 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000877 // [...] When looking for a prior declaration of a class or a function
878 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000879 // function is neither a qualified name nor a template-id, scopes outside
880 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000881 if (!SS.isSet()) {
882 DeclContext *OutermostContext = CurContext;
883 while (!OutermostContext->isFileContext())
884 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000885
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000886 if (PrevDecl &&
887 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
888 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
889 SemanticContext = PrevDecl->getDeclContext();
890 } else {
891 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000892 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000893 // declaration.
894 PrevDecl = PrevClassTemplate = 0;
895 SemanticContext = OutermostContext;
896 }
John McCalle129d442009-12-17 23:21:11 +0000897 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000898
John McCalle129d442009-12-17 23:21:11 +0000899 if (CurContext->isDependentContext()) {
900 // If this is a dependent context, we don't want to link the friend
901 // class template to the template in scope, because that would perform
902 // checking of the template parameter lists that can't be performed
903 // until the outer context is instantiated.
904 PrevDecl = PrevClassTemplate = 0;
905 }
906 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
907 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000908
Douglas Gregorddc29e12009-02-06 22:42:48 +0000909 if (PrevClassTemplate) {
910 // Ensure that the template parameter lists are compatible.
911 if (!TemplateParameterListsAreEqual(TemplateParams,
912 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000913 /*Complain=*/true,
914 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000915 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916
917 // C++ [temp.class]p4:
918 // In a redeclaration, partial specialization, explicit
919 // specialization or explicit instantiation of a class template,
920 // the class-key shall agree in kind with the original class
921 // template declaration (7.1.5.3).
922 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000923 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000924 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000925 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000926 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000927 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000928 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000929 }
930
Douglas Gregorddc29e12009-02-06 22:42:48 +0000931 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000932 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000933 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000934 Diag(NameLoc, diag::err_redefinition) << Name;
935 Diag(Def->getLocation(), diag::note_previous_definition);
936 // FIXME: Would it make sense to try to "forget" the previous
937 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000938 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000939 }
940 }
941 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
942 // Maybe we will complain about the shadowed template parameter.
943 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
944 // Just pretend that we didn't see the previous declaration.
945 PrevDecl = 0;
946 } else if (PrevDecl) {
947 // C++ [temp]p5:
948 // A class template shall not have the same name as any other
949 // template, class, function, object, enumeration, enumerator,
950 // namespace, or type in the same scope (3.3), except as specified
951 // in (14.5.4).
952 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
953 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000954 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000955 }
956
Douglas Gregord684b002009-02-10 19:49:53 +0000957 // Check the template parameter list of this declaration, possibly
958 // merging in the template parameter list from the previous class
959 // template declaration.
960 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000961 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000962 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000963 SemanticContext->isRecord() &&
964 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000965 ? TPC_ClassTemplateMember
966 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000967 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Douglas Gregor57265e32010-04-12 16:00:01 +0000969 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000970 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000971 // template out-of-line.
972 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
973 !(TUK == TUK_Friend && CurContext->isDependentContext()))
974 Diag(NameLoc, diag::err_member_def_does_not_match)
975 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000976 }
977
Mike Stump1eb44332009-09-09 15:08:12 +0000978 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000979 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000980 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000981 PrevClassTemplate->getTemplatedDecl() : 0,
982 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000983 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000984 if (NumOuterTemplateParamLists > 0)
985 NewClass->setTemplateParameterListsInfo(Context,
986 NumOuterTemplateParamLists,
987 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000988
989 ClassTemplateDecl *NewTemplate
990 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
991 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000992 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000993 NewClass->setDescribedClassTemplate(NewTemplate);
994
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000995 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000996 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000997 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000998 assert(T->isDependentType() && "Class template type is not dependent?");
999 (void)T;
1000
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001001 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001002 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001003 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001004 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1005 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001006
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001007 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001008 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001009 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Douglas Gregorddc29e12009-02-06 22:42:48 +00001011 // Set the lexical context of these templates
1012 NewClass->setLexicalDeclContext(CurContext);
1013 NewTemplate->setLexicalDeclContext(CurContext);
1014
John McCall0f434ec2009-07-31 02:45:11 +00001015 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001016 NewClass->startDefinition();
1017
1018 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001019 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001020
John McCall05b23ea2009-09-14 21:59:20 +00001021 if (TUK != TUK_Friend)
1022 PushOnScopeChains(NewTemplate, S);
1023 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001024 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001025 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001026 NewClass->setAccess(PrevClassTemplate->getAccess());
1027 }
John McCall05b23ea2009-09-14 21:59:20 +00001028
Douglas Gregord85bea22009-09-26 06:47:28 +00001029 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1030 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001031
John McCall05b23ea2009-09-14 21:59:20 +00001032 // Friend templates are visible in fairly strange ways.
1033 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001034 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001035 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1036 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1037 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001038 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001039 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001040
Douglas Gregord85bea22009-09-26 06:47:28 +00001041 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1042 NewClass->getLocation(),
1043 NewTemplate,
1044 /*FIXME:*/NewClass->getLocation());
1045 Friend->setAccess(AS_public);
1046 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001047 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001048
Douglas Gregord684b002009-02-10 19:49:53 +00001049 if (Invalid) {
1050 NewTemplate->setInvalidDecl();
1051 NewClass->setInvalidDecl();
1052 }
John McCalld226f652010-08-21 09:40:31 +00001053 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001054}
1055
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001056/// \brief Diagnose the presence of a default template argument on a
1057/// template parameter, which is ill-formed in certain contexts.
1058///
1059/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001060static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001061 Sema::TemplateParamListContext TPC,
1062 SourceLocation ParamLoc,
1063 SourceRange DefArgRange) {
1064 switch (TPC) {
1065 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001066 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001067 return false;
1068
1069 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001070 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001071 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001072 // A default template-argument shall not be specified in a
1073 // function template declaration or a function template
1074 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001075 // If a friend function template declaration specifies a default
1076 // template-argument, that declaration shall be a definition and shall be
1077 // the only declaration of the function template in the translation unit.
1078 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001079 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001080 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001081 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001082 << DefArgRange;
1083 return false;
1084
1085 case Sema::TPC_ClassTemplateMember:
1086 // C++0x [temp.param]p9:
1087 // A default template-argument shall not be specified in the
1088 // template-parameter-lists of the definition of a member of a
1089 // class template that appears outside of the member's class.
1090 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1091 << DefArgRange;
1092 return true;
1093
1094 case Sema::TPC_FriendFunctionTemplate:
1095 // C++ [temp.param]p9:
1096 // A default template-argument shall not be specified in a
1097 // friend template declaration.
1098 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1099 << DefArgRange;
1100 return true;
1101
1102 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1103 // for friend function templates if there is only a single
1104 // declaration (and it is a definition). Strange!
1105 }
1106
1107 return false;
1108}
1109
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001110/// \brief Check for unexpanded parameter packs within the template parameters
1111/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001112static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1113 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001114 TemplateParameterList *Params = TTP->getTemplateParameters();
1115 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1116 NamedDecl *P = Params->getParam(I);
1117 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001118 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001119 NTTP->getTypeSourceInfo(),
1120 Sema::UPPC_NonTypeTemplateParameterType))
1121 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001122
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001123 continue;
1124 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001125
1126 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001127 = dyn_cast<TemplateTemplateParmDecl>(P))
1128 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1129 return true;
1130 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001131
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001132 return false;
1133}
1134
Douglas Gregord684b002009-02-10 19:49:53 +00001135/// \brief Checks the validity of a template parameter list, possibly
1136/// considering the template parameter list from a previous
1137/// declaration.
1138///
1139/// If an "old" template parameter list is provided, it must be
1140/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1141/// template parameter list.
1142///
1143/// \param NewParams Template parameter list for a new template
1144/// declaration. This template parameter list will be updated with any
1145/// default arguments that are carried through from the previous
1146/// template parameter list.
1147///
1148/// \param OldParams If provided, template parameter list from a
1149/// previous declaration of the same template. Default template
1150/// arguments will be merged from the old template parameter list to
1151/// the new template parameter list.
1152///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001153/// \param TPC Describes the context in which we are checking the given
1154/// template parameter list.
1155///
Douglas Gregord684b002009-02-10 19:49:53 +00001156/// \returns true if an error occurred, false otherwise.
1157bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001158 TemplateParameterList *OldParams,
1159 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001160 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Douglas Gregord684b002009-02-10 19:49:53 +00001162 // C++ [temp.param]p10:
1163 // The set of default template-arguments available for use with a
1164 // template declaration or definition is obtained by merging the
1165 // default arguments from the definition (if in scope) and all
1166 // declarations in scope in the same way default function
1167 // arguments are (8.3.6).
1168 bool SawDefaultArgument = false;
1169 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001170
Anders Carlsson49d25572009-06-12 23:20:15 +00001171 bool SawParameterPack = false;
1172 SourceLocation ParameterPackLoc;
1173
Mike Stump1a35fde2009-02-11 23:03:27 +00001174 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001175 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001176 if (OldParams)
1177 OldParam = OldParams->begin();
1178
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001179 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001180 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1181 NewParamEnd = NewParams->end();
1182 NewParam != NewParamEnd; ++NewParam) {
1183 // Variables used to diagnose redundant default arguments
1184 bool RedundantDefaultArg = false;
1185 SourceLocation OldDefaultLoc;
1186 SourceLocation NewDefaultLoc;
1187
1188 // Variables used to diagnose missing default arguments
1189 bool MissingDefaultArg = false;
1190
Anders Carlsson49d25572009-06-12 23:20:15 +00001191 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001192 // If a template parameter of a primary class template or alias template
1193 // is a template parameter pack, it shall be the last template parameter.
1194 if (SawParameterPack &&
1195 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001196 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001197 diag::err_template_param_pack_must_be_last_template_parameter);
1198 Invalid = true;
1199 }
1200
Douglas Gregord684b002009-02-10 19:49:53 +00001201 if (TemplateTypeParmDecl *NewTypeParm
1202 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001203 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001204 if (NewTypeParm->hasDefaultArgument() &&
1205 DiagnoseDefaultTemplateArgument(*this, TPC,
1206 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001207 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001208 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001209 NewTypeParm->removeDefaultArgument();
1210
1211 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001212 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001213 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Anders Carlsson49d25572009-06-12 23:20:15 +00001215 if (NewTypeParm->isParameterPack()) {
1216 assert(!NewTypeParm->hasDefaultArgument() &&
1217 "Parameter packs can't have a default argument!");
1218 SawParameterPack = true;
1219 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001220 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001221 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001222 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1223 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1224 SawDefaultArgument = true;
1225 RedundantDefaultArg = true;
1226 PreviousDefaultArgLoc = NewDefaultLoc;
1227 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1228 // Merge the default argument from the old declaration to the
1229 // new declaration.
1230 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001231 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001232 true);
1233 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1234 } else if (NewTypeParm->hasDefaultArgument()) {
1235 SawDefaultArgument = true;
1236 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1237 } else if (SawDefaultArgument)
1238 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001239 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001240 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001241 // Check for unexpanded parameter packs.
1242 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001243 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001244 UPPC_NonTypeTemplateParameterType)) {
1245 Invalid = true;
1246 continue;
1247 }
1248
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001249 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001250 if (NewNonTypeParm->hasDefaultArgument() &&
1251 DiagnoseDefaultTemplateArgument(*this, TPC,
1252 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001253 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001254 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001255 }
1256
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001257 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001258 NonTypeTemplateParmDecl *OldNonTypeParm
1259 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001260 if (NewNonTypeParm->isParameterPack()) {
1261 assert(!NewNonTypeParm->hasDefaultArgument() &&
1262 "Parameter packs can't have a default argument!");
1263 SawParameterPack = true;
1264 ParameterPackLoc = NewNonTypeParm->getLocation();
1265 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001266 NewNonTypeParm->hasDefaultArgument()) {
1267 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1268 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1269 SawDefaultArgument = true;
1270 RedundantDefaultArg = true;
1271 PreviousDefaultArgLoc = NewDefaultLoc;
1272 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1273 // Merge the default argument from the old declaration to the
1274 // new declaration.
1275 SawDefaultArgument = true;
1276 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001277 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001278 // parameter.
1279 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001280 OldNonTypeParm->getDefaultArgument(),
1281 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001282 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1283 } else if (NewNonTypeParm->hasDefaultArgument()) {
1284 SawDefaultArgument = true;
1285 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1286 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001287 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001288 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001289 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001290 TemplateTemplateParmDecl *NewTemplateParm
1291 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001292
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001293 // Check for unexpanded parameter packs, recursively.
1294 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1295 Invalid = true;
1296 continue;
1297 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001298
1299 if (NewTemplateParm->hasDefaultArgument() &&
1300 DiagnoseDefaultTemplateArgument(*this, TPC,
1301 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001302 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001303 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001304
1305 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001306 TemplateTemplateParmDecl *OldTemplateParm
1307 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001308 if (NewTemplateParm->isParameterPack()) {
1309 assert(!NewTemplateParm->hasDefaultArgument() &&
1310 "Parameter packs can't have a default argument!");
1311 SawParameterPack = true;
1312 ParameterPackLoc = NewTemplateParm->getLocation();
1313 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001314 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001315 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1316 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001317 SawDefaultArgument = true;
1318 RedundantDefaultArg = true;
1319 PreviousDefaultArgLoc = NewDefaultLoc;
1320 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1321 // Merge the default argument from the old declaration to the
1322 // new declaration.
1323 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001324 // FIXME: We need to create a new kind of "default argument" expression
1325 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001326 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001327 OldTemplateParm->getDefaultArgument(),
1328 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001329 PreviousDefaultArgLoc
1330 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001331 } else if (NewTemplateParm->hasDefaultArgument()) {
1332 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001333 PreviousDefaultArgLoc
1334 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001335 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001336 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001337 }
1338
1339 if (RedundantDefaultArg) {
1340 // C++ [temp.param]p12:
1341 // A template-parameter shall not be given default arguments
1342 // by two different declarations in the same scope.
1343 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1344 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1345 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001346 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001347 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001348 // If a template-parameter of a class template has a default
1349 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001350 // have a default template-argument supplied or be a template parameter
1351 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001352 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001353 diag::err_template_param_default_arg_missing);
1354 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1355 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001356 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001357 }
1358
1359 // If we have an old template parameter list that we're merging
1360 // in, move on to the next parameter.
1361 if (OldParams)
1362 ++OldParam;
1363 }
1364
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001365 // We were missing some default arguments at the end of the list, so remove
1366 // all of the default arguments.
1367 if (RemoveDefaultArguments) {
1368 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1369 NewParamEnd = NewParams->end();
1370 NewParam != NewParamEnd; ++NewParam) {
1371 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1372 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001373 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001374 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1375 NTTP->removeDefaultArgument();
1376 else
1377 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1378 }
1379 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001380
Douglas Gregord684b002009-02-10 19:49:53 +00001381 return Invalid;
1382}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001383
John McCall4e2cbb22010-10-20 05:44:58 +00001384namespace {
1385
1386/// A class which looks for a use of a certain level of template
1387/// parameter.
1388struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1389 typedef RecursiveASTVisitor<DependencyChecker> super;
1390
1391 unsigned Depth;
1392 bool Match;
1393
1394 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1395 NamedDecl *ND = Params->getParam(0);
1396 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1397 Depth = PD->getDepth();
1398 } else if (NonTypeTemplateParmDecl *PD =
1399 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1400 Depth = PD->getDepth();
1401 } else {
1402 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1403 }
1404 }
1405
1406 bool Matches(unsigned ParmDepth) {
1407 if (ParmDepth >= Depth) {
1408 Match = true;
1409 return true;
1410 }
1411 return false;
1412 }
1413
1414 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1415 return !Matches(T->getDepth());
1416 }
1417
1418 bool TraverseTemplateName(TemplateName N) {
1419 if (TemplateTemplateParmDecl *PD =
1420 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1421 if (Matches(PD->getDepth())) return false;
1422 return super::TraverseTemplateName(N);
1423 }
1424
1425 bool VisitDeclRefExpr(DeclRefExpr *E) {
1426 if (NonTypeTemplateParmDecl *PD =
1427 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1428 if (PD->getDepth() == Depth) {
1429 Match = true;
1430 return false;
1431 }
1432 }
1433 return super::VisitDeclRefExpr(E);
1434 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001435
1436 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1437 return TraverseType(T->getInjectedSpecializationType());
1438 }
John McCall4e2cbb22010-10-20 05:44:58 +00001439};
1440}
1441
Douglas Gregorc8406492011-05-10 18:27:06 +00001442/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001443/// list.
1444static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001445DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001446 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001447 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001448 return Checker.Match;
1449}
1450
Douglas Gregorc8406492011-05-10 18:27:06 +00001451// Find the source range corresponding to the named type in the given
1452// nested-name-specifier, if any.
1453static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1454 QualType T,
1455 const CXXScopeSpec &SS) {
1456 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1457 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1458 if (const Type *CurType = NNS->getAsType()) {
1459 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1460 return NNSLoc.getTypeLoc().getSourceRange();
1461 } else
1462 break;
1463
1464 NNSLoc = NNSLoc.getPrefix();
1465 }
1466
1467 return SourceRange();
1468}
1469
Mike Stump1eb44332009-09-09 15:08:12 +00001470/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001471/// specifier, returning the template parameter list that applies to the
1472/// name.
1473///
1474/// \param DeclStartLoc the start of the declaration that has a scope
1475/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001476///
Douglas Gregorc8406492011-05-10 18:27:06 +00001477/// \param DeclLoc The location of the declaration itself.
1478///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001479/// \param SS the scope specifier that will be matched to the given template
1480/// parameter lists. This scope specifier precedes a qualified name that is
1481/// being declared.
1482///
1483/// \param ParamLists the template parameter lists, from the outermost to the
1484/// innermost template parameter lists.
1485///
1486/// \param NumParamLists the number of template parameter lists in ParamLists.
1487///
John McCall77e8b112010-04-13 20:37:33 +00001488/// \param IsFriend Whether to apply the slightly different rules for
1489/// matching template parameters to scope specifiers in friend
1490/// declarations.
1491///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001492/// \param IsExplicitSpecialization will be set true if the entity being
1493/// declared is an explicit specialization, false otherwise.
1494///
Mike Stump1eb44332009-09-09 15:08:12 +00001495/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001496/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001497/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001498/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001499/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001500/// itself a template).
1501TemplateParameterList *
1502Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001503 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001504 const CXXScopeSpec &SS,
1505 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001506 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001507 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001508 bool &IsExplicitSpecialization,
1509 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001510 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001511 Invalid = false;
1512
1513 // The sequence of nested types to which we will match up the template
1514 // parameter lists. We first build this list by starting with the type named
1515 // by the nested-name-specifier and walking out until we run out of types.
1516 llvm::SmallVector<QualType, 4> NestedTypes;
1517 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001518 if (SS.getScopeRep()) {
1519 if (CXXRecordDecl *Record
1520 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1521 T = Context.getTypeDeclType(Record);
1522 else
1523 T = QualType(SS.getScopeRep()->getAsType(), 0);
1524 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001525
1526 // If we found an explicit specialization that prevents us from needing
1527 // 'template<>' headers, this will be set to the location of that
1528 // explicit specialization.
1529 SourceLocation ExplicitSpecLoc;
1530
1531 while (!T.isNull()) {
1532 NestedTypes.push_back(T);
1533
1534 // Retrieve the parent of a record type.
1535 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1536 // If this type is an explicit specialization, we're done.
1537 if (ClassTemplateSpecializationDecl *Spec
1538 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1539 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1540 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1541 ExplicitSpecLoc = Spec->getLocation();
1542 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001543 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001544 } else if (Record->getTemplateSpecializationKind()
1545 == TSK_ExplicitSpecialization) {
1546 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001547 break;
1548 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001549
1550 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1551 T = Context.getTypeDeclType(Parent);
1552 else
1553 T = QualType();
1554 continue;
1555 }
1556
1557 if (const TemplateSpecializationType *TST
1558 = T->getAs<TemplateSpecializationType>()) {
1559 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1560 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1561 T = Context.getTypeDeclType(Parent);
1562 else
1563 T = QualType();
1564 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001565 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001566 }
1567
1568 // Look one step prior in a dependent template specialization type.
1569 if (const DependentTemplateSpecializationType *DependentTST
1570 = T->getAs<DependentTemplateSpecializationType>()) {
1571 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1572 T = QualType(NNS->getAsType(), 0);
1573 else
1574 T = QualType();
1575 continue;
1576 }
1577
1578 // Look one step prior in a dependent name type.
1579 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1580 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1581 T = QualType(NNS->getAsType(), 0);
1582 else
1583 T = QualType();
1584 continue;
1585 }
1586
1587 // Retrieve the parent of an enumeration type.
1588 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1589 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1590 // check here.
1591 EnumDecl *Enum = EnumT->getDecl();
1592
1593 // Get to the parent type.
1594 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1595 T = Context.getTypeDeclType(Parent);
1596 else
1597 T = QualType();
1598 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001599 }
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Douglas Gregorc8406492011-05-10 18:27:06 +00001601 T = QualType();
1602 }
1603 // Reverse the nested types list, since we want to traverse from the outermost
1604 // to the innermost while checking template-parameter-lists.
1605 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001606
Douglas Gregorc8406492011-05-10 18:27:06 +00001607 // C++0x [temp.expl.spec]p17:
1608 // A member or a member template may be nested within many
1609 // enclosing class templates. In an explicit specialization for
1610 // such a member, the member declaration shall be preceded by a
1611 // template<> for each enclosing class template that is
1612 // explicitly specialized.
1613 unsigned ParamIdx = 0;
1614 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1615 ++TypeIdx) {
1616 T = NestedTypes[TypeIdx];
1617
1618 // Whether we expect a 'template<>' header.
1619 bool NeedEmptyTemplateHeader = false;
1620
1621 // Whether we expect a template header with parameters.
1622 bool NeedNonemptyTemplateHeader = false;
1623
1624 // For a dependent type, the set of template parameters that we
1625 // expect to see.
1626 TemplateParameterList *ExpectedTemplateParams = 0;
1627
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001628 // C++0x [temp.expl.spec]p15:
1629 // A member or a member template may be nested within many enclosing
1630 // class templates. In an explicit specialization for such a member, the
1631 // member declaration shall be preceded by a template<> for each
1632 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001633 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1634 if (ClassTemplatePartialSpecializationDecl *Partial
1635 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1636 ExpectedTemplateParams = Partial->getTemplateParameters();
1637 NeedNonemptyTemplateHeader = true;
1638 } else if (Record->isDependentType()) {
1639 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001640 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001641 ->getTemplateParameters();
1642 NeedNonemptyTemplateHeader = true;
1643 }
1644 } else if (ClassTemplateSpecializationDecl *Spec
1645 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1646 // C++0x [temp.expl.spec]p4:
1647 // Members of an explicitly specialized class template are defined
1648 // in the same manner as members of normal classes, and not using
1649 // the template<> syntax.
1650 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1651 NeedEmptyTemplateHeader = true;
1652 else
1653 break;
1654 } else if (Record->getTemplateSpecializationKind()) {
1655 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001656 != TSK_ExplicitSpecialization &&
1657 TypeIdx == NumTypes - 1)
1658 IsExplicitSpecialization = true;
1659
1660 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001661 }
1662 } else if (const TemplateSpecializationType *TST
1663 = T->getAs<TemplateSpecializationType>()) {
1664 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1665 ExpectedTemplateParams = Template->getTemplateParameters();
1666 NeedNonemptyTemplateHeader = true;
1667 }
1668 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1669 // FIXME: We actually could/should check the template arguments here
1670 // against the corresponding template parameter list.
1671 NeedNonemptyTemplateHeader = false;
1672 }
1673
1674 if (NeedEmptyTemplateHeader) {
1675 // If we're on the last of the types, and we need a 'template<>' header
1676 // here, then it's an explicit specialization.
1677 if (TypeIdx == NumTypes - 1)
1678 IsExplicitSpecialization = true;
1679
1680 if (ParamIdx < NumParamLists) {
1681 if (ParamLists[ParamIdx]->size() > 0) {
1682 // The header has template parameters when it shouldn't. Complain.
1683 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1684 diag::err_template_param_list_matches_nontemplate)
1685 << T
1686 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1687 ParamLists[ParamIdx]->getRAngleLoc())
1688 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1689 Invalid = true;
1690 return 0;
1691 }
1692
1693 // Consume this template header.
1694 ++ParamIdx;
1695 continue;
1696 }
1697
1698 if (!IsFriend) {
1699 // We don't have a template header, but we should.
1700 SourceLocation ExpectedTemplateLoc;
1701 if (NumParamLists > 0)
1702 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1703 else
1704 ExpectedTemplateLoc = DeclStartLoc;
1705
1706 Diag(DeclLoc, diag::err_template_spec_needs_header)
1707 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1708 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1709 }
1710
1711 continue;
1712 }
1713
1714 if (NeedNonemptyTemplateHeader) {
1715 // In friend declarations we can have template-ids which don't
1716 // depend on the corresponding template parameter lists. But
1717 // assume that empty parameter lists are supposed to match this
1718 // template-id.
1719 if (IsFriend && T->isDependentType()) {
1720 if (ParamIdx < NumParamLists &&
1721 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1722 ExpectedTemplateParams = 0;
1723 else
1724 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001725 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001726
Douglas Gregorc8406492011-05-10 18:27:06 +00001727 if (ParamIdx < NumParamLists) {
1728 // Check the template parameter list, if we can.
1729 if (ExpectedTemplateParams &&
1730 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1731 ExpectedTemplateParams,
1732 true, TPL_TemplateMatch))
1733 Invalid = true;
1734
1735 if (!Invalid &&
1736 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1737 TPC_ClassTemplateMember))
1738 Invalid = true;
1739
1740 ++ParamIdx;
1741 continue;
1742 }
1743
1744 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1745 << T
1746 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1747 Invalid = true;
1748 continue;
1749 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001750 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001751
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001752 // If there were at least as many template-ids as there were template
1753 // parameter lists, then there are no template parameter lists remaining for
1754 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001755 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001756 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001758 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001759 if (ParamIdx < NumParamLists - 1) {
1760 bool HasAnyExplicitSpecHeader = false;
1761 bool AllExplicitSpecHeaders = true;
1762 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1763 if (ParamLists[I]->size() == 0)
1764 HasAnyExplicitSpecHeader = true;
1765 else
1766 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001767 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001768
1769 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1770 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1771 : diag::err_template_spec_extra_headers)
1772 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1773 ParamLists[NumParamLists - 2]->getRAngleLoc());
1774
1775 // If there was a specialization somewhere, such that 'template<>' is
1776 // not required, and there were any 'template<>' headers, note where the
1777 // specialization occurred.
1778 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1779 Diag(ExplicitSpecLoc,
1780 diag::note_explicit_template_spec_does_not_need_header)
1781 << NestedTypes.back();
1782
1783 // We have a template parameter list with no corresponding scope, which
1784 // means that the resulting template declaration can't be instantiated
1785 // properly (we'll end up with dependent nodes when we shouldn't).
1786 if (!AllExplicitSpecHeaders)
1787 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001788 }
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001790 // Return the last template parameter list, which corresponds to the
1791 // entity being declared.
1792 return ParamLists[NumParamLists - 1];
1793}
1794
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001795void Sema::NoteAllFoundTemplates(TemplateName Name) {
1796 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1797 Diag(Template->getLocation(), diag::note_template_declared_here)
1798 << (isa<FunctionTemplateDecl>(Template)? 0
1799 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001800 : isa<TypeAliasTemplateDecl>(Template)? 2
1801 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001802 << Template->getDeclName();
1803 return;
1804 }
1805
1806 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1807 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1808 IEnd = OST->end();
1809 I != IEnd; ++I)
1810 Diag((*I)->getLocation(), diag::note_template_declared_here)
1811 << 0 << (*I)->getDeclName();
1812
1813 return;
1814 }
1815}
1816
1817
Douglas Gregor7532dc62009-03-30 22:58:21 +00001818QualType Sema::CheckTemplateIdType(TemplateName Name,
1819 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001820 TemplateArgumentListInfo &TemplateArgs) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001821 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1822 if (DTN && DTN->isIdentifier())
1823 // When building a template-id where the template-name is dependent,
1824 // assume the template is a type template. Either our assumption is
1825 // correct, or the code is ill-formed and will be diagnosed when the
1826 // dependent name is substituted.
1827 return Context.getDependentTemplateSpecializationType(ETK_None,
1828 DTN->getQualifier(),
1829 DTN->getIdentifier(),
1830 TemplateArgs);
1831
Douglas Gregor7532dc62009-03-30 22:58:21 +00001832 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001833 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1834 // We might have a substituted template template parameter pack. If so,
1835 // build a template specialization type for it.
1836 if (Name.getAsSubstTemplateTemplateParmPack())
1837 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001838
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001839 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1840 << Name;
1841 NoteAllFoundTemplates(Name);
1842 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001843 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001844
Douglas Gregor40808ce2009-03-09 23:48:35 +00001845 // Check that the template argument list is well-formed for this
1846 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001847 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001848 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001849 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001850 return QualType();
1851
Douglas Gregor910f8002010-11-07 23:05:16 +00001852 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001853 "Converted template argument list is too short!");
1854
1855 QualType CanonType;
1856
Richard Smith3e4c6c42011-05-05 21:57:07 +00001857 if (TypeAliasTemplateDecl *AliasTemplate
1858 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1859 // Find the canonical type for this type alias template specialization.
1860 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1861 if (Pattern->isInvalidDecl())
1862 return QualType();
1863
1864 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1865 Converted.data(), Converted.size());
1866
1867 // Only substitute for the innermost template argument list.
1868 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001869 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001870 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1871 for (unsigned I = 0; I < Depth; ++I)
1872 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001873
1874 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1875 CanonType = SubstType(Pattern->getUnderlyingType(),
1876 TemplateArgLists, AliasTemplate->getLocation(),
1877 AliasTemplate->getDeclName());
1878 if (CanonType.isNull())
1879 return QualType();
1880 } else if (Name.isDependent() ||
1881 TemplateSpecializationType::anyDependentTemplateArguments(
1882 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001883 // This class template specialization is a dependent
1884 // type. Therefore, its canonical type is another class template
1885 // specialization type that contains all of the converted
1886 // arguments in canonical form. This ensures that, e.g., A<T> and
1887 // A<T, T> have identical types when A is declared as:
1888 //
1889 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001890 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001891 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001892 Converted.data(),
1893 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Douglas Gregor1275ae02009-07-28 23:00:59 +00001895 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001896 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001897 // In the future, we need to teach getTemplateSpecializationType to only
1898 // build the canonical type and return that to us.
1899 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001900
1901 // This might work out to be a current instantiation, in which
1902 // case the canonical type needs to be the InjectedClassNameType.
1903 //
1904 // TODO: in theory this could be a simple hashtable lookup; most
1905 // changes to CurContext don't change the set of current
1906 // instantiations.
1907 if (isa<ClassTemplateDecl>(Template)) {
1908 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1909 // If we get out to a namespace, we're done.
1910 if (Ctx->isFileContext()) break;
1911
1912 // If this isn't a record, keep looking.
1913 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1914 if (!Record) continue;
1915
1916 // Look for one of the two cases with InjectedClassNameTypes
1917 // and check whether it's the same template.
1918 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1919 !Record->getDescribedClassTemplate())
1920 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001921
John McCall31f17ec2010-04-27 00:57:59 +00001922 // Fetch the injected class name type and check whether its
1923 // injected type is equal to the type we just built.
1924 QualType ICNT = Context.getTypeDeclType(Record);
1925 QualType Injected = cast<InjectedClassNameType>(ICNT)
1926 ->getInjectedSpecializationType();
1927
1928 if (CanonType != Injected->getCanonicalTypeInternal())
1929 continue;
1930
1931 // If so, the canonical type of this TST is the injected
1932 // class name type of the record we just found.
1933 assert(ICNT.isCanonical());
1934 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001935 break;
1936 }
1937 }
Mike Stump1eb44332009-09-09 15:08:12 +00001938 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001939 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001940 // Find the class template specialization declaration that
1941 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001942 void *InsertPos = 0;
1943 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001944 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001945 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001946 if (!Decl) {
1947 // This is the first time we have referenced this class template
1948 // specialization. Create the canonical declaration and add it to
1949 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001950 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001951 ClassTemplate->getTemplatedDecl()->getTagKind(),
1952 ClassTemplate->getDeclContext(),
1953 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001954 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001955 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001956 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001957 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001958 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001959 Decl->setLexicalDeclContext(CurContext);
1960 }
1961
1962 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001963 assert(isa<RecordType>(CanonType) &&
1964 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001965 }
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Douglas Gregor40808ce2009-03-09 23:48:35 +00001967 // Build the fully-sugared type for this class template
1968 // specialization, which refers back to the class template
1969 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001970 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001971}
1972
John McCallf312b1e2010-08-26 23:41:50 +00001973TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001974Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1975 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001976 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001977 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001978 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001979 if (SS.isInvalid())
1980 return true;
1981
Douglas Gregor7532dc62009-03-30 22:58:21 +00001982 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001983
Douglas Gregor40808ce2009-03-09 23:48:35 +00001984 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001985 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001986 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001987
Douglas Gregora88f09f2011-02-28 17:23:35 +00001988 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1989 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1990 DTN->getQualifier(),
1991 DTN->getIdentifier(),
1992 TemplateArgs);
1993
1994 // Build type-source information.
1995 TypeLocBuilder TLB;
1996 DependentTemplateSpecializationTypeLoc SpecTL
1997 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001998 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001999 SpecTL.setNameLoc(TemplateLoc);
2000 SpecTL.setLAngleLoc(LAngleLoc);
2001 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002002 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002003 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2004 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2005 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2006 }
2007
John McCalld5532b62009-11-23 01:53:49 +00002008 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002009 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002010
2011 if (Result.isNull())
2012 return true;
2013
Douglas Gregor059101f2011-03-02 00:47:37 +00002014 // Build type-source information.
2015 TypeLocBuilder TLB;
2016 TemplateSpecializationTypeLoc SpecTL
2017 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2018 SpecTL.setTemplateNameLoc(TemplateLoc);
2019 SpecTL.setLAngleLoc(LAngleLoc);
2020 SpecTL.setRAngleLoc(RAngleLoc);
2021 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2022 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002023
Douglas Gregor059101f2011-03-02 00:47:37 +00002024 if (SS.isNotEmpty()) {
2025 // Create an elaborated-type-specifier containing the nested-name-specifier.
2026 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2027 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2028 ElabTL.setKeywordLoc(SourceLocation());
2029 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2030 }
2031
2032 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002033}
John McCallf1bbbb42009-09-04 01:14:41 +00002034
Douglas Gregor059101f2011-03-02 00:47:37 +00002035TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002036 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002037 SourceLocation TagLoc,
2038 CXXScopeSpec &SS,
2039 TemplateTy TemplateD,
2040 SourceLocation TemplateLoc,
2041 SourceLocation LAngleLoc,
2042 ASTTemplateArgsPtr TemplateArgsIn,
2043 SourceLocation RAngleLoc) {
2044 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2045
2046 // Translate the parser's template argument list in our AST format.
2047 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2048 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2049
2050 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002051 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002052 ElaboratedTypeKeyword Keyword
2053 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Douglas Gregor059101f2011-03-02 00:47:37 +00002055 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2056 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2057 DTN->getQualifier(),
2058 DTN->getIdentifier(),
2059 TemplateArgs);
2060
2061 // Build type-source information.
2062 TypeLocBuilder TLB;
2063 DependentTemplateSpecializationTypeLoc SpecTL
2064 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2065 SpecTL.setKeywordLoc(TagLoc);
2066 SpecTL.setNameLoc(TemplateLoc);
2067 SpecTL.setLAngleLoc(LAngleLoc);
2068 SpecTL.setRAngleLoc(RAngleLoc);
2069 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2070 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2071 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2072 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2073 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002074
2075 if (TypeAliasTemplateDecl *TAT =
2076 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2077 // C++0x [dcl.type.elab]p2:
2078 // If the identifier resolves to a typedef-name or the simple-template-id
2079 // resolves to an alias template specialization, the
2080 // elaborated-type-specifier is ill-formed.
2081 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2082 Diag(TAT->getLocation(), diag::note_declared_at);
2083 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002084
2085 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2086 if (Result.isNull())
2087 return TypeResult();
2088
2089 // Check the tag kind
2090 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002091 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002092
John McCall6b2becf2009-09-08 17:47:29 +00002093 IdentifierInfo *Id = D->getIdentifier();
2094 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002095
John McCall6b2becf2009-09-08 17:47:29 +00002096 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
2097 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002098 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002099 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002100 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002101 }
2102 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002103
2104 // Provide source-location information for the template specialization.
2105 TypeLocBuilder TLB;
2106 TemplateSpecializationTypeLoc SpecTL
2107 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2108 SpecTL.setTemplateNameLoc(TemplateLoc);
2109 SpecTL.setLAngleLoc(LAngleLoc);
2110 SpecTL.setRAngleLoc(RAngleLoc);
2111 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2112 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002113
Douglas Gregor059101f2011-03-02 00:47:37 +00002114 // Construct an elaborated type containing the nested-name-specifier (if any)
2115 // and keyword.
2116 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2117 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2118 ElabTL.setKeywordLoc(TagLoc);
2119 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2120 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002121}
2122
John McCall60d7b3a2010-08-24 06:29:42 +00002123ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002124 LookupResult &R,
2125 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002126 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002127 // FIXME: Can we do any checking at this point? I guess we could check the
2128 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002129 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002130 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002131 // foo<int> could identify a single function unambiguously
2132 // This approach does NOT work, since f<int>(1);
2133 // gets resolved prior to resorting to overload resolution
2134 // i.e., template<class T> void f(double);
2135 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002136
2137 // These should be filtered out by our callers.
2138 assert(!R.empty() && "empty lookup results when building templateid");
2139 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2140
John McCallc373d482010-01-27 01:50:18 +00002141 // We don't want lookup warnings at this point.
2142 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002143
John McCallf7a1a742009-11-24 19:00:30 +00002144 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002145 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002146 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002147 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002148 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002149 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002150
2151 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002152}
2153
John McCallf7a1a742009-11-24 19:00:30 +00002154// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002155ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002156Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002157 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002158 const TemplateArgumentListInfo &TemplateArgs) {
2159 DeclContext *DC;
2160 if (!(DC = computeDeclContext(SS, false)) ||
2161 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002162 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002163 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002164
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002165 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002166 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002167 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2168 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002169
John McCallf7a1a742009-11-24 19:00:30 +00002170 if (R.isAmbiguous())
2171 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002172
John McCallf7a1a742009-11-24 19:00:30 +00002173 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002174 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2175 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002176 return ExprError();
2177 }
2178
2179 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002180 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2181 << (NestedNameSpecifier*) SS.getScopeRep()
2182 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002183 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2184 return ExprError();
2185 }
2186
2187 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002188}
2189
Douglas Gregorc45c2322009-03-31 00:43:58 +00002190/// \brief Form a dependent template name.
2191///
2192/// This action forms a dependent template name given the template
2193/// name and its (presumably dependent) scope specifier. For
2194/// example, given "MetaFun::template apply", the scope specifier \p
2195/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2196/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002197TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002198 SourceLocation TemplateKWLoc,
2199 CXXScopeSpec &SS,
2200 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002201 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002202 bool EnteringContext,
2203 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002204 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2205 !getLangOptions().CPlusPlus0x)
2206 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002207 << FixItHint::CreateRemoval(TemplateKWLoc);
2208
Douglas Gregor0707bc52010-01-19 16:01:07 +00002209 DeclContext *LookupCtx = 0;
2210 if (SS.isSet())
2211 LookupCtx = computeDeclContext(SS, EnteringContext);
2212 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002213 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002214 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002215 // C++0x [temp.names]p5:
2216 // If a name prefixed by the keyword template is not the name of
2217 // a template, the program is ill-formed. [Note: the keyword
2218 // template may not be applied to non-template members of class
2219 // templates. -end note ] [ Note: as is the case with the
2220 // typename prefix, the template prefix is allowed in cases
2221 // where it is not strictly necessary; i.e., when the
2222 // nested-name-specifier or the expression on the left of the ->
2223 // or . is not dependent on a template-parameter, or the use
2224 // does not appear in the scope of a template. -end note]
2225 //
2226 // Note: C++03 was more strict here, because it banned the use of
2227 // the "template" keyword prior to a template-name that was not a
2228 // dependent name. C++ DR468 relaxed this requirement (the
2229 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002230 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002231 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002232 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2233 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002234 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002235 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2236 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002237 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2238 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002239 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002240 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002241 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002242 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002243 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002244 << Name.getSourceRange()
2245 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002246 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002247 } else {
2248 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002249 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002250 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002251 }
2252
Mike Stump1eb44332009-09-09 15:08:12 +00002253 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002254 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002255
Douglas Gregor014e88d2009-11-03 23:16:33 +00002256 switch (Name.getKind()) {
2257 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002258 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002259 Name.Identifier));
2260 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002261
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002262 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002263 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002264 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002265 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002266
2267 case UnqualifiedId::IK_LiteralOperatorId:
2268 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2269
Douglas Gregor014e88d2009-11-03 23:16:33 +00002270 default:
2271 break;
2272 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002273
2274 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002275 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002276 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002277 << Name.getSourceRange()
2278 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002279 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002280}
2281
Mike Stump1eb44332009-09-09 15:08:12 +00002282bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002283 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002284 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002285 const TemplateArgument &Arg = AL.getArgument();
2286
Anders Carlsson436b1562009-06-13 00:33:33 +00002287 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002288 switch(Arg.getKind()) {
2289 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002290 // C++ [temp.arg.type]p1:
2291 // A template-argument for a template-parameter which is a
2292 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002293 break;
2294 case TemplateArgument::Template: {
2295 // We have a template type parameter but the template argument
2296 // is a template without any arguments.
2297 SourceRange SR = AL.getSourceRange();
2298 TemplateName Name = Arg.getAsTemplate();
2299 Diag(SR.getBegin(), diag::err_template_missing_args)
2300 << Name << SR;
2301 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2302 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002303
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002304 return true;
2305 }
2306 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002307 // We have a template type parameter but the template argument
2308 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002309 SourceRange SR = AL.getSourceRange();
2310 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002311 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Anders Carlsson436b1562009-06-13 00:33:33 +00002313 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002314 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002315 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002316
John McCalla93c9342009-12-07 02:54:59 +00002317 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002318 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002319
Anders Carlsson436b1562009-06-13 00:33:33 +00002320 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002321 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002322 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002323 return false;
2324}
2325
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002326/// \brief Substitute template arguments into the default template argument for
2327/// the given template type parameter.
2328///
2329/// \param SemaRef the semantic analysis object for which we are performing
2330/// the substitution.
2331///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002332/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002333/// for.
2334///
2335/// \param TemplateLoc the location of the template name that started the
2336/// template-id we are checking.
2337///
2338/// \param RAngleLoc the location of the right angle bracket ('>') that
2339/// terminates the template-id.
2340///
2341/// \param Param the template template parameter whose default we are
2342/// substituting into.
2343///
2344/// \param Converted the list of template arguments provided for template
2345/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002346/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002347static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002348SubstDefaultTemplateArgument(Sema &SemaRef,
2349 TemplateDecl *Template,
2350 SourceLocation TemplateLoc,
2351 SourceLocation RAngleLoc,
2352 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002353 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002354 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002355
2356 // If the argument type is dependent, instantiate it now based
2357 // on the previously-computed template arguments.
2358 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002359 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002360 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002361
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002362 MultiLevelTemplateArgumentList AllTemplateArgs
2363 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2364
2365 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002366 Template, Converted.data(),
2367 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002368 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002369
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002370 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2371 Param->getDefaultArgumentLoc(),
2372 Param->getDeclName());
2373 }
2374
2375 return ArgType;
2376}
2377
2378/// \brief Substitute template arguments into the default template argument for
2379/// the given non-type template parameter.
2380///
2381/// \param SemaRef the semantic analysis object for which we are performing
2382/// the substitution.
2383///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002384/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002385/// for.
2386///
2387/// \param TemplateLoc the location of the template name that started the
2388/// template-id we are checking.
2389///
2390/// \param RAngleLoc the location of the right angle bracket ('>') that
2391/// terminates the template-id.
2392///
Douglas Gregor788cd062009-11-11 01:00:40 +00002393/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002394/// substituting into.
2395///
2396/// \param Converted the list of template arguments provided for template
2397/// parameters that precede \p Param in the template parameter list.
2398///
2399/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002400static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002401SubstDefaultTemplateArgument(Sema &SemaRef,
2402 TemplateDecl *Template,
2403 SourceLocation TemplateLoc,
2404 SourceLocation RAngleLoc,
2405 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002406 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002407 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002408 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002409
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002410 MultiLevelTemplateArgumentList AllTemplateArgs
2411 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002412
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002413 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002414 Template, Converted.data(),
2415 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002416 SourceRange(TemplateLoc, RAngleLoc));
2417
2418 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2419}
2420
Douglas Gregor788cd062009-11-11 01:00:40 +00002421/// \brief Substitute template arguments into the default template argument for
2422/// the given template template parameter.
2423///
2424/// \param SemaRef the semantic analysis object for which we are performing
2425/// the substitution.
2426///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002427/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002428/// for.
2429///
2430/// \param TemplateLoc the location of the template name that started the
2431/// template-id we are checking.
2432///
2433/// \param RAngleLoc the location of the right angle bracket ('>') that
2434/// terminates the template-id.
2435///
2436/// \param Param the template template parameter whose default we are
2437/// substituting into.
2438///
2439/// \param Converted the list of template arguments provided for template
2440/// parameters that precede \p Param in the template parameter list.
2441///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002442/// \param QualifierLoc Will be set to the nested-name-specifier (with
2443/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002444///
Douglas Gregor788cd062009-11-11 01:00:40 +00002445/// \returns the substituted template argument, or NULL if an error occurred.
2446static TemplateName
2447SubstDefaultTemplateArgument(Sema &SemaRef,
2448 TemplateDecl *Template,
2449 SourceLocation TemplateLoc,
2450 SourceLocation RAngleLoc,
2451 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002452 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2453 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002454 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002455 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002456
Douglas Gregor788cd062009-11-11 01:00:40 +00002457 MultiLevelTemplateArgumentList AllTemplateArgs
2458 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002459
Douglas Gregor788cd062009-11-11 01:00:40 +00002460 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002461 Template, Converted.data(),
2462 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002463 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002464
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002465 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002466 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002467 if (QualifierLoc) {
2468 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2469 AllTemplateArgs);
2470 if (!QualifierLoc)
2471 return TemplateName();
2472 }
2473
Douglas Gregor1d752d72011-03-02 18:46:51 +00002474 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002475 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002476 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002477 AllTemplateArgs);
2478}
2479
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002480/// \brief If the given template parameter has a default template
2481/// argument, substitute into that default template argument and
2482/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002483TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002484Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2485 SourceLocation TemplateLoc,
2486 SourceLocation RAngleLoc,
2487 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002488 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2489 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002490 if (!TypeParm->hasDefaultArgument())
2491 return TemplateArgumentLoc();
2492
John McCalla93c9342009-12-07 02:54:59 +00002493 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002494 TemplateLoc,
2495 RAngleLoc,
2496 TypeParm,
2497 Converted);
2498 if (DI)
2499 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2500
2501 return TemplateArgumentLoc();
2502 }
2503
2504 if (NonTypeTemplateParmDecl *NonTypeParm
2505 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2506 if (!NonTypeParm->hasDefaultArgument())
2507 return TemplateArgumentLoc();
2508
John McCall60d7b3a2010-08-24 06:29:42 +00002509 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002510 TemplateLoc,
2511 RAngleLoc,
2512 NonTypeParm,
2513 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002514 if (Arg.isInvalid())
2515 return TemplateArgumentLoc();
2516
2517 Expr *ArgE = Arg.takeAs<Expr>();
2518 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2519 }
2520
2521 TemplateTemplateParmDecl *TempTempParm
2522 = cast<TemplateTemplateParmDecl>(Param);
2523 if (!TempTempParm->hasDefaultArgument())
2524 return TemplateArgumentLoc();
2525
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002526
Douglas Gregor1d752d72011-03-02 18:46:51 +00002527 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002528 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002529 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002530 RAngleLoc,
2531 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002532 Converted,
2533 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002534 if (TName.isNull())
2535 return TemplateArgumentLoc();
2536
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002537 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002538 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002539 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2540}
2541
Douglas Gregore7526412009-11-11 19:31:23 +00002542/// \brief Check that the given template argument corresponds to the given
2543/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002544///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002545/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002546/// checked.
2547///
2548/// \param Arg The template argument.
2549///
2550/// \param Template The template in which the template argument resides.
2551///
2552/// \param TemplateLoc The location of the template name for the template
2553/// whose argument list we're matching.
2554///
2555/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2556/// the template argument list.
2557///
2558/// \param ArgumentPackIndex The index into the argument pack where this
2559/// argument will be placed. Only valid if the parameter is a parameter pack.
2560///
2561/// \param Converted The checked, converted argument will be added to the
2562/// end of this small vector.
2563///
2564/// \param CTAK Describes how we arrived at this particular template argument:
2565/// explicitly written, deduced, etc.
2566///
2567/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002568bool Sema::CheckTemplateArgument(NamedDecl *Param,
2569 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002570 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002571 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002572 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002573 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002574 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002575 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002576 // Check template type parameters.
2577 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002578 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002579
Douglas Gregord9e15302009-11-11 19:41:09 +00002580 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002581 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002582 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002583 // with the template arguments we've seen thus far. But if the
2584 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002585 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002586 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2587 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002588
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002589 if (NTTPType->isDependentType() &&
2590 !isa<TemplateTemplateParmDecl>(Template) &&
2591 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002592 // Do substitution on the type of the non-type template parameter.
2593 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002594 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002595 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002596
2597 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002598 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002599 NTTPType = SubstType(NTTPType,
2600 MultiLevelTemplateArgumentList(TemplateArgs),
2601 NTTP->getLocation(),
2602 NTTP->getDeclName());
2603 // If that worked, check the non-type template parameter type
2604 // for validity.
2605 if (!NTTPType.isNull())
2606 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2607 NTTP->getLocation());
2608 if (NTTPType.isNull())
2609 return true;
2610 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002611
Douglas Gregore7526412009-11-11 19:31:23 +00002612 switch (Arg.getArgument().getKind()) {
2613 case TemplateArgument::Null:
2614 assert(false && "Should never see a NULL template argument here");
2615 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002616
Douglas Gregore7526412009-11-11 19:31:23 +00002617 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002618 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002619 ExprResult Res =
2620 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2621 Result, CTAK);
2622 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002623 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002624
Douglas Gregor910f8002010-11-07 23:05:16 +00002625 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002626 break;
2627 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002628
Douglas Gregore7526412009-11-11 19:31:23 +00002629 case TemplateArgument::Declaration:
2630 case TemplateArgument::Integral:
2631 // We've already checked this template argument, so just copy
2632 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002633 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002634 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002635
Douglas Gregore7526412009-11-11 19:31:23 +00002636 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002637 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002638 // We were given a template template argument. It may not be ill-formed;
2639 // see below.
2640 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002641 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2642 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002643 // We have a template argument such as \c T::template X, which we
2644 // parsed as a template template argument. However, since we now
2645 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002646 // template name into an expression.
2647
2648 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2649 Arg.getTemplateNameLoc());
2650
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002651 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002652 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002653 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002654 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002655 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002656
Douglas Gregora7fc9012011-01-05 18:58:31 +00002657 // If we parsed the template argument as a pack expansion, create a
2658 // pack expansion expression.
2659 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002660 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2661 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002662 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002663 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002664
Douglas Gregore7526412009-11-11 19:31:23 +00002665 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002666 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2667 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002668 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002669
Douglas Gregor910f8002010-11-07 23:05:16 +00002670 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002671 break;
2672 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002673
Douglas Gregore7526412009-11-11 19:31:23 +00002674 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002675 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002676 // therefore cannot be a non-type template argument.
2677 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2678 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002679
Douglas Gregore7526412009-11-11 19:31:23 +00002680 Diag(Param->getLocation(), diag::note_template_param_here);
2681 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002682
Douglas Gregore7526412009-11-11 19:31:23 +00002683 case TemplateArgument::Type: {
2684 // We have a non-type template parameter but the template
2685 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002686
Douglas Gregore7526412009-11-11 19:31:23 +00002687 // C++ [temp.arg]p2:
2688 // In a template-argument, an ambiguity between a type-id and
2689 // an expression is resolved to a type-id, regardless of the
2690 // form of the corresponding template-parameter.
2691 //
2692 // We warn specifically about this case, since it can be rather
2693 // confusing for users.
2694 QualType T = Arg.getArgument().getAsType();
2695 SourceRange SR = Arg.getSourceRange();
2696 if (T->isFunctionType())
2697 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2698 else
2699 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2700 Diag(Param->getLocation(), diag::note_template_param_here);
2701 return true;
2702 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002703
Douglas Gregore7526412009-11-11 19:31:23 +00002704 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002705 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002706 break;
2707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002708
Douglas Gregore7526412009-11-11 19:31:23 +00002709 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002710 }
2711
2712
Douglas Gregore7526412009-11-11 19:31:23 +00002713 // Check template template parameters.
2714 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002715
Douglas Gregore7526412009-11-11 19:31:23 +00002716 // Substitute into the template parameter list of the template
2717 // template parameter, since previously-supplied template arguments
2718 // may appear within the template template parameter.
2719 {
2720 // Set up a template instantiation context.
2721 LocalInstantiationScope Scope(*this);
2722 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002723 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002724 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002725
2726 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002727 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002728 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002729 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002730 MultiLevelTemplateArgumentList(TemplateArgs)));
2731 if (!TempParm)
2732 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002733 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002734
Douglas Gregore7526412009-11-11 19:31:23 +00002735 switch (Arg.getArgument().getKind()) {
2736 case TemplateArgument::Null:
2737 assert(false && "Should never see a NULL template argument here");
2738 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002739
Douglas Gregore7526412009-11-11 19:31:23 +00002740 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002741 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002742 if (CheckTemplateArgument(TempParm, Arg))
2743 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002744
Douglas Gregor910f8002010-11-07 23:05:16 +00002745 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002746 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002747
Douglas Gregore7526412009-11-11 19:31:23 +00002748 case TemplateArgument::Expression:
2749 case TemplateArgument::Type:
2750 // We have a template template parameter but the template
2751 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002752 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2753 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002754 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002755
Douglas Gregore7526412009-11-11 19:31:23 +00002756 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002757 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002758 "Declaration argument with template template parameter");
2759 break;
2760 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002761 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002762 "Integral argument with template template parameter");
2763 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002764
Douglas Gregore7526412009-11-11 19:31:23 +00002765 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002766 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002767 break;
2768 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002769
Douglas Gregore7526412009-11-11 19:31:23 +00002770 return false;
2771}
2772
Douglas Gregorc15cb382009-02-09 23:23:08 +00002773/// \brief Check that the given template argument list is well-formed
2774/// for specializing the given template.
2775bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2776 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002777 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002778 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002779 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002780 TemplateParameterList *Params = Template->getTemplateParameters();
2781 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002782 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002783 bool Invalid = false;
2784
John McCalld5532b62009-11-23 01:53:49 +00002785 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2786
Mike Stump1eb44332009-09-09 15:08:12 +00002787 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002788 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002789
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002790 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002791 (NumArgs < Params->getMinRequiredArguments() &&
2792 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002793 // FIXME: point at either the first arg beyond what we can handle,
2794 // or the '>', depending on whether we have too many or too few
2795 // arguments.
2796 SourceRange Range;
2797 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002798 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002799 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2800 << (NumArgs > NumParams)
2801 << (isa<ClassTemplateDecl>(Template)? 0 :
2802 isa<FunctionTemplateDecl>(Template)? 1 :
2803 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2804 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002805 Diag(Template->getLocation(), diag::note_template_decl_here)
2806 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002807 Invalid = true;
2808 }
Mike Stump1eb44332009-09-09 15:08:12 +00002809
2810 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002811 // [...] The type and form of each template-argument specified in
2812 // a template-id shall match the type and form specified for the
2813 // corresponding parameter declared by the template in its
2814 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002815 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002816 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2817 TemplateParameterList::iterator Param = Params->begin(),
2818 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002819 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002820 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002821 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002822 if (ArgIdx > NumArgs && PartialTemplateArgs)
2823 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002824
Douglas Gregorf35f8282009-11-11 21:54:23 +00002825 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002826 // If we have an expanded parameter pack, make sure we don't have too
2827 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002828 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002829 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002830 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002831 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2832 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2833 << true
2834 << (isa<ClassTemplateDecl>(Template)? 0 :
2835 isa<FunctionTemplateDecl>(Template)? 1 :
2836 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2837 << Template;
2838 Diag(Template->getLocation(), diag::note_template_decl_here)
2839 << Params->getSourceRange();
2840 return true;
2841 }
2842 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002843
Douglas Gregorf35f8282009-11-11 21:54:23 +00002844 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002845 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2846 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002847 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002848 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002849
Douglas Gregor14be16b2010-12-20 16:57:52 +00002850 if ((*Param)->isTemplateParameterPack()) {
2851 // The template parameter was a template parameter pack, so take the
2852 // deduced argument and place it on the argument pack. Note that we
2853 // stay on the same template parameter so that we can deduce more
2854 // arguments.
2855 ArgumentPack.push_back(Converted.back());
2856 Converted.pop_back();
2857 } else {
2858 // Move to the next template parameter.
2859 ++Param;
2860 }
2861 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002862 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002863 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002864
2865 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002866 // arguments, just break out now and we'll fill in the argument pack below.
2867 if ((*Param)->isTemplateParameterPack())
2868 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002869
Douglas Gregorf35f8282009-11-11 21:54:23 +00002870 // We have a default template argument that we will use.
2871 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002872
Douglas Gregorf35f8282009-11-11 21:54:23 +00002873 // Retrieve the default template argument from the template
2874 // parameter. For each kind of template parameter, we substitute the
2875 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002876 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002877 // the default argument.
2878 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2879 if (!TTP->hasDefaultArgument()) {
2880 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2881 break;
2882 }
2883
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002884 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002885 Template,
2886 TemplateLoc,
2887 RAngleLoc,
2888 TTP,
2889 Converted);
2890 if (!ArgType)
2891 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002892
Douglas Gregorf35f8282009-11-11 21:54:23 +00002893 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2894 ArgType);
2895 } else if (NonTypeTemplateParmDecl *NTTP
2896 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2897 if (!NTTP->hasDefaultArgument()) {
2898 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2899 break;
2900 }
2901
John McCall60d7b3a2010-08-24 06:29:42 +00002902 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002903 TemplateLoc,
2904 RAngleLoc,
2905 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002906 Converted);
2907 if (E.isInvalid())
2908 return true;
2909
2910 Expr *Ex = E.takeAs<Expr>();
2911 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2912 } else {
2913 TemplateTemplateParmDecl *TempParm
2914 = cast<TemplateTemplateParmDecl>(*Param);
2915
2916 if (!TempParm->hasDefaultArgument()) {
2917 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2918 break;
2919 }
2920
Douglas Gregor1d752d72011-03-02 18:46:51 +00002921 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002922 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002923 TemplateLoc,
2924 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002925 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002926 Converted,
2927 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002928 if (Name.isNull())
2929 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002930
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002931 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2932 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002933 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002934
Douglas Gregorf35f8282009-11-11 21:54:23 +00002935 // Introduce an instantiation record that describes where we are using
2936 // the default template argument.
2937 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002938 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002939 SourceRange(TemplateLoc, RAngleLoc));
2940
Douglas Gregorf35f8282009-11-11 21:54:23 +00002941 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002942 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002943 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002944 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002945
Douglas Gregor67714232011-03-03 02:41:12 +00002946 // Core issue 150 (assumed resolution): if this is a template template
2947 // parameter, keep track of the default template arguments from the
2948 // template definition.
2949 if (isTemplateTemplateParameter)
2950 TemplateArgs.addArgument(Arg);
2951
Douglas Gregor14be16b2010-12-20 16:57:52 +00002952 // Move to the next template parameter and argument.
2953 ++Param;
2954 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002955 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002956
Douglas Gregor14be16b2010-12-20 16:57:52 +00002957 // Form argument packs for each of the parameter packs remaining.
2958 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002959 // If we're checking a partial list of template arguments, don't fill
2960 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002961
2962 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002963 if (PartialTemplateArgs && ArgumentPack.empty()) {
2964 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002965 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002966 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002967 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002968 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2969 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002970 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002971 ArgumentPack.clear();
2972 }
2973 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002974
Douglas Gregor14be16b2010-12-20 16:57:52 +00002975 ++Param;
2976 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002977
Douglas Gregorc15cb382009-02-09 23:23:08 +00002978 return Invalid;
2979}
2980
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002981namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002982 class UnnamedLocalNoLinkageFinder
2983 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002984 {
2985 Sema &S;
2986 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002987
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002988 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002989
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002990 public:
2991 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2992
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002993 bool Visit(QualType T) {
2994 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002995 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002996
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002997#define TYPE(Class, Parent) \
2998 bool Visit##Class##Type(const Class##Type *);
2999#define ABSTRACT_TYPE(Class, Parent) \
3000 bool Visit##Class##Type(const Class##Type *) { return false; }
3001#define NON_CANONICAL_TYPE(Class, Parent) \
3002 bool Visit##Class##Type(const Class##Type *) { return false; }
3003#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003004
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003005 bool VisitTagDecl(const TagDecl *Tag);
3006 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3007 };
3008}
3009
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003010bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003011 return false;
3012}
3013
3014bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3015 return Visit(T->getElementType());
3016}
3017
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003018bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003019 return Visit(T->getPointeeType());
3020}
3021
3022bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003023 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003024 return Visit(T->getPointeeType());
3025}
3026
3027bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003028 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003029 return Visit(T->getPointeeType());
3030}
3031
3032bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003033 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003034 return Visit(T->getPointeeType());
3035}
3036
3037bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003038 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003039 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3040}
3041
3042bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003043 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003044 return Visit(T->getElementType());
3045}
3046
3047bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003048 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003049 return Visit(T->getElementType());
3050}
3051
3052bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003053 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003054 return Visit(T->getElementType());
3055}
3056
3057bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003058 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003059 return Visit(T->getElementType());
3060}
3061
3062bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003063 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003064 return Visit(T->getElementType());
3065}
3066
3067bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3068 return Visit(T->getElementType());
3069}
3070
3071bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3072 return Visit(T->getElementType());
3073}
3074
3075bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3076 const FunctionProtoType* T) {
3077 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003078 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003079 A != AEnd; ++A) {
3080 if (Visit(*A))
3081 return true;
3082 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003083
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003084 return Visit(T->getResultType());
3085}
3086
3087bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3088 const FunctionNoProtoType* T) {
3089 return Visit(T->getResultType());
3090}
3091
3092bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3093 const UnresolvedUsingType*) {
3094 return false;
3095}
3096
3097bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3098 return false;
3099}
3100
3101bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3102 return Visit(T->getUnderlyingType());
3103}
3104
3105bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3106 return false;
3107}
3108
Sean Huntca63c202011-05-24 22:41:36 +00003109bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3110 const UnaryTransformType*) {
3111 return false;
3112}
3113
Richard Smith34b41d92011-02-20 03:19:35 +00003114bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3115 return Visit(T->getDeducedType());
3116}
3117
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003118bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3119 return VisitTagDecl(T->getDecl());
3120}
3121
3122bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3123 return VisitTagDecl(T->getDecl());
3124}
3125
3126bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3127 const TemplateTypeParmType*) {
3128 return false;
3129}
3130
Douglas Gregorc3069d62011-01-14 02:55:32 +00003131bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3132 const SubstTemplateTypeParmPackType *) {
3133 return false;
3134}
3135
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003136bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3137 const TemplateSpecializationType*) {
3138 return false;
3139}
3140
3141bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3142 const InjectedClassNameType* T) {
3143 return VisitTagDecl(T->getDecl());
3144}
3145
3146bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3147 const DependentNameType* T) {
3148 return VisitNestedNameSpecifier(T->getQualifier());
3149}
3150
3151bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3152 const DependentTemplateSpecializationType* T) {
3153 return VisitNestedNameSpecifier(T->getQualifier());
3154}
3155
Douglas Gregor7536dd52010-12-20 02:24:11 +00003156bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3157 const PackExpansionType* T) {
3158 return Visit(T->getPattern());
3159}
3160
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003161bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3162 return false;
3163}
3164
3165bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3166 const ObjCInterfaceType *) {
3167 return false;
3168}
3169
3170bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3171 const ObjCObjectPointerType *) {
3172 return false;
3173}
3174
3175bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3176 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3177 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3178 << S.Context.getTypeDeclType(Tag) << SR;
3179 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003180 }
3181
Richard Smith162e1c12011-04-15 14:24:37 +00003182 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003183 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3184 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3185 return true;
3186 }
3187
3188 return false;
3189}
3190
3191bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3192 NestedNameSpecifier *NNS) {
3193 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3194 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003195
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003196 switch (NNS->getKind()) {
3197 case NestedNameSpecifier::Identifier:
3198 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003199 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003200 case NestedNameSpecifier::Global:
3201 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003202
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003203 case NestedNameSpecifier::TypeSpec:
3204 case NestedNameSpecifier::TypeSpecWithTemplate:
3205 return Visit(QualType(NNS->getAsType(), 0));
3206 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003207 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003208}
3209
3210
Douglas Gregorc15cb382009-02-09 23:23:08 +00003211/// \brief Check a template argument against its corresponding
3212/// template type parameter.
3213///
3214/// This routine implements the semantics of C++ [temp.arg.type]. It
3215/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003216bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003217 TypeSourceInfo *ArgInfo) {
3218 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003219 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003220 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003221
3222 if (Arg->isVariablyModifiedType()) {
3223 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003224 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003225 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003226 }
3227
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003228 // C++03 [temp.arg.type]p2:
3229 // A local type, a type with no linkage, an unnamed type or a type
3230 // compounded from any of these types shall not be used as a
3231 // template-argument for a template type-parameter.
3232 //
3233 // C++0x allows these, and even in C++03 we allow them as an extension with
3234 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003235 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003236 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3237 (void)Finder.Visit(Context.getCanonicalType(Arg));
3238 }
3239
Douglas Gregorc15cb382009-02-09 23:23:08 +00003240 return false;
3241}
3242
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003243/// \brief Checks whether the given template argument is the address
3244/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003245static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003246CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3247 NonTypeTemplateParmDecl *Param,
3248 QualType ParamType,
3249 Expr *ArgIn,
3250 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003251 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003252 Expr *Arg = ArgIn;
3253 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003254
3255 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003256 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003257 Arg = Cast->getSubExpr();
3258
3259 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003260 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003261 // A template-argument for a non-type, non-template
3262 // template-parameter shall be one of: [...]
3263 //
3264 // -- the address of an object or function with external
3265 // linkage, including function templates and function
3266 // template-ids but excluding non-static class members,
3267 // expressed as & id-expression where the & is optional if
3268 // the name refers to a function or array, or if the
3269 // corresponding template-parameter is a reference; or
3270 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003272 // In C++98/03 mode, give an extension warning on any extra parentheses.
3273 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3274 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003275 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003276 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003277 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003278 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003279 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003280 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003281 }
3282
3283 Arg = Parens->getSubExpr();
3284 }
3285
Douglas Gregorb7a09262010-04-01 18:32:35 +00003286 bool AddressTaken = false;
3287 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003288 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003289 if (UnOp->getOpcode() == UO_AddrOf) {
Francois Pichet11f98d02011-04-28 05:12:34 +00003290 // Support &__uuidof(class_with_uuid) as a non-type template argument.
3291 // Very common in Microsoft COM headers.
3292 if (S.getLangOptions().Microsoft &&
3293 isa<CXXUuidofExpr>(UnOp->getSubExpr())) {
3294 Converted = TemplateArgument(ArgIn);
3295 return false;
3296 }
3297
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003298 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003299 AddressTaken = true;
3300 AddrOpLoc = UnOp->getOperatorLoc();
3301 }
Francois Picheta343a412011-04-29 09:08:14 +00003302 } else {
3303 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3304 Converted = TemplateArgument(ArgIn);
3305 return false;
3306 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003307 DRE = dyn_cast<DeclRefExpr>(Arg);
Francois Picheta343a412011-04-29 09:08:14 +00003308 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003309 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003310 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3311 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003312 S.Diag(Param->getLocation(), diag::note_template_param_here);
3313 return true;
3314 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003315
3316 // Stop checking the precise nature of the argument if it is value dependent,
3317 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003318 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003319 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003320 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003321 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003322
Douglas Gregorb7a09262010-04-01 18:32:35 +00003323 if (!isa<ValueDecl>(DRE->getDecl())) {
3324 S.Diag(Arg->getSourceRange().getBegin(),
3325 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003326 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003327 S.Diag(Param->getLocation(), diag::note_template_param_here);
3328 return true;
3329 }
3330
3331 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003332
3333 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003334 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3335 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003336 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003337 S.Diag(Param->getLocation(), diag::note_template_param_here);
3338 return true;
3339 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003340
3341 // Cannot refer to non-static member functions
3342 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003343 if (!Method->isStatic()) {
3344 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003345 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003346 S.Diag(Param->getLocation(), diag::note_template_param_here);
3347 return true;
3348 }
Mike Stump1eb44332009-09-09 15:08:12 +00003349
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003350 // Functions must have external linkage.
3351 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003352 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003353 S.Diag(Arg->getSourceRange().getBegin(),
3354 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003355 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003356 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003357 << true;
3358 return true;
3359 }
3360
3361 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003362 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003363
Douglas Gregorb7a09262010-04-01 18:32:35 +00003364 // If the template parameter has pointer type, the function decays.
3365 if (ParamType->isPointerType() && !AddressTaken)
3366 ArgType = S.Context.getPointerType(Func->getType());
3367 else if (AddressTaken && ParamType->isReferenceType()) {
3368 // If we originally had an address-of operator, but the
3369 // parameter has reference type, complain and (if things look
3370 // like they will work) drop the address-of operator.
3371 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3372 ParamType.getNonReferenceType())) {
3373 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3374 << ParamType;
3375 S.Diag(Param->getLocation(), diag::note_template_param_here);
3376 return true;
3377 }
3378
3379 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3380 << ParamType
3381 << FixItHint::CreateRemoval(AddrOpLoc);
3382 S.Diag(Param->getLocation(), diag::note_template_param_here);
3383
3384 ArgType = Func->getType();
3385 }
3386 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003387 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003388 S.Diag(Arg->getSourceRange().getBegin(),
3389 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003390 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003391 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003392 << true;
3393 return true;
3394 }
3395
Douglas Gregorb7a09262010-04-01 18:32:35 +00003396 // A value of reference type is not an object.
3397 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003398 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003399 diag::err_template_arg_reference_var)
3400 << Var->getType() << Arg->getSourceRange();
3401 S.Diag(Param->getLocation(), diag::note_template_param_here);
3402 return true;
3403 }
3404
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003405 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003406 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003407
3408 // If the template parameter has pointer type, we must have taken
3409 // the address of this object.
3410 if (ParamType->isReferenceType()) {
3411 if (AddressTaken) {
3412 // If we originally had an address-of operator, but the
3413 // parameter has reference type, complain and (if things look
3414 // like they will work) drop the address-of operator.
3415 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3416 ParamType.getNonReferenceType())) {
3417 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3418 << ParamType;
3419 S.Diag(Param->getLocation(), diag::note_template_param_here);
3420 return true;
3421 }
3422
3423 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3424 << ParamType
3425 << FixItHint::CreateRemoval(AddrOpLoc);
3426 S.Diag(Param->getLocation(), diag::note_template_param_here);
3427
3428 ArgType = Var->getType();
3429 }
3430 } else if (!AddressTaken && ParamType->isPointerType()) {
3431 if (Var->getType()->isArrayType()) {
3432 // Array-to-pointer decay.
3433 ArgType = S.Context.getArrayDecayedType(Var->getType());
3434 } else {
3435 // If the template parameter has pointer type but the address of
3436 // this object was not taken, complain and (possibly) recover by
3437 // taking the address of the entity.
3438 ArgType = S.Context.getPointerType(Var->getType());
3439 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3440 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3441 << ParamType;
3442 S.Diag(Param->getLocation(), diag::note_template_param_here);
3443 return true;
3444 }
3445
3446 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3447 << ParamType
3448 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3449
3450 S.Diag(Param->getLocation(), diag::note_template_param_here);
3451 }
3452 }
3453 } else {
3454 // We found something else, but we don't know specifically what it is.
3455 S.Diag(Arg->getSourceRange().getBegin(),
3456 diag::err_template_arg_not_object_or_func)
3457 << Arg->getSourceRange();
3458 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3459 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003460 }
Mike Stump1eb44332009-09-09 15:08:12 +00003461
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003462 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003463 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003464 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003465 // For pointer-to-object types, qualification conversions are
3466 // permitted.
3467 } else {
3468 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3469 if (!ParamRef->getPointeeType()->isFunctionType()) {
3470 // C++ [temp.arg.nontype]p5b3:
3471 // For a non-type template-parameter of type reference to
3472 // object, no conversions apply. The type referred to by the
3473 // reference may be more cv-qualified than the (otherwise
3474 // identical) type of the template- argument. The
3475 // template-parameter is bound directly to the
3476 // template-argument, which shall be an lvalue.
3477
3478 // FIXME: Other qualifiers?
3479 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3480 unsigned ArgQuals = ArgType.getCVRQualifiers();
3481
3482 if ((ParamQuals | ArgQuals) != ParamQuals) {
3483 S.Diag(Arg->getSourceRange().getBegin(),
3484 diag::err_template_arg_ref_bind_ignores_quals)
3485 << ParamType << Arg->getType()
3486 << Arg->getSourceRange();
3487 S.Diag(Param->getLocation(), diag::note_template_param_here);
3488 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003489 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003490 }
3491 }
3492
3493 // At this point, the template argument refers to an object or
3494 // function with external linkage. We now need to check whether the
3495 // argument and parameter types are compatible.
3496 if (!S.Context.hasSameUnqualifiedType(ArgType,
3497 ParamType.getNonReferenceType())) {
3498 // We can't perform this conversion or binding.
3499 if (ParamType->isReferenceType())
3500 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3501 << ParamType << Arg->getType() << Arg->getSourceRange();
3502 else
3503 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3504 << Arg->getType() << ParamType << Arg->getSourceRange();
3505 S.Diag(Param->getLocation(), diag::note_template_param_here);
3506 return true;
3507 }
3508 }
3509
3510 // Create the template argument.
3511 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003512 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003513 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003514}
3515
3516/// \brief Checks whether the given template argument is a pointer to
3517/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003518bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003519 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003520 bool Invalid = false;
3521
3522 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003523 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003524 Arg = Cast->getSubExpr();
3525
3526 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003527 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003528 // A template-argument for a non-type, non-template
3529 // template-parameter shall be one of: [...]
3530 //
3531 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003532 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003533
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003534 // In C++98/03 mode, give an extension warning on any extra parentheses.
3535 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3536 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003537 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003538 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003539 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003540 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003541 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003542 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003543 }
3544
3545 Arg = Parens->getSubExpr();
3546 }
3547
Douglas Gregorcaddba02009-11-12 18:38:13 +00003548 // A pointer-to-member constant written &Class::member.
3549 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003550 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003551 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3552 if (DRE && !DRE->getQualifier())
3553 DRE = 0;
3554 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003555 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003556 // A constant of pointer-to-member type.
3557 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3558 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3559 if (VD->getType()->isMemberPointerType()) {
3560 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003561 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003562 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3563 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003564 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003565 else
3566 Converted = TemplateArgument(VD->getCanonicalDecl());
3567 return Invalid;
3568 }
3569 }
3570 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003571
Douglas Gregorcaddba02009-11-12 18:38:13 +00003572 DRE = 0;
3573 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003574
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003575 if (!DRE)
3576 return Diag(Arg->getSourceRange().getBegin(),
3577 diag::err_template_arg_not_pointer_to_member_form)
3578 << Arg->getSourceRange();
3579
3580 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3581 assert((isa<FieldDecl>(DRE->getDecl()) ||
3582 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3583 "Only non-static member pointers can make it here");
3584
3585 // Okay: this is the address of a non-static member, and therefore
3586 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003587 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003588 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003589 else
3590 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003591 return Invalid;
3592 }
3593
3594 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003595 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003596 diag::err_template_arg_not_pointer_to_member_form)
3597 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003598 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003599 diag::note_template_arg_refers_here);
3600 return true;
3601}
3602
Douglas Gregorc15cb382009-02-09 23:23:08 +00003603/// \brief Check a template argument against its corresponding
3604/// non-type template parameter.
3605///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003606/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003607/// If an error occurred, it returns ExprError(); otherwise, it
3608/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003609/// InstantiatedParamType is the type of the non-type template
3610/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003611ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3612 QualType InstantiatedParamType, Expr *Arg,
3613 TemplateArgument &Converted,
3614 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003615 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3616
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003617 // If either the parameter has a dependent type or the argument is
3618 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003619 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3620 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003621 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003622 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003623 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003624
3625 // C++ [temp.arg.nontype]p5:
3626 // The following conversions are performed on each expression used
3627 // as a non-type template-argument. If a non-type
3628 // template-argument cannot be converted to the type of the
3629 // corresponding template-parameter then the program is
3630 // ill-formed.
3631 //
3632 // -- for a non-type template-parameter of integral or
3633 // enumeration type, integral promotions (4.5) and integral
3634 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003635 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003636 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003637 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003638 // C++ [temp.arg.nontype]p1:
3639 // A template-argument for a non-type, non-template
3640 // template-parameter shall be one of:
3641 //
3642 // -- an integral constant-expression of integral or enumeration
3643 // type; or
3644 // -- the name of a non-type template-parameter; or
3645 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003646 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003647 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003648 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003649 diag::err_template_arg_not_integral_or_enumeral)
3650 << ArgType << Arg->getSourceRange();
3651 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003652 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003653 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003654 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003655 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3656 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003657 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003658 }
3659
Douglas Gregor02024a92010-03-28 02:42:43 +00003660 // From here on out, all we care about are the unqualified forms
3661 // of the parameter and argument types.
3662 ParamType = ParamType.getUnqualifiedType();
3663 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003664
3665 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003666 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003667 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003668 } else if (CTAK == CTAK_Deduced) {
3669 // C++ [temp.deduct.type]p17:
3670 // If, in the declaration of a function template with a non-type
3671 // template-parameter, the non-type template- parameter is used
3672 // in an expression in the function parameter-list and, if the
3673 // corresponding template-argument is deduced, the
3674 // template-argument type shall match the type of the
3675 // template-parameter exactly, except that a template-argument
3676 // deduced from an array bound may be of any integral type.
3677 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3678 << ArgType << ParamType;
3679 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003680 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003681 } else if (ParamType->isBooleanType()) {
3682 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003683 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003684 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3685 !ParamType->isEnumeralType()) {
3686 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003687 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003688 } else {
3689 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003690 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003691 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003692 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003693 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003694 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003695 }
3696
Douglas Gregorc7469372011-05-04 21:55:00 +00003697 // Add the value of this argument to the list of converted
3698 // arguments. We use the bitwidth and signedness of the template
3699 // parameter.
3700 if (Arg->isValueDependent()) {
3701 // The argument is value-dependent. Create a new
3702 // TemplateArgument with the converted expression.
3703 Converted = TemplateArgument(Arg);
3704 return Owned(Arg);
3705 }
3706
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003707 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003708 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003709 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003710
Douglas Gregorc7469372011-05-04 21:55:00 +00003711 if (ParamType->isBooleanType()) {
3712 // Value must be zero or one.
3713 Value = Value != 0;
3714 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3715 if (Value.getBitWidth() != AllowedBits)
3716 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003717 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003718 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003719 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003720
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003721 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003722 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003723 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003724 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003725 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003726 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003727
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003728 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003729 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003730 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003731 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3732 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3733 << Arg->getSourceRange();
3734 Diag(Param->getLocation(), diag::note_template_param_here);
3735 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003736
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003737 // Complain if we overflowed the template parameter's type.
3738 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003739 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003740 RequiredBits = OldValue.getActiveBits();
3741 else if (OldValue.isUnsigned())
3742 RequiredBits = OldValue.getActiveBits() + 1;
3743 else
3744 RequiredBits = OldValue.getMinSignedBits();
3745 if (RequiredBits > AllowedBits) {
3746 Diag(Arg->getSourceRange().getBegin(),
3747 diag::warn_template_arg_too_large)
3748 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3749 << Arg->getSourceRange();
3750 Diag(Param->getLocation(), diag::note_template_param_here);
3751 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003752 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003753
John McCall833ca992009-10-29 08:12:44 +00003754 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003755 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003756 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003757 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003758 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003759
John McCall6bb80172010-03-30 21:47:33 +00003760 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3761
Douglas Gregorb7a09262010-04-01 18:32:35 +00003762 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3763 // from a template argument of type std::nullptr_t to a non-type
3764 // template parameter of type pointer to object, pointer to
3765 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003766 if (ArgType->isNullPtrType()) {
3767 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3768 Converted = TemplateArgument((NamedDecl *)0);
3769 return Owned(Arg);
3770 }
3771
3772 if (ParamType->isNullPtrType()) {
3773 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3774 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3775 return Owned(Arg);
3776 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003777 }
3778
Douglas Gregorb86b0572009-02-11 01:18:59 +00003779 // Handle pointer-to-function, reference-to-function, and
3780 // pointer-to-member-function all in (roughly) the same way.
3781 if (// -- For a non-type template-parameter of type pointer to
3782 // function, only the function-to-pointer conversion (4.3) is
3783 // applied. If the template-argument represents a set of
3784 // overloaded functions (or a pointer to such), the matching
3785 // function is selected from the set (13.4).
3786 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003787 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003788 // -- For a non-type template-parameter of type reference to
3789 // function, no conversions apply. If the template-argument
3790 // represents a set of overloaded functions, the matching
3791 // function is selected from the set (13.4).
3792 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003793 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003794 // -- For a non-type template-parameter of type pointer to
3795 // member function, no conversions apply. If the
3796 // template-argument represents a set of overloaded member
3797 // functions, the matching member function is selected from
3798 // the set (13.4).
3799 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003800 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003801 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003802
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003803 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003804 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003805 true,
3806 FoundResult)) {
3807 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003808 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003809
3810 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3811 ArgType = Arg->getType();
3812 } else
John Wiegley429bb272011-04-08 18:41:53 +00003813 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003814 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003815
John Wiegley429bb272011-04-08 18:41:53 +00003816 if (!ParamType->isMemberPointerType()) {
3817 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3818 ParamType,
3819 Arg, Converted))
3820 return ExprError();
3821 return Owned(Arg);
3822 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003823
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003824 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3825 false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003826 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003827 } else if (!Context.hasSameUnqualifiedType(ArgType,
3828 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003829 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003830 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003831 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003832 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003833 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003834 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003835 }
Mike Stump1eb44332009-09-09 15:08:12 +00003836
John Wiegley429bb272011-04-08 18:41:53 +00003837 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3838 return ExprError();
3839 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003840 }
3841
Chris Lattnerfe90de72009-02-20 21:37:53 +00003842 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003843 // -- for a non-type template-parameter of type pointer to
3844 // object, qualification conversions (4.4) and the
3845 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003846 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003847 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003848 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003849
John Wiegley429bb272011-04-08 18:41:53 +00003850 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3851 ParamType,
3852 Arg, Converted))
3853 return ExprError();
3854 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003855 }
Mike Stump1eb44332009-09-09 15:08:12 +00003856
Ted Kremenek6217b802009-07-29 21:53:49 +00003857 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003858 // -- For a non-type template-parameter of type reference to
3859 // object, no conversions apply. The type referred to by the
3860 // reference may be more cv-qualified than the (otherwise
3861 // identical) type of the template-argument. The
3862 // template-parameter is bound directly to the
3863 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003864 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003865 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003866
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003867 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003868 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3869 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003870 true,
3871 FoundResult)) {
3872 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003873 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003874
3875 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3876 ArgType = Arg->getType();
3877 } else
John Wiegley429bb272011-04-08 18:41:53 +00003878 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003879 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003880
John Wiegley429bb272011-04-08 18:41:53 +00003881 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3882 ParamType,
3883 Arg, Converted))
3884 return ExprError();
3885 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003886 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003887
3888 // -- For a non-type template-parameter of type pointer to data
3889 // member, qualification conversions (4.4) are applied.
3890 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3891
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003892 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003893 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003894 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John Wiegley429bb272011-04-08 18:41:53 +00003895 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003896 } else {
3897 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003898 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003899 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003900 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003901 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003902 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003903 }
3904
John Wiegley429bb272011-04-08 18:41:53 +00003905 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3906 return ExprError();
3907 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003908}
3909
3910/// \brief Check a template argument against its corresponding
3911/// template template parameter.
3912///
3913/// This routine implements the semantics of C++ [temp.arg.template].
3914/// It returns true if an error occurred, and false otherwise.
3915bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003916 const TemplateArgumentLoc &Arg) {
3917 TemplateName Name = Arg.getArgument().getAsTemplate();
3918 TemplateDecl *Template = Name.getAsTemplateDecl();
3919 if (!Template) {
3920 // Any dependent template name is fine.
3921 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3922 return false;
3923 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003924
Richard Smith3e4c6c42011-05-05 21:57:07 +00003925 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00003926 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00003927 // the name of a class template or an alias template, expressed as an
3928 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00003929 // primary class templates are considered when matching the
3930 // template template argument with the corresponding parameter;
3931 // partial specializations are not considered even if their
3932 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003933 //
3934 // Note that we also allow template template parameters here, which
3935 // will happen when we are dealing with, e.g., class template
3936 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003937 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00003938 !isa<TemplateTemplateParmDecl>(Template) &&
3939 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003940 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003941 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003942 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003943 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003944 << Template;
3945 }
3946
3947 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3948 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003949 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003950 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003951 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003952}
3953
Douglas Gregor02024a92010-03-28 02:42:43 +00003954/// \brief Given a non-type template argument that refers to a
3955/// declaration and the type of its corresponding non-type template
3956/// parameter, produce an expression that properly refers to that
3957/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003958ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003959Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3960 QualType ParamType,
3961 SourceLocation Loc) {
3962 assert(Arg.getKind() == TemplateArgument::Declaration &&
3963 "Only declaration template arguments permitted here");
3964 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3965
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003966 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003967 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3968 // If the value is a class member, we might have a pointer-to-member.
3969 // Determine whether the non-type template template parameter is of
3970 // pointer-to-member type. If so, we need to build an appropriate
3971 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3972 // would refer to the member itself.
3973 if (ParamType->isMemberPointerType()) {
3974 QualType ClassType
3975 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3976 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003977 = NestedNameSpecifier::Create(Context, 0, false,
3978 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003979 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003980 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003981
3982 // The actual value-ness of this is unimportant, but for
3983 // internal consistency's sake, references to instance methods
3984 // are r-values.
3985 ExprValueKind VK = VK_LValue;
3986 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3987 VK = VK_RValue;
3988
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003989 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003990 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003991 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003992 Loc,
3993 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003994 if (RefExpr.isInvalid())
3995 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003996
John McCall2de56d12010-08-25 11:45:40 +00003997 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003998
Douglas Gregorc0c83002010-04-30 21:46:38 +00003999 // We might need to perform a trailing qualification conversion, since
4000 // the element type on the parameter could be more qualified than the
4001 // element type in the expression we constructed.
4002 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John Wiegley429bb272011-04-08 18:41:53 +00004003 ParamType.getUnqualifiedType(), false))
4004 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004005
Douglas Gregor02024a92010-03-28 02:42:43 +00004006 assert(!RefExpr.isInvalid() &&
4007 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004008 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004009 return move(RefExpr);
4010 }
4011 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004012
Douglas Gregor02024a92010-03-28 02:42:43 +00004013 QualType T = VD->getType().getNonReferenceType();
4014 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004015 // When the non-type template parameter is a pointer, take the
4016 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004017 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004018 if (RefExpr.isInvalid())
4019 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004020
4021 if (T->isFunctionType() || T->isArrayType()) {
4022 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004023 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4024 if (RefExpr.isInvalid())
4025 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004026
4027 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004028 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004029
Douglas Gregorb7a09262010-04-01 18:32:35 +00004030 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004031 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004032 }
4033
John McCallf89e55a2010-11-18 06:31:45 +00004034 ExprValueKind VK = VK_RValue;
4035
Douglas Gregor02024a92010-03-28 02:42:43 +00004036 // If the non-type template parameter has reference type, qualify the
4037 // resulting declaration reference with the extra qualifiers on the
4038 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004039 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4040 VK = VK_LValue;
4041 T = Context.getQualifiedType(T,
4042 TargetRef->getPointeeType().getQualifiers());
4043 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004044
John McCallf89e55a2010-11-18 06:31:45 +00004045 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004046}
4047
4048/// \brief Construct a new expression that refers to the given
4049/// integral template argument with the given source-location
4050/// information.
4051///
4052/// This routine takes care of the mapping from an integral template
4053/// argument (which may have any integral type) to the appropriate
4054/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004055ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004056Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4057 SourceLocation Loc) {
4058 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004059 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004060 QualType T = Arg.getIntegralType();
4061 if (T->isCharType() || T->isWideCharType())
4062 return Owned(new (Context) CharacterLiteral(
4063 Arg.getAsIntegral()->getZExtValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004064 T->isWideCharType(), T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004065 if (T->isBooleanType())
4066 return Owned(new (Context) CXXBoolLiteralExpr(
4067 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004068 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004069
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004070 if (T->isNullPtrType())
4071 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4072
Chris Lattner223de242011-04-25 20:37:58 +00004073 // If this is an enum type that we're instantiating, we need to use an integer
4074 // type the same size as the enumerator. We don't want to build an
4075 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004076 QualType BT;
4077 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004078 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004079 else
4080 BT = T;
4081
4082 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00004083 if (T->isEnumeralType()) {
4084 // FIXME: This is a hack. We need a better way to handle substituted
4085 // non-type template parameters.
Chris Lattner223de242011-04-25 20:37:58 +00004086 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4087 Context.getTrivialTypeSourceInfo(T, Loc),
4088 Loc, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00004089 }
4090
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004091 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004092}
4093
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004094/// \brief Match two template parameters within template parameter lists.
4095static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4096 bool Complain,
4097 Sema::TemplateParameterListEqualKind Kind,
4098 SourceLocation TemplateArgLoc) {
4099 // Check the actual kind (type, non-type, template).
4100 if (Old->getKind() != New->getKind()) {
4101 if (Complain) {
4102 unsigned NextDiag = diag::err_template_param_different_kind;
4103 if (TemplateArgLoc.isValid()) {
4104 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4105 NextDiag = diag::note_template_param_different_kind;
4106 }
4107 S.Diag(New->getLocation(), NextDiag)
4108 << (Kind != Sema::TPL_TemplateMatch);
4109 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4110 << (Kind != Sema::TPL_TemplateMatch);
4111 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004112
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004113 return false;
4114 }
4115
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004116 // Check that both are parameter packs are neither are parameter packs.
4117 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004118 // template template parameter, the template template parameter can have
4119 // a parameter pack where the template template argument does not.
4120 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4121 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4122 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004123 if (Complain) {
4124 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4125 if (TemplateArgLoc.isValid()) {
4126 S.Diag(TemplateArgLoc,
4127 diag::err_template_arg_template_params_mismatch);
4128 NextDiag = diag::note_template_parameter_pack_non_pack;
4129 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004130
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004131 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4132 : isa<NonTypeTemplateParmDecl>(New)? 1
4133 : 2;
4134 S.Diag(New->getLocation(), NextDiag)
4135 << ParamKind << New->isParameterPack();
4136 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4137 << ParamKind << Old->isParameterPack();
4138 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004139
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004140 return false;
4141 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004142
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004143 // For non-type template parameters, check the type of the parameter.
4144 if (NonTypeTemplateParmDecl *OldNTTP
4145 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4146 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004147
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004148 // If we are matching a template template argument to a template
4149 // template parameter and one of the non-type template parameter types
4150 // is dependent, then we must wait until template instantiation time
4151 // to actually compare the arguments.
4152 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4153 (OldNTTP->getType()->isDependentType() ||
4154 NewNTTP->getType()->isDependentType()))
4155 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004156
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004157 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4158 if (Complain) {
4159 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4160 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004161 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004162 diag::err_template_arg_template_params_mismatch);
4163 NextDiag = diag::note_template_nontype_parm_different_type;
4164 }
4165 S.Diag(NewNTTP->getLocation(), NextDiag)
4166 << NewNTTP->getType()
4167 << (Kind != Sema::TPL_TemplateMatch);
4168 S.Diag(OldNTTP->getLocation(),
4169 diag::note_template_nontype_parm_prev_declaration)
4170 << OldNTTP->getType();
4171 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004172
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004173 return false;
4174 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004175
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004176 return true;
4177 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004178
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004179 // For template template parameters, check the template parameter types.
4180 // The template parameter lists of template template
4181 // parameters must agree.
4182 if (TemplateTemplateParmDecl *OldTTP
4183 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004184 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004185 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4186 OldTTP->getTemplateParameters(),
4187 Complain,
4188 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004189 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004190 : Kind),
4191 TemplateArgLoc);
4192 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004193
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004194 return true;
4195}
Douglas Gregor02024a92010-03-28 02:42:43 +00004196
Douglas Gregora0347822011-01-13 00:08:50 +00004197/// \brief Diagnose a known arity mismatch when comparing template argument
4198/// lists.
4199static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004200void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004201 TemplateParameterList *New,
4202 TemplateParameterList *Old,
4203 Sema::TemplateParameterListEqualKind Kind,
4204 SourceLocation TemplateArgLoc) {
4205 unsigned NextDiag = diag::err_template_param_list_different_arity;
4206 if (TemplateArgLoc.isValid()) {
4207 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4208 NextDiag = diag::note_template_param_list_different_arity;
4209 }
4210 S.Diag(New->getTemplateLoc(), NextDiag)
4211 << (New->size() > Old->size())
4212 << (Kind != Sema::TPL_TemplateMatch)
4213 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4214 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4215 << (Kind != Sema::TPL_TemplateMatch)
4216 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4217}
4218
Douglas Gregorddc29e12009-02-06 22:42:48 +00004219/// \brief Determine whether the given template parameter lists are
4220/// equivalent.
4221///
Mike Stump1eb44332009-09-09 15:08:12 +00004222/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004223/// source code as part of a new template declaration.
4224///
4225/// \param Old The old template parameter list, typically found via
4226/// name lookup of the template declared with this template parameter
4227/// list.
4228///
4229/// \param Complain If true, this routine will produce a diagnostic if
4230/// the template parameter lists are not equivalent.
4231///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004232/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004233///
4234/// \param TemplateArgLoc If this source location is valid, then we
4235/// are actually checking the template parameter list of a template
4236/// argument (New) against the template parameter list of its
4237/// corresponding template template parameter (Old). We produce
4238/// slightly different diagnostics in this scenario.
4239///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004240/// \returns True if the template parameter lists are equal, false
4241/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004242bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004243Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4244 TemplateParameterList *Old,
4245 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004246 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004247 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004248 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4249 if (Complain)
4250 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4251 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004252
4253 return false;
4254 }
4255
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004256 // C++0x [temp.arg.template]p3:
4257 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004258 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004259 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004260 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004261 // template-parameter-list of P. [...]
4262 TemplateParameterList::iterator NewParm = New->begin();
4263 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004264 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004265 OldParmEnd = Old->end();
4266 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004267 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4268 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004269 if (NewParm == NewParmEnd) {
4270 if (Complain)
4271 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4272 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004273
Douglas Gregora0347822011-01-13 00:08:50 +00004274 return false;
4275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004276
Douglas Gregora0347822011-01-13 00:08:50 +00004277 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4278 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004279 return false;
4280
Douglas Gregora0347822011-01-13 00:08:50 +00004281 ++NewParm;
4282 continue;
4283 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004284
Douglas Gregora0347822011-01-13 00:08:50 +00004285 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004286 // [...] When P's template- parameter-list contains a template parameter
4287 // pack (14.5.3), the template parameter pack will match zero or more
4288 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004289 // template-parameter-list of A with the same type and form as the
4290 // template parameter pack in P (ignoring whether those template
4291 // parameters are template parameter packs).
4292 for (; NewParm != NewParmEnd; ++NewParm) {
4293 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4294 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004295 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004296 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004297 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004298
Douglas Gregora0347822011-01-13 00:08:50 +00004299 // Make sure we exhausted all of the arguments.
4300 if (NewParm != NewParmEnd) {
4301 if (Complain)
4302 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4303 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004304
Douglas Gregora0347822011-01-13 00:08:50 +00004305 return false;
4306 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004307
Douglas Gregorddc29e12009-02-06 22:42:48 +00004308 return true;
4309}
4310
4311/// \brief Check whether a template can be declared within this scope.
4312///
4313/// If the template declaration is valid in this scope, returns
4314/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004315bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004316Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004317 // Find the nearest enclosing declaration scope.
4318 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4319 (S->getFlags() & Scope::TemplateParamScope) != 0)
4320 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004321
Douglas Gregorddc29e12009-02-06 22:42:48 +00004322 // C++ [temp]p2:
4323 // A template-declaration can appear only as a namespace scope or
4324 // class scope declaration.
4325 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004326 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4327 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004328 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004329 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004330
Eli Friedman1503f772009-07-31 01:43:05 +00004331 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004332 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004333
4334 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4335 return false;
4336
Mike Stump1eb44332009-09-09 15:08:12 +00004337 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004338 diag::err_template_outside_namespace_or_class_scope)
4339 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004340}
Douglas Gregorcc636682009-02-17 23:15:12 +00004341
Douglas Gregord5cb8762009-10-07 00:13:32 +00004342/// \brief Determine what kind of template specialization the given declaration
4343/// is.
4344static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4345 if (!D)
4346 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004347
Douglas Gregorf6b11852009-10-08 15:14:33 +00004348 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4349 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004350 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4351 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004352 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4353 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004354
Douglas Gregord5cb8762009-10-07 00:13:32 +00004355 return TSK_Undeclared;
4356}
4357
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004358/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004359/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004360///
Douglas Gregor9302da62009-10-14 23:50:59 +00004361/// This routine determines whether a template specialization can be declared
4362/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004363///
4364/// \param S the semantic analysis object for which this check is being
4365/// performed.
4366///
4367/// \param Specialized the entity being specialized or instantiated, which
4368/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004369/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004370/// member class).
4371///
4372/// \param PrevDecl the previous declaration of this entity, if any.
4373///
4374/// \param Loc the location of the explicit specialization or instantiation of
4375/// this entity.
4376///
4377/// \param IsPartialSpecialization whether this is a partial specialization of
4378/// a class template.
4379///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004380/// \returns true if there was an error that we cannot recover from, false
4381/// otherwise.
4382static bool CheckTemplateSpecializationScope(Sema &S,
4383 NamedDecl *Specialized,
4384 NamedDecl *PrevDecl,
4385 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004386 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004387 // Keep these "kind" numbers in sync with the %select statements in the
4388 // various diagnostics emitted by this routine.
4389 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004390 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004391 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004392 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004393 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004394 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004395 EntityKind = 3;
4396 else if (isa<VarDecl>(Specialized))
4397 EntityKind = 4;
4398 else if (isa<RecordDecl>(Specialized))
4399 EntityKind = 5;
4400 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004401 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4402 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004403 return true;
4404 }
4405
Douglas Gregor88b70942009-02-25 22:02:03 +00004406 // C++ [temp.expl.spec]p2:
4407 // An explicit specialization shall be declared in the namespace
4408 // of which the template is a member, or, for member templates, in
4409 // the namespace of which the enclosing class or enclosing class
4410 // template is a member. An explicit specialization of a member
4411 // function, member class or static data member of a class
4412 // template shall be declared in the namespace of which the class
4413 // template is a member. Such a declaration may also be a
4414 // definition. If the declaration is not a definition, the
4415 // specialization may be defined later in the name- space in which
4416 // the explicit specialization was declared, or in a namespace
4417 // that encloses the one in which the explicit specialization was
4418 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004419 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004420 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004421 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004422 return true;
4423 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004424
Douglas Gregor0a407472009-10-07 17:30:37 +00004425 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichete1e96a62011-05-14 19:17:07 +00004426 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4427 << Specialized;
4428 return true;
Douglas Gregor0a407472009-10-07 17:30:37 +00004429 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004430
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004431 // C++ [temp.class.spec]p6:
4432 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004433 // in any namespace scope in which its definition may be defined (14.5.1
4434 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004435 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004436 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004437 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004438 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004439 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004440 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4441 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004442 // C++ [temp.exp.spec]p2:
4443 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004444 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004445 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004446 // An explicit specialization of a member function, member class or
4447 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004448 // namespace of which the class template is a member.
4449 //
4450 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004451 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004452 // the specialized template.
4453 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4454 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004455 bool IsCPlusPlus0xExtension
4456 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004457 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004458 S.Diag(Loc, IsCPlusPlus0xExtension
4459 ? diag::ext_template_spec_decl_out_of_scope_global
4460 : diag::err_template_spec_decl_out_of_scope_global)
4461 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004462 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004463 S.Diag(Loc, IsCPlusPlus0xExtension
4464 ? diag::ext_template_spec_decl_out_of_scope
4465 : diag::err_template_spec_decl_out_of_scope)
4466 << EntityKind << Specialized
4467 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004468
Douglas Gregor9302da62009-10-14 23:50:59 +00004469 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4470 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004471 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004472 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004473
4474 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004475 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004476 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004477 // specializations of function templates, static data members, and member
4478 // functions, so we skip the check here for those kinds of entities.
4479 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004480 // Should we refactor that check, so that it occurs later?
4481 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004482 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4483 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004484 if (isa<TranslationUnitDecl>(SpecializedContext))
4485 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4486 << EntityKind << Specialized;
4487 else if (isa<NamespaceDecl>(SpecializedContext))
4488 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4489 << EntityKind << Specialized
4490 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004491
Douglas Gregor9302da62009-10-14 23:50:59 +00004492 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004493 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004494
Douglas Gregord5cb8762009-10-07 00:13:32 +00004495 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004496
Douglas Gregor88b70942009-02-25 22:02:03 +00004497 return false;
4498}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004499
Douglas Gregorbacb9492011-01-03 21:13:47 +00004500/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4501/// that checks non-type template partial specialization arguments.
4502static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4503 NonTypeTemplateParmDecl *Param,
4504 const TemplateArgument *Args,
4505 unsigned NumArgs) {
4506 for (unsigned I = 0; I != NumArgs; ++I) {
4507 if (Args[I].getKind() == TemplateArgument::Pack) {
4508 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004509 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004510 Args[I].pack_size()))
4511 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004512
Douglas Gregore94866f2009-06-12 21:21:02 +00004513 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004514 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004515
Douglas Gregorbacb9492011-01-03 21:13:47 +00004516 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004517 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004518 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004519 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004520
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004521 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004522 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4523 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004524
4525 // Strip off any implicit casts we added as part of type checking.
4526 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4527 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004528
Douglas Gregore94866f2009-06-12 21:21:02 +00004529 // C++ [temp.class.spec]p8:
4530 // A non-type argument is non-specialized if it is the name of a
4531 // non-type parameter. All other non-type arguments are
4532 // specialized.
4533 //
4534 // Below, we check the two conditions that only apply to
4535 // specialized non-type arguments, so skip any non-specialized
4536 // arguments.
4537 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004538 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004539 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004540
Douglas Gregore94866f2009-06-12 21:21:02 +00004541 // C++ [temp.class.spec]p9:
4542 // Within the argument list of a class template partial
4543 // specialization, the following restrictions apply:
4544 // -- A partially specialized non-type argument expression
4545 // shall not involve a template parameter of the partial
4546 // specialization except when the argument expression is a
4547 // simple identifier.
4548 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004549 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004550 diag::err_dependent_non_type_arg_in_partial_spec)
4551 << ArgExpr->getSourceRange();
4552 return true;
4553 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004554
Douglas Gregore94866f2009-06-12 21:21:02 +00004555 // -- The type of a template parameter corresponding to a
4556 // specialized non-type argument shall not be dependent on a
4557 // parameter of the specialization.
4558 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004559 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004560 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4561 << Param->getType()
4562 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004563 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004564 return true;
4565 }
4566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004567
Douglas Gregorbacb9492011-01-03 21:13:47 +00004568 return false;
4569}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004570
Douglas Gregorbacb9492011-01-03 21:13:47 +00004571/// \brief Check the non-type template arguments of a class template
4572/// partial specialization according to C++ [temp.class.spec]p9.
4573///
4574/// \param TemplateParams the template parameters of the primary class
4575/// template.
4576///
4577/// \param TemplateArg the template arguments of the class template
4578/// partial specialization.
4579///
4580/// \returns true if there was an error, false otherwise.
4581static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4582 TemplateParameterList *TemplateParams,
4583 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4584 const TemplateArgument *ArgList = TemplateArgs.data();
4585
4586 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4587 NonTypeTemplateParmDecl *Param
4588 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4589 if (!Param)
4590 continue;
4591
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004592 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004593 &ArgList[I], 1))
4594 return true;
4595 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004596
4597 return false;
4598}
4599
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004600/// \brief Retrieve the previous declaration of the given declaration.
4601static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4602 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4603 return VD->getPreviousDeclaration();
4604 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4605 return FD->getPreviousDeclaration();
4606 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4607 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004608 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004609 return TD->getPreviousDeclaration();
4610 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4611 return FTD->getPreviousDeclaration();
4612 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4613 return CTD->getPreviousDeclaration();
4614 return 0;
4615}
4616
John McCalld226f652010-08-21 09:40:31 +00004617DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004618Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4619 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004620 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004621 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004622 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004623 SourceLocation TemplateNameLoc,
4624 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004625 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004626 SourceLocation RAngleLoc,
4627 AttributeList *Attr,
4628 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004629 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004630
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004631 // NOTE: KWLoc is the location of the tag keyword. This will instead
4632 // store the location of the outermost template keyword in the declaration.
4633 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4634 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4635
Douglas Gregorcc636682009-02-17 23:15:12 +00004636 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004637 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004638 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004639 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4640
4641 if (!ClassTemplate) {
4642 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004643 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004644 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4645 return true;
4646 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004647
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004648 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004649 bool isPartialSpecialization = false;
4650
Douglas Gregor88b70942009-02-25 22:02:03 +00004651 // Check the validity of the template headers that introduce this
4652 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004653 // FIXME: We probably shouldn't complain about these headers for
4654 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004655 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004656 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004657 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4658 TemplateNameLoc,
4659 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004660 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004661 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004662 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004663 isExplicitSpecialization,
4664 Invalid);
4665 if (Invalid)
4666 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004667
Douglas Gregor05396e22009-08-25 17:23:04 +00004668 if (TemplateParams && TemplateParams->size() > 0) {
4669 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004670
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004671 if (TUK == TUK_Friend) {
4672 Diag(KWLoc, diag::err_partial_specialization_friend)
4673 << SourceRange(LAngleLoc, RAngleLoc);
4674 return true;
4675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004676
Douglas Gregor05396e22009-08-25 17:23:04 +00004677 // C++ [temp.class.spec]p10:
4678 // The template parameter list of a specialization shall not
4679 // contain default template argument values.
4680 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4681 Decl *Param = TemplateParams->getParam(I);
4682 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4683 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004684 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004685 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004686 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004687 }
4688 } else if (NonTypeTemplateParmDecl *NTTP
4689 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4690 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004691 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004692 diag::err_default_arg_in_partial_spec)
4693 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004694 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004695 }
4696 } else {
4697 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004698 if (TTP->hasDefaultArgument()) {
4699 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004700 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004701 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004702 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004703 }
4704 }
4705 }
Douglas Gregora735b202009-10-13 14:39:41 +00004706 } else if (TemplateParams) {
4707 if (TUK == TUK_Friend)
4708 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004709 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004710 SourceRange(TemplateParams->getTemplateLoc(),
4711 TemplateParams->getRAngleLoc()))
4712 << SourceRange(LAngleLoc, RAngleLoc);
4713 else
4714 isExplicitSpecialization = true;
4715 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004716 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004717 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004718 isExplicitSpecialization = true;
4719 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004720
Douglas Gregorcc636682009-02-17 23:15:12 +00004721 // Check that the specialization uses the same tag kind as the
4722 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004723 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4724 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004725 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004726 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004727 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004728 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004729 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004730 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004731 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004732 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004733 diag::note_previous_use);
4734 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4735 }
4736
Douglas Gregor40808ce2009-03-09 23:48:35 +00004737 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004738 TemplateArgumentListInfo TemplateArgs;
4739 TemplateArgs.setLAngleLoc(LAngleLoc);
4740 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004741 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004742
Douglas Gregor925910d2011-01-03 20:35:03 +00004743 // Check for unexpanded parameter packs in any of the template arguments.
4744 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004745 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004746 UPPC_PartialSpecialization))
4747 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004748
Douglas Gregorcc636682009-02-17 23:15:12 +00004749 // Check that the template argument list is well-formed for this
4750 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004751 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004752 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4753 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004754 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004755
Douglas Gregor910f8002010-11-07 23:05:16 +00004756 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004757 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004758
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004759 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004760 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004761 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004762 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004763 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004764 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004765 return true;
4766
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004767 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004768 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004769 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004770 TemplateArgs.size())) {
4771 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4772 << ClassTemplate->getDeclName();
4773 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004774 }
4775 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004776
Douglas Gregorcc636682009-02-17 23:15:12 +00004777 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004778 ClassTemplateSpecializationDecl *PrevDecl = 0;
4779
4780 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004781 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004782 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004783 = ClassTemplate->findPartialSpecialization(Converted.data(),
4784 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004785 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004786 else
4787 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004788 = ClassTemplate->findSpecialization(Converted.data(),
4789 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004790
4791 ClassTemplateSpecializationDecl *Specialization = 0;
4792
Douglas Gregor88b70942009-02-25 22:02:03 +00004793 // Check whether we can declare a class template specialization in
4794 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004795 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004796 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4797 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004798 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004799 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004800
Douglas Gregorb88e8882009-07-30 17:40:51 +00004801 // The canonical type
4802 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004803 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004804 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004805 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004806 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004807 // arguments was referenced but not declared, or we're only
4808 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004809 // declaration node as our own, updating its source location and
4810 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004811 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004812 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004813 if (TemplateParameterLists.size() > 0) {
4814 Specialization->setTemplateParameterListsInfo(Context,
4815 TemplateParameterLists.size(),
4816 (TemplateParameterList**) TemplateParameterLists.release());
4817 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004818 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004819 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004820 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004821 // Build the canonical type that describes the converted template
4822 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004823 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4824 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004825 Converted.data(),
4826 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004827
4828 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004829 ClassTemplate->getInjectedClassNameSpecialization())) {
4830 // C++ [temp.class.spec]p9b3:
4831 //
4832 // -- The argument list of the specialization shall not be identical
4833 // to the implicit argument list of the primary template.
4834 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4835 << (TUK == TUK_Definition)
4836 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4837 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4838 ClassTemplate->getIdentifier(),
4839 TemplateNameLoc,
4840 Attr,
4841 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004842 AS_none,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004843 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004844 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004845 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004846
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004847 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004848 ClassTemplatePartialSpecializationDecl *PrevPartial
4849 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004850 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004851 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004852 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004853 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004854 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004855 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004856 TemplateParams,
4857 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004858 Converted.data(),
4859 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004860 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004861 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004862 PrevPartial,
4863 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004864 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004865 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004866 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004867 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004868 (TemplateParameterList**) TemplateParameterLists.release());
4869 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004870
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004871 if (!PrevPartial)
4872 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004873 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004874
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004875 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004876 // template specialization, make a note of that.
4877 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4878 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004879
Douglas Gregor031a5882009-06-13 00:26:55 +00004880 // Check that all of the template parameters of the class template
4881 // partial specialization are deducible from the template
4882 // arguments. If not, this class template partial specialization
4883 // will never be used.
4884 llvm::SmallVector<bool, 8> DeducibleParams;
4885 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004886 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004887 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004888 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004889 unsigned NumNonDeducible = 0;
4890 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4891 if (!DeducibleParams[I])
4892 ++NumNonDeducible;
4893
4894 if (NumNonDeducible) {
4895 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4896 << (NumNonDeducible > 1)
4897 << SourceRange(TemplateNameLoc, RAngleLoc);
4898 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4899 if (!DeducibleParams[I]) {
4900 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4901 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004902 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004903 diag::note_partial_spec_unused_parameter)
4904 << Param->getDeclName();
4905 else
Mike Stump1eb44332009-09-09 15:08:12 +00004906 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004907 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004908 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004909 }
4910 }
4911 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004912 } else {
4913 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004914 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004915 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004916 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004917 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004918 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004919 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004920 Converted.data(),
4921 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004922 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004923 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004924 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004925 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004926 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00004927 (TemplateParameterList**) TemplateParameterLists.release());
4928 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004929
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004930 if (!PrevDecl)
4931 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004932
4933 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004934 }
4935
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004936 // C++ [temp.expl.spec]p6:
4937 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004938 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004939 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004940 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004941 // use occurs; no diagnostic is required.
4942 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004943 bool Okay = false;
4944 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4945 // Is there any previous explicit specialization declaration?
4946 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4947 Okay = true;
4948 break;
4949 }
4950 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004951
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004952 if (!Okay) {
4953 SourceRange Range(TemplateNameLoc, RAngleLoc);
4954 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4955 << Context.getTypeDeclType(Specialization) << Range;
4956
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004957 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004958 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004959 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004960 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004961 return true;
4962 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004963 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004964
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004965 // If this is not a friend, note that this is an explicit specialization.
4966 if (TUK != TUK_Friend)
4967 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004968
4969 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004970 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004971 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004972 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004973 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004974 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004975 Diag(Def->getLocation(), diag::note_previous_definition);
4976 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004977 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004978 }
4979 }
4980
John McCall7f1b9872010-12-18 03:30:47 +00004981 if (Attr)
4982 ProcessDeclAttributeList(S, Specialization, Attr);
4983
Douglas Gregorfc705b82009-02-26 22:19:44 +00004984 // Build the fully-sugared type for this class template
4985 // specialization as the user wrote in the specialization
4986 // itself. This means that we'll pretty-print the type retrieved
4987 // from the specialization's declaration the way that the user
4988 // actually wrote the specialization, rather than formatting the
4989 // name based on the "canonical" representation used to store the
4990 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004991 TypeSourceInfo *WrittenTy
4992 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4993 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004994 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004995 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004996 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004997 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004998 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004999
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005000 // C++ [temp.expl.spec]p9:
5001 // A template explicit specialization is in the scope of the
5002 // namespace in which the template was defined.
5003 //
5004 // We actually implement this paragraph where we set the semantic
5005 // context (in the creation of the ClassTemplateSpecializationDecl),
5006 // but we also maintain the lexical context where the actual
5007 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005008 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005009
Douglas Gregorcc636682009-02-17 23:15:12 +00005010 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005011 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005012 Specialization->startDefinition();
5013
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005014 if (TUK == TUK_Friend) {
5015 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5016 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005017 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005018 /*FIXME:*/KWLoc);
5019 Friend->setAccess(AS_public);
5020 CurContext->addDecl(Friend);
5021 } else {
5022 // Add the specialization into its lexical context, so that it can
5023 // be seen when iterating through the list of declarations in that
5024 // context. However, specializations are not found by name lookup.
5025 CurContext->addDecl(Specialization);
5026 }
John McCalld226f652010-08-21 09:40:31 +00005027 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005028}
Douglas Gregord57959a2009-03-27 23:10:48 +00005029
John McCalld226f652010-08-21 09:40:31 +00005030Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005031 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005032 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00005033 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
5034}
5035
John McCalld226f652010-08-21 09:40:31 +00005036Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005037 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005038 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005039 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005040 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005041
Douglas Gregor52591bf2009-06-24 00:54:41 +00005042 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005043 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005044 }
Mike Stump1eb44332009-09-09 15:08:12 +00005045
Douglas Gregor52591bf2009-06-24 00:54:41 +00005046 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005047
John McCalld226f652010-08-21 09:40:31 +00005048 Decl *DP = HandleDeclarator(ParentScope, D,
5049 move(TemplateParameterLists),
5050 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00005051 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005052 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005053 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005054 FunctionTemplate->getTemplatedDecl());
5055 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5056 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5057 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005058}
5059
John McCall75042392010-02-11 01:33:53 +00005060/// \brief Strips various properties off an implicit instantiation
5061/// that has just been explicitly specialized.
5062static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005063 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005064
5065 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5066 FD->setInlineSpecified(false);
5067 }
5068}
5069
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005070/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005071/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005072/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005073/// new specialization/instantiation will have any effect.
5074///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005075/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005076/// instantiation.
5077///
5078/// \param NewTSK the kind of the new explicit specialization or instantiation.
5079///
5080/// \param PrevDecl the previous declaration of the entity.
5081///
5082/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5083///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005084/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005085/// declaration was instantiated (either implicitly or explicitly).
5086///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005087/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005088/// specialization or instantiation has no effect and should be ignored.
5089///
5090/// \returns true if there was an error that should prevent the introduction of
5091/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005092bool
5093Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5094 TemplateSpecializationKind NewTSK,
5095 NamedDecl *PrevDecl,
5096 TemplateSpecializationKind PrevTSK,
5097 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005098 bool &HasNoEffect) {
5099 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005100
Douglas Gregor454885e2009-10-15 15:54:05 +00005101 switch (NewTSK) {
5102 case TSK_Undeclared:
5103 case TSK_ImplicitInstantiation:
5104 assert(false && "Don't check implicit instantiations here");
5105 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005106
Douglas Gregor454885e2009-10-15 15:54:05 +00005107 case TSK_ExplicitSpecialization:
5108 switch (PrevTSK) {
5109 case TSK_Undeclared:
5110 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005111 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005112 // explicitly specialized or has merely been mentioned without any
5113 // instantiation.
5114 return false;
5115
5116 case TSK_ImplicitInstantiation:
5117 if (PrevPointOfInstantiation.isInvalid()) {
5118 // The declaration itself has not actually been instantiated, so it is
5119 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005120 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005121 return false;
5122 }
5123 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005124
Douglas Gregor454885e2009-10-15 15:54:05 +00005125 case TSK_ExplicitInstantiationDeclaration:
5126 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005127 assert((PrevTSK == TSK_ImplicitInstantiation ||
5128 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005129 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005130
Douglas Gregor454885e2009-10-15 15:54:05 +00005131 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005132 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005133 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005134 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005135 // implicit instantiation to take place, in every translation unit in
5136 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005137 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5138 // Is there any previous explicit specialization declaration?
5139 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5140 return false;
5141 }
5142
Douglas Gregor0d035142009-10-27 18:42:08 +00005143 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005144 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005145 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005146 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005147
Douglas Gregor454885e2009-10-15 15:54:05 +00005148 return true;
5149 }
5150 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005151
Douglas Gregor454885e2009-10-15 15:54:05 +00005152 case TSK_ExplicitInstantiationDeclaration:
5153 switch (PrevTSK) {
5154 case TSK_ExplicitInstantiationDeclaration:
5155 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005156 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005157 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005158
Douglas Gregor454885e2009-10-15 15:54:05 +00005159 case TSK_Undeclared:
5160 case TSK_ImplicitInstantiation:
5161 // We're explicitly instantiating something that may have already been
5162 // implicitly instantiated; that's fine.
5163 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005164
Douglas Gregor454885e2009-10-15 15:54:05 +00005165 case TSK_ExplicitSpecialization:
5166 // C++0x [temp.explicit]p4:
5167 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005168 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005169 // specialization for that template, the explicit instantiation has no
5170 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005171 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005172 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005173
Douglas Gregor454885e2009-10-15 15:54:05 +00005174 case TSK_ExplicitInstantiationDefinition:
5175 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005176 // If an entity is the subject of both an explicit instantiation
5177 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005178 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005179 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005180 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005181 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005182 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005183 assert(PrevPointOfInstantiation.isValid() &&
5184 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005185 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005186 return false;
5187 }
5188 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005189
Douglas Gregor454885e2009-10-15 15:54:05 +00005190 case TSK_ExplicitInstantiationDefinition:
5191 switch (PrevTSK) {
5192 case TSK_Undeclared:
5193 case TSK_ImplicitInstantiation:
5194 // We're explicitly instantiating something that may have already been
5195 // implicitly instantiated; that's fine.
5196 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005197
Douglas Gregor454885e2009-10-15 15:54:05 +00005198 case TSK_ExplicitSpecialization:
5199 // C++ DR 259, C++0x [temp.explicit]p4:
5200 // For a given set of template parameters, if an explicit
5201 // instantiation of a template appears after a declaration of
5202 // an explicit specialization for that template, the explicit
5203 // instantiation has no effect.
5204 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005205 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005206 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005207 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005208 if (!getLangOptions().CPlusPlus0x) {
5209 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005210 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005211 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005212 diag::note_previous_template_specialization);
5213 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005214 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005215 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005216
Douglas Gregor454885e2009-10-15 15:54:05 +00005217 case TSK_ExplicitInstantiationDeclaration:
5218 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005219 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005220 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005221
Douglas Gregor454885e2009-10-15 15:54:05 +00005222 case TSK_ExplicitInstantiationDefinition:
5223 // C++0x [temp.spec]p5:
5224 // For a given template and a given set of template-arguments,
5225 // - an explicit instantiation definition shall appear at most once
5226 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005227 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005228 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005229 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005230 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005231 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005232 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005233 }
5234 break;
5235 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005236
Douglas Gregor454885e2009-10-15 15:54:05 +00005237 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005238
Douglas Gregor454885e2009-10-15 15:54:05 +00005239 return false;
5240}
5241
John McCallaf2094e2010-04-08 09:05:18 +00005242/// \brief Perform semantic analysis for the given dependent function
5243/// template specialization. The only possible way to get a dependent
5244/// function template specialization is with a friend declaration,
5245/// like so:
5246///
5247/// template <class T> void foo(T);
5248/// template <class T> class A {
5249/// friend void foo<>(T);
5250/// };
5251///
5252/// There really isn't any useful analysis we can do here, so we
5253/// just store the information.
5254bool
5255Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5256 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5257 LookupResult &Previous) {
5258 // Remove anything from Previous that isn't a function template in
5259 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005260 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005261 LookupResult::Filter F = Previous.makeFilter();
5262 while (F.hasNext()) {
5263 NamedDecl *D = F.next()->getUnderlyingDecl();
5264 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005265 !FDLookupContext->InEnclosingNamespaceSetOf(
5266 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005267 F.erase();
5268 }
5269 F.done();
5270
5271 // Should this be diagnosed here?
5272 if (Previous.empty()) return true;
5273
5274 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5275 ExplicitTemplateArgs);
5276 return false;
5277}
5278
Abramo Bagnarae03db982010-05-20 15:32:11 +00005279/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005280/// specialization.
5281///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005282/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005283/// explicit function template specialization. On successful completion,
5284/// the function declaration \p FD will become a function template
5285/// specialization.
5286///
5287/// \param FD the function declaration, which will be updated to become a
5288/// function template specialization.
5289///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005290/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5291/// if any. Note that this may be valid info even when 0 arguments are
5292/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5293/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005294///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005295/// \param PrevDecl the set of declarations that may be specialized by
5296/// this function specialization.
5297bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005298Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005299 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005300 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005301 // The set of function template specializations that could match this
5302 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005303 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005304
Sebastian Redl7a126a42010-08-31 00:36:30 +00005305 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005306 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5307 I != E; ++I) {
5308 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5309 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005310 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005311 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005312 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5313 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005314 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005315
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005316 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005317 // A trailing template-argument can be left unspecified in the
5318 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005319 // provided it can be deduced from the function argument type.
5320 // Perform template argument deduction to determine whether we may be
5321 // specializing this template.
5322 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005323 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005324 FunctionDecl *Specialization = 0;
5325 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005326 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005327 FD->getType(),
5328 Specialization,
5329 Info)) {
5330 // FIXME: Template argument deduction failed; record why it failed, so
5331 // that we can provide nifty diagnostics.
5332 (void)TDK;
5333 continue;
5334 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005335
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005336 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005337 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005338 }
5339 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005340
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005341 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005342 UnresolvedSetIterator Result
5343 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005344 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005345 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005346 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005347 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005348 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005349 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005350 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005351 return true;
John McCallc373d482010-01-27 01:50:18 +00005352
5353 // Ignore access information; it doesn't figure into redeclaration checking.
5354 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005355
5356 FunctionTemplateSpecializationInfo *SpecInfo
5357 = Specialization->getTemplateSpecializationInfo();
5358 assert(SpecInfo && "Function template specialization info missing?");
5359 {
5360 // Note: do not overwrite location info if previous template
5361 // specialization kind was explicit.
5362 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5363 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5364 Specialization->setLocation(FD->getLocation());
5365 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005366
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005367 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005368 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005369
5370 // If this is a friend declaration, then we're not really declaring
5371 // an explicit specialization.
5372 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005373
Douglas Gregord5cb8762009-10-07 00:13:32 +00005374 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005375 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005376 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005377 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005378 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005379 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005380 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005381
5382 // C++ [temp.expl.spec]p6:
5383 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005384 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005385 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005386 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005387 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005388 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005389 if (!isFriend &&
5390 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005391 TSK_ExplicitSpecialization,
5392 Specialization,
5393 SpecInfo->getTemplateSpecializationKind(),
5394 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005395 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005396 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005397
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005398 // Mark the prior declaration as an explicit specialization, so that later
5399 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005400 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005401 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005402 MarkUnusedFileScopedDecl(Specialization);
5403 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005404
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005405 // Turn the given function declaration into a function template
5406 // specialization, with the template arguments from the previous
5407 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005408 // Take copies of (semantic and syntactic) template argument lists.
5409 const TemplateArgumentList* TemplArgs = new (Context)
5410 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5411 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5412 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005413 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005414 TemplArgs, /*InsertPos=*/0,
5415 SpecInfo->getTemplateSpecializationKind(),
5416 TemplArgsAsWritten);
Douglas Gregore885e182011-05-21 18:53:30 +00005417 FD->setStorageClass(Specialization->getStorageClass());
5418
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005419 // The "previous declaration" for this function template specialization is
5420 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005421 Previous.clear();
5422 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005423 return false;
5424}
5425
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005426/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005427/// specialization.
5428///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005429/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005430/// explicit member function specialization. On successful completion,
5431/// the function declaration \p FD will become a member function
5432/// specialization.
5433///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005434/// \param Member the member declaration, which will be updated to become a
5435/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005436///
John McCall68263142009-11-18 22:49:29 +00005437/// \param Previous the set of declarations, one of which may be specialized
5438/// by this function specialization; the set will be modified to contain the
5439/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005440bool
John McCall68263142009-11-18 22:49:29 +00005441Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005442 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005443
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005444 // Try to find the member we are instantiating.
5445 NamedDecl *Instantiation = 0;
5446 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005447 MemberSpecializationInfo *MSInfo = 0;
5448
John McCall68263142009-11-18 22:49:29 +00005449 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005450 // Nowhere to look anyway.
5451 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005452 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5453 I != E; ++I) {
5454 NamedDecl *D = (*I)->getUnderlyingDecl();
5455 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005456 if (Context.hasSameType(Function->getType(), Method->getType())) {
5457 Instantiation = Method;
5458 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005459 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005460 break;
5461 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005462 }
5463 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005464 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005465 VarDecl *PrevVar;
5466 if (Previous.isSingleResult() &&
5467 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005468 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005469 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005470 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005471 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005472 }
5473 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005474 CXXRecordDecl *PrevRecord;
5475 if (Previous.isSingleResult() &&
5476 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5477 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005478 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005479 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005480 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005481 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005482
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005483 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005484 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005485 // specializations are always out-of-line, the caller will complain about
5486 // this mismatch later.
5487 return false;
5488 }
John McCall77e8b112010-04-13 20:37:33 +00005489
5490 // If this is a friend, just bail out here before we start turning
5491 // things into explicit specializations.
5492 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5493 // Preserve instantiation information.
5494 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5495 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5496 cast<CXXMethodDecl>(InstantiatedFrom),
5497 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5498 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5499 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5500 cast<CXXRecordDecl>(InstantiatedFrom),
5501 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5502 }
5503
5504 Previous.clear();
5505 Previous.addDecl(Instantiation);
5506 return false;
5507 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005508
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005509 // Make sure that this is a specialization of a member.
5510 if (!InstantiatedFrom) {
5511 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5512 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005513 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5514 return true;
5515 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005516
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005517 // C++ [temp.expl.spec]p6:
5518 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005519 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005520 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005521 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005522 // use occurs; no diagnostic is required.
5523 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005524
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005525 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005526 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5527 TSK_ExplicitSpecialization,
5528 Instantiation,
5529 MSInfo->getTemplateSpecializationKind(),
5530 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005531 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005532 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005533
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005534 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005535 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005536 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005537 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005538 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005539 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005540
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005541 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005542 // the original declaration to note that it is an explicit specialization
5543 // (if it was previously an implicit instantiation). This latter step
5544 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005545 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005546 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5547 if (InstantiationFunction->getTemplateSpecializationKind() ==
5548 TSK_ImplicitInstantiation) {
5549 InstantiationFunction->setTemplateSpecializationKind(
5550 TSK_ExplicitSpecialization);
5551 InstantiationFunction->setLocation(Member->getLocation());
5552 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005553
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005554 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5555 cast<CXXMethodDecl>(InstantiatedFrom),
5556 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005557 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005558 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005559 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5560 if (InstantiationVar->getTemplateSpecializationKind() ==
5561 TSK_ImplicitInstantiation) {
5562 InstantiationVar->setTemplateSpecializationKind(
5563 TSK_ExplicitSpecialization);
5564 InstantiationVar->setLocation(Member->getLocation());
5565 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005566
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005567 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5568 cast<VarDecl>(InstantiatedFrom),
5569 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005570 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005571 } else {
5572 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005573 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5574 if (InstantiationClass->getTemplateSpecializationKind() ==
5575 TSK_ImplicitInstantiation) {
5576 InstantiationClass->setTemplateSpecializationKind(
5577 TSK_ExplicitSpecialization);
5578 InstantiationClass->setLocation(Member->getLocation());
5579 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005580
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005581 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005582 cast<CXXRecordDecl>(InstantiatedFrom),
5583 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005584 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005585
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005586 // Save the caller the trouble of having to figure out which declaration
5587 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005588 Previous.clear();
5589 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005590 return false;
5591}
5592
Douglas Gregor558c0322009-10-14 23:41:34 +00005593/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005594///
5595/// \returns true if a serious error occurs, false otherwise.
5596static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005597 SourceLocation InstLoc,
5598 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005599 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5600 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005601
Douglas Gregor669eed82010-07-13 00:10:04 +00005602 if (CurContext->isRecord()) {
5603 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5604 << D;
5605 return true;
5606 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005607
Douglas Gregor558c0322009-10-14 23:41:34 +00005608 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005609 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005610 // template.
5611 //
5612 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005613 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005614 !CurContext->Encloses(OrigContext)) {
5615 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005616 S.Diag(InstLoc,
5617 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005618 diag::err_explicit_instantiation_out_of_scope
5619 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005620 << D << NS;
5621 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005622 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005623 S.getLangOptions().CPlusPlus0x?
5624 diag::err_explicit_instantiation_must_be_global
5625 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005626 << D;
5627 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005628 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005629 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005630
Douglas Gregor558c0322009-10-14 23:41:34 +00005631 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005632 // If the name declared in the explicit instantiation is an unqualified
5633 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005634 // its template is declared or, if that namespace is inline (7.3.1), any
5635 // namespace from its enclosing namespace set.
5636 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005637 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005638
5639 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005640 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005641
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005642 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005643 S.getLangOptions().CPlusPlus0x?
5644 diag::err_explicit_instantiation_unqualified_wrong_namespace
5645 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005646 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005647 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005648 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005649}
5650
5651/// \brief Determine whether the given scope specifier has a template-id in it.
5652static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5653 if (!SS.isSet())
5654 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005655
Douglas Gregor558c0322009-10-14 23:41:34 +00005656 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005657 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005658 // or a static data member of a class template specialization, the name of
5659 // the class template specialization in the qualified-id for the member
5660 // name shall be a simple-template-id.
5661 //
5662 // C++98 has the same restriction, just worded differently.
5663 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5664 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005665 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005666 if (isa<TemplateSpecializationType>(T))
5667 return true;
5668
5669 return false;
5670}
5671
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005672// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005673DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005674Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005675 SourceLocation ExternLoc,
5676 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005677 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005678 SourceLocation KWLoc,
5679 const CXXScopeSpec &SS,
5680 TemplateTy TemplateD,
5681 SourceLocation TemplateNameLoc,
5682 SourceLocation LAngleLoc,
5683 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005684 SourceLocation RAngleLoc,
5685 AttributeList *Attr) {
5686 // Find the class template we're specializing
5687 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005688 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005689 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5690
5691 // Check that the specialization uses the same tag kind as the
5692 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005693 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5694 assert(Kind != TTK_Enum &&
5695 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005696 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005697 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005698 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005699 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005700 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005701 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005702 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005703 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005704 diag::note_previous_use);
5705 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5706 }
5707
Douglas Gregor558c0322009-10-14 23:41:34 +00005708 // C++0x [temp.explicit]p2:
5709 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005710 // definition and an explicit instantiation declaration. An explicit
5711 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005712 TemplateSpecializationKind TSK
5713 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5714 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005715
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005716 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005717 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005718 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005719
5720 // Check that the template argument list is well-formed for this
5721 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005722 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005723 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5724 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005725 return true;
5726
Douglas Gregor910f8002010-11-07 23:05:16 +00005727 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005728 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005729
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005730 // Find the class template specialization declaration that
5731 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005732 void *InsertPos = 0;
5733 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005734 = ClassTemplate->findSpecialization(Converted.data(),
5735 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005736
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005737 TemplateSpecializationKind PrevDecl_TSK
5738 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5739
Douglas Gregord5cb8762009-10-07 00:13:32 +00005740 // C++0x [temp.explicit]p2:
5741 // [...] An explicit instantiation shall appear in an enclosing
5742 // namespace of its template. [...]
5743 //
5744 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005745 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5746 SS.isSet()))
5747 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005748
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005749 ClassTemplateSpecializationDecl *Specialization = 0;
5750
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005751 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005752 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005753 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005754 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005755 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005756 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005757 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005758
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005759 // Even though HasNoEffect == true means that this explicit instantiation
5760 // has no effect on semantics, we go on to put its syntax in the AST.
5761
5762 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5763 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005764 // Since the only prior class template specialization with these
5765 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005766 // declaration node as our own, updating the source location
5767 // for the template name to reflect our new declaration.
5768 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005769 Specialization = PrevDecl;
5770 Specialization->setLocation(TemplateNameLoc);
5771 PrevDecl = 0;
5772 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005773 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005774
Douglas Gregor52604ab2009-09-11 21:19:12 +00005775 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005776 // Create a new class template specialization declaration node for
5777 // this explicit specialization.
5778 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005779 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005780 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005781 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005782 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005783 Converted.data(),
5784 Converted.size(),
5785 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005786 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005787
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005788 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005789 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005790 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005791 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005792 }
5793
5794 // Build the fully-sugared type for this explicit instantiation as
5795 // the user wrote in the explicit instantiation itself. This means
5796 // that we'll pretty-print the type retrieved from the
5797 // specialization's declaration the way that the user actually wrote
5798 // the explicit instantiation, rather than formatting the name based
5799 // on the "canonical" representation used to store the template
5800 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005801 TypeSourceInfo *WrittenTy
5802 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5803 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005804 Context.getTypeDeclType(Specialization));
5805 Specialization->setTypeAsWritten(WrittenTy);
5806 TemplateArgsIn.release();
5807
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005808 // Set source locations for keywords.
5809 Specialization->setExternLoc(ExternLoc);
5810 Specialization->setTemplateKeywordLoc(TemplateLoc);
5811
5812 // Add the explicit instantiation into its lexical context. However,
5813 // since explicit instantiations are never found by name lookup, we
5814 // just put it into the declaration context directly.
5815 Specialization->setLexicalDeclContext(CurContext);
5816 CurContext->addDecl(Specialization);
5817
5818 // Syntax is now OK, so return if it has no other effect on semantics.
5819 if (HasNoEffect) {
5820 // Set the template specialization kind.
5821 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005822 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005823 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005824
5825 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005826 // A definition of a class template or class member template
5827 // shall be in scope at the point of the explicit instantiation of
5828 // the class template or class member template.
5829 //
5830 // This check comes when we actually try to perform the
5831 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005832 ClassTemplateSpecializationDecl *Def
5833 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005834 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005835 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005836 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005837 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005838 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005839 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5840 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005841
Douglas Gregor0d035142009-10-27 18:42:08 +00005842 // Instantiate the members of this class template specialization.
5843 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005844 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005845 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005846 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5847
5848 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5849 // TSK_ExplicitInstantiationDefinition
5850 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5851 TSK == TSK_ExplicitInstantiationDefinition)
5852 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005853
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005854 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005855 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005856
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005857 // Set the template specialization kind.
5858 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005859 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005860}
5861
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005862// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005863DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005864Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005865 SourceLocation ExternLoc,
5866 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005867 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005868 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005869 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005870 IdentifierInfo *Name,
5871 SourceLocation NameLoc,
5872 AttributeList *Attr) {
5873
Douglas Gregor402abb52009-05-28 23:31:59 +00005874 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005875 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005876 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005877 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5878 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005879 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005880 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005881 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5882
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005883 if (!TagD)
5884 return true;
5885
John McCalld226f652010-08-21 09:40:31 +00005886 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005887 if (Tag->isEnum()) {
5888 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5889 << Context.getTypeDeclType(Tag);
5890 return true;
5891 }
5892
Douglas Gregord0c87372009-05-27 17:30:49 +00005893 if (Tag->isInvalidDecl())
5894 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005895
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005896 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5897 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5898 if (!Pattern) {
5899 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5900 << Context.getTypeDeclType(Record);
5901 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5902 return true;
5903 }
5904
Douglas Gregor558c0322009-10-14 23:41:34 +00005905 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005906 // If the explicit instantiation is for a class or member class, the
5907 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005908 // simple-template-id.
5909 //
5910 // C++98 has the same restriction, just worded differently.
5911 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005912 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005913 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005914
Douglas Gregor558c0322009-10-14 23:41:34 +00005915 // C++0x [temp.explicit]p2:
5916 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005917 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005918 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005919 TemplateSpecializationKind TSK
5920 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5921 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005922
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005923 // C++0x [temp.explicit]p2:
5924 // [...] An explicit instantiation shall appear in an enclosing
5925 // namespace of its template. [...]
5926 //
5927 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005928 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005929
Douglas Gregor454885e2009-10-15 15:54:05 +00005930 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005931 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005932 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005933 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005934 PrevDecl = Record;
5935 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005936 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005937 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005938 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005939 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005940 PrevDecl,
5941 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005942 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005943 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005944 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005945 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005946 return TagD;
5947 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005948
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005949 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005950 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005951 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005952 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005953 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005954 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005955 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005956 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005957 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005958 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5959 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005960 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5961 << Pattern;
5962 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005963 } else {
5964 if (InstantiateClass(NameLoc, Record, Def,
5965 getTemplateInstantiationArgs(Record),
5966 TSK))
5967 return true;
5968
Douglas Gregor952b0172010-02-11 01:04:33 +00005969 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005970 if (!RecordDef)
5971 return true;
5972 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005973 }
5974
Douglas Gregor0d035142009-10-27 18:42:08 +00005975 // Instantiate all of the members of the class.
5976 InstantiateClassMembers(NameLoc, RecordDef,
5977 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005978
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005979 if (TSK == TSK_ExplicitInstantiationDefinition)
5980 MarkVTableUsed(NameLoc, RecordDef, true);
5981
Mike Stump390b4cc2009-05-16 07:39:55 +00005982 // FIXME: We don't have any representation for explicit instantiations of
5983 // member classes. Such a representation is not needed for compilation, but it
5984 // should be available for clients that want to see all of the declarations in
5985 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005986 return TagD;
5987}
5988
John McCallf312b1e2010-08-26 23:41:50 +00005989DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5990 SourceLocation ExternLoc,
5991 SourceLocation TemplateLoc,
5992 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005993 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005994 // TODO: check if/when DNInfo should replace Name.
5995 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5996 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005997 if (!Name) {
5998 if (!D.isInvalidType())
5999 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6000 diag::err_explicit_instantiation_requires_name)
6001 << D.getDeclSpec().getSourceRange()
6002 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006003
Douglas Gregord5a423b2009-09-25 18:43:00 +00006004 return true;
6005 }
6006
6007 // The scope passed in may not be a decl scope. Zip up the scope tree until
6008 // we find one that is.
6009 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6010 (S->getFlags() & Scope::TemplateParamScope) != 0)
6011 S = S->getParent();
6012
6013 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006014 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6015 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006016 if (R.isNull())
6017 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006018
Douglas Gregore885e182011-05-21 18:53:30 +00006019 // C++ [dcl.stc]p1:
6020 // A storage-class-specifier shall not be specified in [...] an explicit
6021 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006022 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006023 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6024 << Name;
6025 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006026 } else if (D.getDeclSpec().getStorageClassSpec()
6027 != DeclSpec::SCS_unspecified) {
6028 // Complain about then remove the storage class specifier.
6029 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6030 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6031
6032 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006033 }
6034
Douglas Gregor663b5a02009-10-14 20:14:33 +00006035 // C++0x [temp.explicit]p1:
6036 // [...] An explicit instantiation of a function template shall not use the
6037 // inline or constexpr specifiers.
6038 // Presumably, this also applies to member functions of class templates as
6039 // well.
6040 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006041 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00006042 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00006043 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006044
Douglas Gregor663b5a02009-10-14 20:14:33 +00006045 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006046
Douglas Gregor558c0322009-10-14 23:41:34 +00006047 // C++0x [temp.explicit]p2:
6048 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006049 // definition and an explicit instantiation declaration. An explicit
6050 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006051 TemplateSpecializationKind TSK
6052 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6053 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006054
Abramo Bagnara25777432010-08-11 22:01:17 +00006055 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006056 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006057
6058 if (!R->isFunctionType()) {
6059 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006060 // A [...] static data member of a class template can be explicitly
6061 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006062 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006063 if (Previous.isAmbiguous())
6064 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006065
John McCall1bcee0a2009-12-02 08:25:40 +00006066 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006067 if (!Prev || !Prev->isStaticDataMember()) {
6068 // We expect to see a data data member here.
6069 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6070 << Name;
6071 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6072 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006073 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006074 return true;
6075 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006076
Douglas Gregord5a423b2009-09-25 18:43:00 +00006077 if (!Prev->getInstantiatedFromStaticDataMember()) {
6078 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006079 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006080 diag::err_explicit_instantiation_data_member_not_instantiated)
6081 << Prev;
6082 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6083 // FIXME: Can we provide a note showing where this was declared?
6084 return true;
6085 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006086
Douglas Gregor558c0322009-10-14 23:41:34 +00006087 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006088 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006089 // or a static data member of a class template specialization, the name of
6090 // the class template specialization in the qualified-id for the member
6091 // name shall be a simple-template-id.
6092 //
6093 // C++98 has the same restriction, just worded differently.
6094 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006095 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006096 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006097 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006098
Douglas Gregor558c0322009-10-14 23:41:34 +00006099 // Check the scope of this explicit instantiation.
6100 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006101
Douglas Gregor454885e2009-10-15 15:54:05 +00006102 // Verify that it is okay to explicitly instantiate here.
6103 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6104 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006105 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006106 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006107 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006108 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006109 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006110 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006111 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006112 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006113
Douglas Gregord5a423b2009-09-25 18:43:00 +00006114 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006115 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006116 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006117 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006118
Douglas Gregord5a423b2009-09-25 18:43:00 +00006119 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006120 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006121 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006122
6123 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006124 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006125 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006126 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006127 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6128 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006129 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6130 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006131 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6132 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006133 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006134 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006135 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006136 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006137 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006138
Douglas Gregord5a423b2009-09-25 18:43:00 +00006139 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006140 // A [...] function [...] can be explicitly instantiated from its template.
6141 // A member function [...] of a class template can be explicitly
6142 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006143 // template.
John McCallc373d482010-01-27 01:50:18 +00006144 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006145 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6146 P != PEnd; ++P) {
6147 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006148 if (!HasExplicitTemplateArgs) {
6149 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6150 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6151 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006152
John McCallc373d482010-01-27 01:50:18 +00006153 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006154 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6155 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006156 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006157 }
6158 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006159
Douglas Gregord5a423b2009-09-25 18:43:00 +00006160 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6161 if (!FunTmpl)
6162 continue;
6163
John McCall5769d612010-02-08 23:07:23 +00006164 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006165 FunctionDecl *Specialization = 0;
6166 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006167 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006168 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006169 R, Specialization, Info)) {
6170 // FIXME: Keep track of almost-matches?
6171 (void)TDK;
6172 continue;
6173 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006174
John McCallc373d482010-01-27 01:50:18 +00006175 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006176 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006177
Douglas Gregord5a423b2009-09-25 18:43:00 +00006178 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006179 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006180 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006181 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006182 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6183 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6184 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006185
John McCallc373d482010-01-27 01:50:18 +00006186 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006187 return true;
John McCallc373d482010-01-27 01:50:18 +00006188
6189 // Ignore access control bits, we don't need them for redeclaration checking.
6190 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006191
Douglas Gregor0a897e32009-10-15 17:21:20 +00006192 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006193 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006194 diag::err_explicit_instantiation_member_function_not_instantiated)
6195 << Specialization
6196 << (Specialization->getTemplateSpecializationKind() ==
6197 TSK_ExplicitSpecialization);
6198 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6199 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006200 }
6201
Douglas Gregor0a897e32009-10-15 17:21:20 +00006202 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006203 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6204 PrevDecl = Specialization;
6205
Douglas Gregor0a897e32009-10-15 17:21:20 +00006206 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006207 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006208 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006209 PrevDecl,
6210 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006211 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006212 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006213 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006214
Douglas Gregor0a897e32009-10-15 17:21:20 +00006215 // FIXME: We may still want to build some representation of this
6216 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006217 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006218 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006219 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006220
6221 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006222
Douglas Gregor0a897e32009-10-15 17:21:20 +00006223 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006224 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006225
Douglas Gregor558c0322009-10-14 23:41:34 +00006226 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006227 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006228 // or a static data member of a class template specialization, the name of
6229 // the class template specialization in the qualified-id for the member
6230 // name shall be a simple-template-id.
6231 //
6232 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006233 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006234 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006235 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006236 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006237 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006238 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006239 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006240
Douglas Gregor558c0322009-10-14 23:41:34 +00006241 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006242 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006243 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006244 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006245 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006246
Douglas Gregord5a423b2009-09-25 18:43:00 +00006247 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006248 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006249}
6250
John McCallf312b1e2010-08-26 23:41:50 +00006251TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006252Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6253 const CXXScopeSpec &SS, IdentifierInfo *Name,
6254 SourceLocation TagLoc, SourceLocation NameLoc) {
6255 // This has to hold, because SS is expected to be defined.
6256 assert(Name && "Expected a name in a dependent tag");
6257
6258 NestedNameSpecifier *NNS
6259 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6260 if (!NNS)
6261 return true;
6262
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006263 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006264
Douglas Gregor48c89f42010-04-24 16:38:41 +00006265 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6266 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006267 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006268 return true;
6269 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006270
Douglas Gregor059101f2011-03-02 00:47:37 +00006271 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006272 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006273 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6274
6275 // Create type-source location information for this type.
6276 TypeLocBuilder TLB;
6277 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6278 TL.setKeywordLoc(TagLoc);
6279 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6280 TL.setNameLoc(NameLoc);
6281 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006282}
6283
John McCallf312b1e2010-08-26 23:41:50 +00006284TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006285Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6286 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006287 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006288 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006289 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006290
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006291 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6292 !getLangOptions().CPlusPlus0x)
6293 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006294 << FixItHint::CreateRemoval(TypenameLoc);
6295
Douglas Gregor2494dd02011-03-01 01:34:45 +00006296 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006297 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6298 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006299 if (T.isNull())
6300 return true;
John McCall63b43852010-04-29 23:50:39 +00006301
6302 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6303 if (isa<DependentNameType>(T)) {
6304 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006305 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006306 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006307 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006308 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006309 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006310 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006311 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006312 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006313 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006314
John McCallb3d87482010-08-24 05:47:05 +00006315 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006316}
6317
John McCallf312b1e2010-08-26 23:41:50 +00006318TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006319Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6320 const CXXScopeSpec &SS,
6321 SourceLocation TemplateLoc,
6322 TemplateTy TemplateIn,
6323 SourceLocation TemplateNameLoc,
6324 SourceLocation LAngleLoc,
6325 ASTTemplateArgsPtr TemplateArgsIn,
6326 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006327 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6328 !getLangOptions().CPlusPlus0x)
6329 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006330 << FixItHint::CreateRemoval(TypenameLoc);
6331
6332 // Translate the parser's template argument list in our AST format.
6333 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6334 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6335
6336 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006337 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6338 // Construct a dependent template specialization type.
6339 assert(DTN && "dependent template has non-dependent name?");
6340 assert(DTN->getQualifier()
6341 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6342 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6343 DTN->getQualifier(),
6344 DTN->getIdentifier(),
6345 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006346
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006347 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006348 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006349 DependentTemplateSpecializationTypeLoc SpecTL
6350 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006351 SpecTL.setLAngleLoc(LAngleLoc);
6352 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006353 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006354 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006355 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006356 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6357 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006358 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006359 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006360
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006361 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6362 if (T.isNull())
6363 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006364
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006365 // Provide source-location information for the template specialization
6366 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006367 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006368 TemplateSpecializationTypeLoc SpecTL
6369 = Builder.push<TemplateSpecializationTypeLoc>(T);
6370
6371 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006372 SpecTL.setLAngleLoc(LAngleLoc);
6373 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006374 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006375 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6376 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6377
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006378 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6379 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6380 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006381 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6382
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006383 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6384 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006385}
6386
Douglas Gregora02411e2011-02-27 22:46:49 +00006387
Douglas Gregord57959a2009-03-27 23:10:48 +00006388/// \brief Build the type that describes a C++ typename specifier,
6389/// e.g., "typename T::type".
6390QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006391Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6392 SourceLocation KeywordLoc,
6393 NestedNameSpecifierLoc QualifierLoc,
6394 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006395 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006396 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006397 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006398
John McCall77bb1aa2010-05-01 00:40:08 +00006399 DeclContext *Ctx = computeDeclContext(SS);
6400 if (!Ctx) {
6401 // If the nested-name-specifier is dependent and couldn't be
6402 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006403 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6404 return Context.getDependentNameType(Keyword,
6405 QualifierLoc.getNestedNameSpecifier(),
6406 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006407 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006408
John McCall77bb1aa2010-05-01 00:40:08 +00006409 // If the nested-name-specifier refers to the current instantiation,
6410 // the "typename" keyword itself is superfluous. In C++03, the
6411 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6412 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006413 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006414
John McCall77bb1aa2010-05-01 00:40:08 +00006415 if (RequireCompleteDeclContext(SS, Ctx))
6416 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006417
6418 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006419 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006420 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006421 unsigned DiagID = 0;
6422 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006423 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006424 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006425 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006426 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006427
6428 case LookupResult::FoundUnresolvedValue: {
6429 // We found a using declaration that is a value. Most likely, the using
6430 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006431 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006432 IILoc);
6433 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6434 << Name << Ctx << FullRange;
6435 if (UnresolvedUsingValueDecl *Using
6436 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006437 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006438 Diag(Loc, diag::note_using_value_decl_missing_typename)
6439 << FixItHint::CreateInsertion(Loc, "typename ");
6440 }
6441 }
6442 // Fall through to create a dependent typename type, from which we can recover
6443 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006444
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006445 case LookupResult::NotFoundInCurrentInstantiation:
6446 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006447 return Context.getDependentNameType(Keyword,
6448 QualifierLoc.getNestedNameSpecifier(),
6449 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006450
6451 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006452 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006453 // We found a type. Build an ElaboratedType, since the
6454 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006455 return Context.getElaboratedType(ETK_Typename,
6456 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006457 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006458 }
6459
6460 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006461 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006462 break;
6463
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006464
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006465 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006466 return QualType();
6467
Douglas Gregord57959a2009-03-27 23:10:48 +00006468 case LookupResult::FoundOverloaded:
6469 DiagID = diag::err_typename_nested_not_type;
6470 Referenced = *Result.begin();
6471 break;
6472
John McCall6e247262009-10-10 05:48:19 +00006473 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006474 return QualType();
6475 }
6476
6477 // If we get here, it's because name lookup did not find a
6478 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006479 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006480 IILoc);
6481 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006482 if (Referenced)
6483 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6484 << Name;
6485 return QualType();
6486}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006487
6488namespace {
6489 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006490 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006491 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006492 SourceLocation Loc;
6493 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006494
Douglas Gregor4a959d82009-08-06 16:20:37 +00006495 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006496 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006497
Mike Stump1eb44332009-09-09 15:08:12 +00006498 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006499 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006500 DeclarationName Entity)
6501 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006502 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006503
6504 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006505 /// transformed.
6506 ///
6507 /// For the purposes of type reconstruction, a type has already been
6508 /// transformed if it is NULL or if it is not dependent.
6509 bool AlreadyTransformed(QualType T) {
6510 return T.isNull() || !T->isDependentType();
6511 }
Mike Stump1eb44332009-09-09 15:08:12 +00006512
6513 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006514 /// rebuilt.
6515 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006516
Douglas Gregor4a959d82009-08-06 16:20:37 +00006517 /// \brief Returns the name of the entity whose type is being rebuilt.
6518 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006519
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006520 /// \brief Sets the "base" location and entity when that
6521 /// information is known based on another transformation.
6522 void setBase(SourceLocation Loc, DeclarationName Entity) {
6523 this->Loc = Loc;
6524 this->Entity = Entity;
6525 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006526 };
6527}
6528
Douglas Gregor4a959d82009-08-06 16:20:37 +00006529/// \brief Rebuilds a type within the context of the current instantiation.
6530///
Mike Stump1eb44332009-09-09 15:08:12 +00006531/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006532/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006533/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006534/// partial specialization thereof). This routine will rebuild that type now
6535/// that we have entered the declarator's scope, which may produce different
6536/// canonical types, e.g.,
6537///
6538/// \code
6539/// template<typename T>
6540/// struct X {
6541/// typedef T* pointer;
6542/// pointer data();
6543/// };
6544///
6545/// template<typename T>
6546/// typename X<T>::pointer X<T>::data() { ... }
6547/// \endcode
6548///
Douglas Gregor4714c122010-03-31 17:34:00 +00006549/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006550/// since we do not know that we can look into X<T> when we parsed the type.
6551/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006552/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006553/// as the canonical type of T*, allowing the return types of the out-of-line
6554/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006555TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6556 SourceLocation Loc,
6557 DeclarationName Name) {
6558 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006559 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006560
Douglas Gregor4a959d82009-08-06 16:20:37 +00006561 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6562 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006563}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006564
John McCall60d7b3a2010-08-24 06:29:42 +00006565ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006566 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6567 DeclarationName());
6568 return Rebuilder.TransformExpr(E);
6569}
6570
John McCall63b43852010-04-29 23:50:39 +00006571bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006572 if (SS.isInvalid())
6573 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006574
Douglas Gregor7e384942011-02-25 16:07:42 +00006575 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006576 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6577 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006578 NestedNameSpecifierLoc Rebuilt
6579 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6580 if (!Rebuilt)
6581 return true;
John McCall63b43852010-04-29 23:50:39 +00006582
Douglas Gregor7e384942011-02-25 16:07:42 +00006583 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006584 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006585}
6586
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006587/// \brief Produces a formatted string that describes the binding of
6588/// template parameters to template arguments.
6589std::string
6590Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6591 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006592 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006593}
6594
6595std::string
6596Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6597 const TemplateArgument *Args,
6598 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006599 llvm::SmallString<128> Str;
6600 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006601
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006602 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006603 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006604
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006605 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006606 if (I >= NumArgs)
6607 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006608
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006609 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006610 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006611 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006612 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006613
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006614 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006615 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006616 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006617 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006618 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006619
Douglas Gregor87dd6972010-12-20 16:52:59 +00006620 Out << " = ";
6621 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006622 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006623
6624 Out << ']';
6625 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006626}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006627
6628void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6629 if (!FD)
6630 return;
6631 FD->setLateTemplateParsed(Flag);
6632}
6633
6634bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6635 DeclContext *DC = CurContext;
6636
6637 while (DC) {
6638 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6639 const FunctionDecl *FD = RD->isLocalClass();
6640 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6641 } else if (DC->isTranslationUnit() || DC->isNamespace())
6642 return false;
6643
6644 DC = DC->getParent();
6645 }
6646 return false;
6647}