blob: d2641888bd933d6875179b175752a20e0c33592d [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();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000297 Found.clear();
298 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
299 Found.getLookupKind(), S, &SS,
300 LookupCtx, false,
301 CTC_CXXCasts)) {
302 Found.setLookupName(Corrected.getCorrection());
303 if (Corrected.getCorrectionDecl())
304 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000305 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000306 if (!Found.empty()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000307 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
308 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000309 if (LookupCtx)
310 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000311 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
312 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000313 else
314 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000315 << Name << CorrectedQuotedStr
316 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000317 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
318 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000319 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000320 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000321 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000322 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000323 }
324 }
325
Douglas Gregor312eadb2011-04-24 05:37:28 +0000326 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000327 if (Found.empty()) {
328 if (isDependent)
329 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000330 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000331 }
John McCallf7a1a742009-11-24 19:00:30 +0000332
333 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
334 // C++ [basic.lookup.classref]p1:
335 // [...] If the lookup in the class of the object expression finds a
336 // template, the name is also looked up in the context of the entire
337 // postfix-expression and [...]
338 //
339 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
340 LookupOrdinaryName);
341 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000342 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000343
John McCallf7a1a742009-11-24 19:00:30 +0000344 if (FoundOuter.empty()) {
345 // - if the name is not found, the name found in the class of the
346 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000347 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
348 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000349 // - if the name is found in the context of the entire
350 // postfix-expression and does not name a class template, the name
351 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000352 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000353 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000354 // - if the name found is a class template, it must refer to the same
355 // entity as the one found in the class of the object expression,
356 // otherwise the program is ill-formed.
357 if (!Found.isSingleResult() ||
358 Found.getFoundDecl()->getCanonicalDecl()
359 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000360 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000361 diag::ext_nested_name_member_ref_lookup_ambiguous)
362 << Found.getLookupName()
363 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000364 Diag(Found.getRepresentativeDecl()->getLocation(),
365 diag::note_ambig_member_ref_object_type)
366 << ObjectType;
367 Diag(FoundOuter.getFoundDecl()->getLocation(),
368 diag::note_ambig_member_ref_scope);
369
370 // Recover by taking the template that we found in the object
371 // expression's type.
372 }
373 }
374 }
375}
376
John McCall2f841ba2009-12-02 03:53:29 +0000377/// ActOnDependentIdExpression - Handle a dependent id-expression that
378/// was just parsed. This is only possible with an explicit scope
379/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000380ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000381Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000382 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000383 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000384 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000385 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000386
John McCall2f841ba2009-12-02 03:53:29 +0000387 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000388 isa<CXXMethodDecl>(DC) &&
389 cast<CXXMethodDecl>(DC)->isInstance()) {
390 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000391
John McCallf7a1a742009-11-24 19:00:30 +0000392 // Since the 'this' expression is synthesized, we don't need to
393 // perform the double-lookup check.
394 NamedDecl *FirstQualifierInScope = 0;
395
John McCallaa81e162009-12-01 22:10:20 +0000396 return Owned(CXXDependentScopeMemberExpr::Create(Context,
397 /*This*/ 0, ThisType,
398 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000399 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000400 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000401 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000402 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000403 TemplateArgs));
404 }
405
Abramo Bagnara25777432010-08-11 22:01:17 +0000406 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000407}
408
John McCall60d7b3a2010-08-24 06:29:42 +0000409ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000410Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000411 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000412 const TemplateArgumentListInfo *TemplateArgs) {
413 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000414 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000415 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000416 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000417}
418
Douglas Gregor72c3f312008-12-05 18:15:24 +0000419/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
420/// that the template parameter 'PrevDecl' is being shadowed by a new
421/// declaration at location Loc. Returns true to indicate that this is
422/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000423void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000424 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000425
426 // Microsoft Visual C++ permits template parameters to be shadowed.
Francois Pichet62ec1f22011-09-17 17:15:52 +0000427 if (getLangOptions().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000428 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000429
430 // C++ [temp.local]p4:
431 // A template-parameter shall not be redeclared within its
432 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000433 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000434 << cast<NamedDecl>(PrevDecl)->getDeclName();
435 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000436 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000437}
438
Douglas Gregor2943aed2009-03-03 04:44:36 +0000439/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000440/// the parameter D to reference the templated declaration and return a pointer
441/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000442TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
443 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
444 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000445 return Temp;
446 }
447 return 0;
448}
449
Douglas Gregorba68eca2011-01-05 17:40:24 +0000450ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
451 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000453 "Only template template arguments can be pack expansions here");
454 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
455 "Template template argument pack expansion without packs");
456 ParsedTemplateArgument Result(*this);
457 Result.EllipsisLoc = EllipsisLoc;
458 return Result;
459}
460
Douglas Gregor788cd062009-11-11 01:00:40 +0000461static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
462 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 switch (Arg.getKind()) {
465 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000466 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000467 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000468 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000469 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000470 return TemplateArgumentLoc(TemplateArgument(T), DI);
471 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000472
Douglas Gregor788cd062009-11-11 01:00:40 +0000473 case ParsedTemplateArgument::NonType: {
474 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
475 return TemplateArgumentLoc(TemplateArgument(E), E);
476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000477
Douglas Gregor788cd062009-11-11 01:00:40 +0000478 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000479 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000480 TemplateArgument TArg;
481 if (Arg.getEllipsisLoc().isValid())
482 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
483 else
484 TArg = Template;
485 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000486 Arg.getScopeSpec().getWithLocInContext(
487 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000488 Arg.getLocation(),
489 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000490 }
491 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000492
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000493 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000494 return TemplateArgumentLoc();
495}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000496
Douglas Gregor788cd062009-11-11 01:00:40 +0000497/// \brief Translates template arguments as provided by the parser
498/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000499void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
500 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000501 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000502 TemplateArgs.addArgument(translateTemplateArgument(*this,
503 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000504}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000505
Douglas Gregor72c3f312008-12-05 18:15:24 +0000506/// ActOnTypeParameter - Called when a C++ template type parameter
507/// (e.g., "typename T") has been parsed. Typename specifies whether
508/// the keyword "typename" was used to declare the type parameter
509/// (otherwise, "class" was used), and KeyLoc is the location of the
510/// "class" or "typename" keyword. ParamName is the name of the
511/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000512/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000513/// If the type parameter has a default argument, it will be added
514/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000515Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
516 SourceLocation EllipsisLoc,
517 SourceLocation KeyLoc,
518 IdentifierInfo *ParamName,
519 SourceLocation ParamNameLoc,
520 unsigned Depth, unsigned Position,
521 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000522 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523 assert(S->isTemplateParamScope() &&
524 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000525 bool Invalid = false;
526
527 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000528 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000529 LookupOrdinaryName,
530 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000531 if (PrevDecl && PrevDecl->isTemplateParameter()) {
532 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
533 PrevDecl = 0;
534 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000535 }
536
Douglas Gregorddc29e12009-02-06 22:42:48 +0000537 SourceLocation Loc = ParamNameLoc;
538 if (!ParamName)
539 Loc = KeyLoc;
540
Douglas Gregor72c3f312008-12-05 18:15:24 +0000541 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000542 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000543 KeyLoc, Loc, Depth, Position, ParamName,
544 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000545 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000546 if (Invalid)
547 Param->setInvalidDecl();
548
549 if (ParamName) {
550 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000551 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000552 IdResolver.AddDecl(Param);
553 }
554
Douglas Gregor61c4d282011-01-05 15:48:55 +0000555 // C++0x [temp.param]p9:
556 // A default template-argument may be specified for any kind of
557 // template-parameter that is not a template parameter pack.
558 if (DefaultArg && Ellipsis) {
559 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
560 DefaultArg = ParsedType();
561 }
562
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000563 // Handle the default argument, if provided.
564 if (DefaultArg) {
565 TypeSourceInfo *DefaultTInfo;
566 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000568 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000569
Douglas Gregor6f526752010-12-16 08:48:57 +0000570 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000571 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000572 UPPC_DefaultArgument))
573 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000574
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000575 // Check the template argument itself.
576 if (CheckTemplateArgument(Param, DefaultTInfo)) {
577 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000578 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000579 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000580
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000581 Param->setDefaultArgument(DefaultTInfo, false);
582 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000583
John McCalld226f652010-08-21 09:40:31 +0000584 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000585}
586
Douglas Gregor2943aed2009-03-03 04:44:36 +0000587/// \brief Check that the type of a non-type template parameter is
588/// well-formed.
589///
590/// \returns the (possibly-promoted) parameter type if valid;
591/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000592QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000593Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000594 // We don't allow variably-modified types as the type of non-type template
595 // parameters.
596 if (T->isVariablyModifiedType()) {
597 Diag(Loc, diag::err_variably_modified_nontype_template_param)
598 << T;
599 return QualType();
600 }
601
Douglas Gregor2943aed2009-03-03 04:44:36 +0000602 // C++ [temp.param]p4:
603 //
604 // A non-type template-parameter shall have one of the following
605 // (optionally cv-qualified) types:
606 //
607 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000608 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000609 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000610 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000611 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000612 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000613 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000614 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000615 // -- std::nullptr_t.
616 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000617 // If T is a dependent type, we can't do the check now, so we
618 // assume that it is well-formed.
619 T->isDependentType())
620 return T;
621 // C++ [temp.param]p8:
622 //
623 // A non-type template-parameter of type "array of T" or
624 // "function returning T" is adjusted to be of type "pointer to
625 // T" or "pointer to function returning T", respectively.
626 else if (T->isArrayType())
627 // FIXME: Keep the type prior to promotion?
628 return Context.getArrayDecayedType(T);
629 else if (T->isFunctionType())
630 // FIXME: Keep the type prior to promotion?
631 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000632
Douglas Gregor2943aed2009-03-03 04:44:36 +0000633 Diag(Loc, diag::err_template_nontype_parm_bad_type)
634 << T;
635
636 return QualType();
637}
638
John McCalld226f652010-08-21 09:40:31 +0000639Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
640 unsigned Depth,
641 unsigned Position,
642 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000643 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000644 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
645 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000646
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000647 assert(S->isTemplateParamScope() &&
648 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000649 bool Invalid = false;
650
651 IdentifierInfo *ParamName = D.getIdentifier();
652 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000653 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000654 LookupOrdinaryName,
655 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000656 if (PrevDecl && PrevDecl->isTemplateParameter()) {
657 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
658 PrevDecl = 0;
659 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000660 }
661
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000662 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
663 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000664 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000665 Invalid = true;
666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000667
Douglas Gregor10738d32010-12-23 23:51:58 +0000668 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000669 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000670 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000671 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000672 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000673 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000674 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000675 Param->setAccess(AS_public);
676
Douglas Gregor72c3f312008-12-05 18:15:24 +0000677 if (Invalid)
678 Param->setInvalidDecl();
679
680 if (D.getIdentifier()) {
681 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000682 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000683 IdResolver.AddDecl(Param);
684 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000685
Douglas Gregor61c4d282011-01-05 15:48:55 +0000686 // C++0x [temp.param]p9:
687 // A default template-argument may be specified for any kind of
688 // template-parameter that is not a template parameter pack.
689 if (Default && IsParameterPack) {
690 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
691 Default = 0;
692 }
693
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000694 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000695 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000696 // Check for unexpanded parameter packs.
697 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
698 return Param;
699
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000700 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000701 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
702 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000703 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000704 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000705 }
John Wiegley429bb272011-04-08 18:41:53 +0000706 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000707
John McCall9ae2f072010-08-23 23:25:46 +0000708 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000709 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000710
John McCalld226f652010-08-21 09:40:31 +0000711 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000712}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000713
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000714/// ActOnTemplateTemplateParameter - Called when a C++ template template
715/// parameter (e.g. T in template <template <typename> class T> class array)
716/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000717Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
718 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000719 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000720 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000721 IdentifierInfo *Name,
722 SourceLocation NameLoc,
723 unsigned Depth,
724 unsigned Position,
725 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000726 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000727 assert(S->isTemplateParamScope() &&
728 "Template template parameter not in template parameter scope!");
729
730 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000731 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000732 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000733 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000734 NameLoc.isInvalid()? TmpLoc : NameLoc,
735 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000736 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000737 Param->setAccess(AS_public);
738
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000739 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000740 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000741 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000742 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000743 IdResolver.AddDecl(Param);
744 }
745
Douglas Gregor6f526752010-12-16 08:48:57 +0000746 if (Params->size() == 0) {
747 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
748 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
749 Param->setInvalidDecl();
750 }
751
Douglas Gregor61c4d282011-01-05 15:48:55 +0000752 // C++0x [temp.param]p9:
753 // A default template-argument may be specified for any kind of
754 // template-parameter that is not a template parameter pack.
755 if (IsParameterPack && !Default.isInvalid()) {
756 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
757 Default = ParsedTemplateArgument();
758 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000759
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000760 if (!Default.isInvalid()) {
761 // Check only that we have a template template argument. We don't want to
762 // try to check well-formedness now, because our template template parameter
763 // might have dependent types in its template parameters, which we wouldn't
764 // be able to match now.
765 //
766 // If none of the template template parameter's template arguments mention
767 // other template parameters, we could actually perform more checking here.
768 // However, it isn't worth doing.
769 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
770 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
771 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
772 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000773 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775
Douglas Gregor6f526752010-12-16 08:48:57 +0000776 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000777 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000778 DefaultArg.getArgument().getAsTemplate(),
779 UPPC_DefaultArgument))
780 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000781
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000782 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000783 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000784
John McCalld226f652010-08-21 09:40:31 +0000785 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000786}
787
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000788/// ActOnTemplateParameterList - Builds a TemplateParameterList that
789/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000790TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000791Sema::ActOnTemplateParameterList(unsigned Depth,
792 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000793 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000794 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000795 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000796 SourceLocation RAngleLoc) {
797 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000798 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000799
Douglas Gregorddc29e12009-02-06 22:42:48 +0000800 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000801 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000802 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000803}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000804
John McCallb6217662010-03-15 10:12:16 +0000805static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
806 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000807 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000808}
809
John McCallf312b1e2010-08-26 23:41:50 +0000810DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000811Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000812 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000813 IdentifierInfo *Name, SourceLocation NameLoc,
814 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000815 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000816 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000817 unsigned NumOuterTemplateParamLists,
818 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000819 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000820 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000821 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000822 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000823
824 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000825 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000826 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000827
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000828 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
829 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000830
831 // There is no such thing as an unnamed class template.
832 if (!Name) {
833 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000834 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000835 }
836
837 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000838 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000839 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000840 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000841 if (SS.isNotEmpty() && !SS.isInvalid()) {
842 SemanticContext = computeDeclContext(SS, true);
843 if (!SemanticContext) {
844 // FIXME: Produce a reasonable diagnostic here
845 return true;
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
John McCall77bb1aa2010-05-01 00:40:08 +0000848 if (RequireCompleteDeclContext(SS, SemanticContext))
849 return true;
850
Douglas Gregor20606502011-10-14 15:31:12 +0000851 // If we're adding a template to a dependent context, we may need to
852 // rebuilding some of the types used within the template parameter list,
853 // now that we know what the current instantiation is.
854 if (SemanticContext->isDependentContext()) {
855 ContextRAII SavedContext(*this, SemanticContext);
856 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
857 Invalid = true;
858 }
859
John McCalla24dc2e2009-11-17 02:14:36 +0000860 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000861 } else {
862 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000863 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregor57265e32010-04-12 16:00:01 +0000866 if (Previous.isAmbiguous())
867 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000868
Douglas Gregorddc29e12009-02-06 22:42:48 +0000869 NamedDecl *PrevDecl = 0;
870 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000871 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000872
Douglas Gregorddc29e12009-02-06 22:42:48 +0000873 // If there is a previous declaration with the same name, check
874 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000875 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000876 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000877
878 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000879 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000880 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000881 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000882 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
883 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000885 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
886 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
887 PrevClassTemplate
888 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
889 ->getSpecializedTemplate();
890 }
891 }
892
John McCall65c49462009-12-18 11:25:59 +0000893 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000894 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895 // [...] When looking for a prior declaration of a class or a function
896 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000897 // function is neither a qualified name nor a template-id, scopes outside
898 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000899 if (!SS.isSet()) {
900 DeclContext *OutermostContext = CurContext;
901 while (!OutermostContext->isFileContext())
902 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000903
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000904 if (PrevDecl &&
905 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
906 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
907 SemanticContext = PrevDecl->getDeclContext();
908 } else {
909 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000910 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000911 // declaration.
912 PrevDecl = PrevClassTemplate = 0;
913 SemanticContext = OutermostContext;
914 }
John McCalle129d442009-12-17 23:21:11 +0000915 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000916
John McCalle129d442009-12-17 23:21:11 +0000917 if (CurContext->isDependentContext()) {
918 // If this is a dependent context, we don't want to link the friend
919 // class template to the template in scope, because that would perform
920 // checking of the template parameter lists that can't be performed
921 // until the outer context is instantiated.
922 PrevDecl = PrevClassTemplate = 0;
923 }
924 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
925 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000926
Douglas Gregorddc29e12009-02-06 22:42:48 +0000927 if (PrevClassTemplate) {
928 // Ensure that the template parameter lists are compatible.
929 if (!TemplateParameterListsAreEqual(TemplateParams,
930 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000931 /*Complain=*/true,
932 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000933 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000934
935 // C++ [temp.class]p4:
936 // In a redeclaration, partial specialization, explicit
937 // specialization or explicit instantiation of a class template,
938 // the class-key shall agree in kind with the original class
939 // template declaration (7.1.5.3).
940 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000941 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
942 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000943 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000944 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000945 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000946 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000947 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000948 }
949
Douglas Gregorddc29e12009-02-06 22:42:48 +0000950 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000951 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000952 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000953 Diag(NameLoc, diag::err_redefinition) << Name;
954 Diag(Def->getLocation(), diag::note_previous_definition);
955 // FIXME: Would it make sense to try to "forget" the previous
956 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000957 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000958 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000959 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000960 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
961 // Maybe we will complain about the shadowed template parameter.
962 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
963 // Just pretend that we didn't see the previous declaration.
964 PrevDecl = 0;
965 } else if (PrevDecl) {
966 // C++ [temp]p5:
967 // A class template shall not have the same name as any other
968 // template, class, function, object, enumeration, enumerator,
969 // namespace, or type in the same scope (3.3), except as specified
970 // in (14.5.4).
971 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
972 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000973 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000974 }
975
Douglas Gregord684b002009-02-10 19:49:53 +0000976 // Check the template parameter list of this declaration, possibly
977 // merging in the template parameter list from the previous class
978 // template declaration.
979 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000980 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000981 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000982 SemanticContext->isRecord() &&
983 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000984 ? TPC_ClassTemplateMember
985 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000986 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Douglas Gregor57265e32010-04-12 16:00:01 +0000988 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000989 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000990 // template out-of-line.
991 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
992 !(TUK == TUK_Friend && CurContext->isDependentContext()))
993 Diag(NameLoc, diag::err_member_def_does_not_match)
994 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000995 }
996
Mike Stump1eb44332009-09-09 15:08:12 +0000997 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000998 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000999 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001000 PrevClassTemplate->getTemplatedDecl() : 0,
1001 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001002 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001003 if (NumOuterTemplateParamLists > 0)
1004 NewClass->setTemplateParameterListsInfo(Context,
1005 NumOuterTemplateParamLists,
1006 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001007
1008 ClassTemplateDecl *NewTemplate
1009 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1010 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001011 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001012 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001013
1014 if (PrevClassTemplate && PrevClassTemplate->isModulePrivate()) {
1015 NewTemplate->setModulePrivate();
Douglas Gregore7612302011-09-09 19:05:14 +00001016 } else if (ModulePrivateLoc.isValid()) {
1017 if (PrevClassTemplate && !PrevClassTemplate->isModulePrivate())
1018 diagnoseModulePrivateRedeclaration(NewTemplate, PrevClassTemplate,
1019 ModulePrivateLoc);
1020 else
1021 NewTemplate->setModulePrivate();
1022 }
Douglas Gregor8d267c52011-09-09 02:06:17 +00001023
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001024 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001025 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001026 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001027 assert(T->isDependentType() && "Class template type is not dependent?");
1028 (void)T;
1029
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001030 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001031 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001032 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001033 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1034 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001035
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001036 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001037 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001038 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Douglas Gregorddc29e12009-02-06 22:42:48 +00001040 // Set the lexical context of these templates
1041 NewClass->setLexicalDeclContext(CurContext);
1042 NewTemplate->setLexicalDeclContext(CurContext);
1043
John McCall0f434ec2009-07-31 02:45:11 +00001044 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001045 NewClass->startDefinition();
1046
1047 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001048 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001049
John McCall05b23ea2009-09-14 21:59:20 +00001050 if (TUK != TUK_Friend)
1051 PushOnScopeChains(NewTemplate, S);
1052 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001053 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001054 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001055 NewClass->setAccess(PrevClassTemplate->getAccess());
1056 }
John McCall05b23ea2009-09-14 21:59:20 +00001057
Douglas Gregord85bea22009-09-26 06:47:28 +00001058 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1059 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001060
John McCall05b23ea2009-09-14 21:59:20 +00001061 // Friend templates are visible in fairly strange ways.
1062 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001063 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001064 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1065 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1066 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001067 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001068 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001069
Douglas Gregord85bea22009-09-26 06:47:28 +00001070 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1071 NewClass->getLocation(),
1072 NewTemplate,
1073 /*FIXME:*/NewClass->getLocation());
1074 Friend->setAccess(AS_public);
1075 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001076 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001077
Douglas Gregord684b002009-02-10 19:49:53 +00001078 if (Invalid) {
1079 NewTemplate->setInvalidDecl();
1080 NewClass->setInvalidDecl();
1081 }
John McCalld226f652010-08-21 09:40:31 +00001082 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001083}
1084
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001085/// \brief Diagnose the presence of a default template argument on a
1086/// template parameter, which is ill-formed in certain contexts.
1087///
1088/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001089static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001090 Sema::TemplateParamListContext TPC,
1091 SourceLocation ParamLoc,
1092 SourceRange DefArgRange) {
1093 switch (TPC) {
1094 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001095 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001096 return false;
1097
1098 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001099 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001100 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001101 // A default template-argument shall not be specified in a
1102 // function template declaration or a function template
1103 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001104 // If a friend function template declaration specifies a default
1105 // template-argument, that declaration shall be a definition and shall be
1106 // the only declaration of the function template in the translation unit.
1107 // (C++98/03 doesn't have this wording; see DR226).
Richard Smithebaf0e62011-10-18 20:49:44 +00001108 S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
1109 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1110 : diag::ext_template_parameter_default_in_function_template)
1111 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001112 return false;
1113
1114 case Sema::TPC_ClassTemplateMember:
1115 // C++0x [temp.param]p9:
1116 // A default template-argument shall not be specified in the
1117 // template-parameter-lists of the definition of a member of a
1118 // class template that appears outside of the member's class.
1119 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1120 << DefArgRange;
1121 return true;
1122
1123 case Sema::TPC_FriendFunctionTemplate:
1124 // C++ [temp.param]p9:
1125 // A default template-argument shall not be specified in a
1126 // friend template declaration.
1127 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1128 << DefArgRange;
1129 return true;
1130
1131 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1132 // for friend function templates if there is only a single
1133 // declaration (and it is a definition). Strange!
1134 }
1135
1136 return false;
1137}
1138
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001139/// \brief Check for unexpanded parameter packs within the template parameters
1140/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001141static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1142 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001143 TemplateParameterList *Params = TTP->getTemplateParameters();
1144 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1145 NamedDecl *P = Params->getParam(I);
1146 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001147 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001148 NTTP->getTypeSourceInfo(),
1149 Sema::UPPC_NonTypeTemplateParameterType))
1150 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001151
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001152 continue;
1153 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001154
1155 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001156 = dyn_cast<TemplateTemplateParmDecl>(P))
1157 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1158 return true;
1159 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001160
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001161 return false;
1162}
1163
Douglas Gregord684b002009-02-10 19:49:53 +00001164/// \brief Checks the validity of a template parameter list, possibly
1165/// considering the template parameter list from a previous
1166/// declaration.
1167///
1168/// If an "old" template parameter list is provided, it must be
1169/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1170/// template parameter list.
1171///
1172/// \param NewParams Template parameter list for a new template
1173/// declaration. This template parameter list will be updated with any
1174/// default arguments that are carried through from the previous
1175/// template parameter list.
1176///
1177/// \param OldParams If provided, template parameter list from a
1178/// previous declaration of the same template. Default template
1179/// arguments will be merged from the old template parameter list to
1180/// the new template parameter list.
1181///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001182/// \param TPC Describes the context in which we are checking the given
1183/// template parameter list.
1184///
Douglas Gregord684b002009-02-10 19:49:53 +00001185/// \returns true if an error occurred, false otherwise.
1186bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001187 TemplateParameterList *OldParams,
1188 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001189 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Douglas Gregord684b002009-02-10 19:49:53 +00001191 // C++ [temp.param]p10:
1192 // The set of default template-arguments available for use with a
1193 // template declaration or definition is obtained by merging the
1194 // default arguments from the definition (if in scope) and all
1195 // declarations in scope in the same way default function
1196 // arguments are (8.3.6).
1197 bool SawDefaultArgument = false;
1198 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001199
Mike Stump1a35fde2009-02-11 23:03:27 +00001200 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001201 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001202 if (OldParams)
1203 OldParam = OldParams->begin();
1204
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001205 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001206 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1207 NewParamEnd = NewParams->end();
1208 NewParam != NewParamEnd; ++NewParam) {
1209 // Variables used to diagnose redundant default arguments
1210 bool RedundantDefaultArg = false;
1211 SourceLocation OldDefaultLoc;
1212 SourceLocation NewDefaultLoc;
1213
David Blaikie1368e582011-10-19 05:19:50 +00001214 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001215 bool MissingDefaultArg = false;
1216
David Blaikie1368e582011-10-19 05:19:50 +00001217 // Variable used to diagnose non-final parameter packs
1218 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001219
Douglas Gregord684b002009-02-10 19:49:53 +00001220 if (TemplateTypeParmDecl *NewTypeParm
1221 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001222 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001223 if (NewTypeParm->hasDefaultArgument() &&
1224 DiagnoseDefaultTemplateArgument(*this, TPC,
1225 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001226 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001227 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001228 NewTypeParm->removeDefaultArgument();
1229
1230 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001231 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001232 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Anders Carlsson49d25572009-06-12 23:20:15 +00001234 if (NewTypeParm->isParameterPack()) {
1235 assert(!NewTypeParm->hasDefaultArgument() &&
1236 "Parameter packs can't have a default argument!");
1237 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001238 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001239 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001240 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1241 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1242 SawDefaultArgument = true;
1243 RedundantDefaultArg = true;
1244 PreviousDefaultArgLoc = NewDefaultLoc;
1245 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1246 // Merge the default argument from the old declaration to the
1247 // new declaration.
1248 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001249 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001250 true);
1251 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1252 } else if (NewTypeParm->hasDefaultArgument()) {
1253 SawDefaultArgument = true;
1254 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1255 } else if (SawDefaultArgument)
1256 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001257 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001258 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001259 // Check for unexpanded parameter packs.
1260 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001261 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001262 UPPC_NonTypeTemplateParameterType)) {
1263 Invalid = true;
1264 continue;
1265 }
1266
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001267 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001268 if (NewNonTypeParm->hasDefaultArgument() &&
1269 DiagnoseDefaultTemplateArgument(*this, TPC,
1270 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001271 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001272 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001273 }
1274
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001275 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001276 NonTypeTemplateParmDecl *OldNonTypeParm
1277 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001278 if (NewNonTypeParm->isParameterPack()) {
1279 assert(!NewNonTypeParm->hasDefaultArgument() &&
1280 "Parameter packs can't have a default argument!");
1281 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001282 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001283 NewNonTypeParm->hasDefaultArgument()) {
1284 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1285 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1286 SawDefaultArgument = true;
1287 RedundantDefaultArg = true;
1288 PreviousDefaultArgLoc = NewDefaultLoc;
1289 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1290 // Merge the default argument from the old declaration to the
1291 // new declaration.
1292 SawDefaultArgument = true;
1293 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001294 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001295 // parameter.
1296 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001297 OldNonTypeParm->getDefaultArgument(),
1298 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001299 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1300 } else if (NewNonTypeParm->hasDefaultArgument()) {
1301 SawDefaultArgument = true;
1302 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1303 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001304 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001305 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001306 TemplateTemplateParmDecl *NewTemplateParm
1307 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001308
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001309 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001310 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001311 Invalid = true;
1312 continue;
1313 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001314
David Blaikie1368e582011-10-19 05:19:50 +00001315 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001316 if (NewTemplateParm->hasDefaultArgument() &&
1317 DiagnoseDefaultTemplateArgument(*this, TPC,
1318 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001319 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001320 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001321
1322 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001323 TemplateTemplateParmDecl *OldTemplateParm
1324 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001325 if (NewTemplateParm->isParameterPack()) {
1326 assert(!NewTemplateParm->hasDefaultArgument() &&
1327 "Parameter packs can't have a default argument!");
1328 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001329 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001330 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001331 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1332 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001333 SawDefaultArgument = true;
1334 RedundantDefaultArg = true;
1335 PreviousDefaultArgLoc = NewDefaultLoc;
1336 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1337 // Merge the default argument from the old declaration to the
1338 // new declaration.
1339 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001340 // FIXME: We need to create a new kind of "default argument" expression
1341 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001342 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001343 OldTemplateParm->getDefaultArgument(),
1344 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001345 PreviousDefaultArgLoc
1346 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001347 } else if (NewTemplateParm->hasDefaultArgument()) {
1348 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001349 PreviousDefaultArgLoc
1350 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001351 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001352 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001353 }
1354
David Blaikie1368e582011-10-19 05:19:50 +00001355 // C++0x [temp.param]p11:
1356 // If a template parameter of a primary class template or alias template
1357 // is a template parameter pack, it shall be the last template parameter.
1358 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1359 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1360 Diag((*NewParam)->getLocation(),
1361 diag::err_template_param_pack_must_be_last_template_parameter);
1362 Invalid = true;
1363 }
1364
Douglas Gregord684b002009-02-10 19:49:53 +00001365 if (RedundantDefaultArg) {
1366 // C++ [temp.param]p12:
1367 // A template-parameter shall not be given default arguments
1368 // by two different declarations in the same scope.
1369 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1370 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1371 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001372 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001373 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001374 // If a template-parameter of a class template has a default
1375 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001376 // have a default template-argument supplied or be a template parameter
1377 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001378 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001379 diag::err_template_param_default_arg_missing);
1380 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1381 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001382 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001383 }
1384
1385 // If we have an old template parameter list that we're merging
1386 // in, move on to the next parameter.
1387 if (OldParams)
1388 ++OldParam;
1389 }
1390
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001391 // We were missing some default arguments at the end of the list, so remove
1392 // all of the default arguments.
1393 if (RemoveDefaultArguments) {
1394 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1395 NewParamEnd = NewParams->end();
1396 NewParam != NewParamEnd; ++NewParam) {
1397 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1398 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001399 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001400 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1401 NTTP->removeDefaultArgument();
1402 else
1403 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1404 }
1405 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001406
Douglas Gregord684b002009-02-10 19:49:53 +00001407 return Invalid;
1408}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001409
John McCall4e2cbb22010-10-20 05:44:58 +00001410namespace {
1411
1412/// A class which looks for a use of a certain level of template
1413/// parameter.
1414struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1415 typedef RecursiveASTVisitor<DependencyChecker> super;
1416
1417 unsigned Depth;
1418 bool Match;
1419
1420 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1421 NamedDecl *ND = Params->getParam(0);
1422 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1423 Depth = PD->getDepth();
1424 } else if (NonTypeTemplateParmDecl *PD =
1425 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1426 Depth = PD->getDepth();
1427 } else {
1428 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1429 }
1430 }
1431
1432 bool Matches(unsigned ParmDepth) {
1433 if (ParmDepth >= Depth) {
1434 Match = true;
1435 return true;
1436 }
1437 return false;
1438 }
1439
1440 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1441 return !Matches(T->getDepth());
1442 }
1443
1444 bool TraverseTemplateName(TemplateName N) {
1445 if (TemplateTemplateParmDecl *PD =
1446 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1447 if (Matches(PD->getDepth())) return false;
1448 return super::TraverseTemplateName(N);
1449 }
1450
1451 bool VisitDeclRefExpr(DeclRefExpr *E) {
1452 if (NonTypeTemplateParmDecl *PD =
1453 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1454 if (PD->getDepth() == Depth) {
1455 Match = true;
1456 return false;
1457 }
1458 }
1459 return super::VisitDeclRefExpr(E);
1460 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001461
1462 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1463 return TraverseType(T->getInjectedSpecializationType());
1464 }
John McCall4e2cbb22010-10-20 05:44:58 +00001465};
1466}
1467
Douglas Gregorc8406492011-05-10 18:27:06 +00001468/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001469/// list.
1470static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001471DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001472 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001473 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001474 return Checker.Match;
1475}
1476
Douglas Gregorc8406492011-05-10 18:27:06 +00001477// Find the source range corresponding to the named type in the given
1478// nested-name-specifier, if any.
1479static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1480 QualType T,
1481 const CXXScopeSpec &SS) {
1482 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1483 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1484 if (const Type *CurType = NNS->getAsType()) {
1485 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1486 return NNSLoc.getTypeLoc().getSourceRange();
1487 } else
1488 break;
1489
1490 NNSLoc = NNSLoc.getPrefix();
1491 }
1492
1493 return SourceRange();
1494}
1495
Mike Stump1eb44332009-09-09 15:08:12 +00001496/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001497/// specifier, returning the template parameter list that applies to the
1498/// name.
1499///
1500/// \param DeclStartLoc the start of the declaration that has a scope
1501/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001502///
Douglas Gregorc8406492011-05-10 18:27:06 +00001503/// \param DeclLoc The location of the declaration itself.
1504///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001505/// \param SS the scope specifier that will be matched to the given template
1506/// parameter lists. This scope specifier precedes a qualified name that is
1507/// being declared.
1508///
1509/// \param ParamLists the template parameter lists, from the outermost to the
1510/// innermost template parameter lists.
1511///
1512/// \param NumParamLists the number of template parameter lists in ParamLists.
1513///
John McCall77e8b112010-04-13 20:37:33 +00001514/// \param IsFriend Whether to apply the slightly different rules for
1515/// matching template parameters to scope specifiers in friend
1516/// declarations.
1517///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001518/// \param IsExplicitSpecialization will be set true if the entity being
1519/// declared is an explicit specialization, false otherwise.
1520///
Mike Stump1eb44332009-09-09 15:08:12 +00001521/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001522/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001523/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001524/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001525/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001526/// itself a template).
1527TemplateParameterList *
1528Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001529 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001530 const CXXScopeSpec &SS,
1531 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001532 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001533 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001534 bool &IsExplicitSpecialization,
1535 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001536 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001537 Invalid = false;
1538
1539 // The sequence of nested types to which we will match up the template
1540 // parameter lists. We first build this list by starting with the type named
1541 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001542 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001543 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001544 if (SS.getScopeRep()) {
1545 if (CXXRecordDecl *Record
1546 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1547 T = Context.getTypeDeclType(Record);
1548 else
1549 T = QualType(SS.getScopeRep()->getAsType(), 0);
1550 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001551
1552 // If we found an explicit specialization that prevents us from needing
1553 // 'template<>' headers, this will be set to the location of that
1554 // explicit specialization.
1555 SourceLocation ExplicitSpecLoc;
1556
1557 while (!T.isNull()) {
1558 NestedTypes.push_back(T);
1559
1560 // Retrieve the parent of a record type.
1561 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1562 // If this type is an explicit specialization, we're done.
1563 if (ClassTemplateSpecializationDecl *Spec
1564 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1565 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1566 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1567 ExplicitSpecLoc = Spec->getLocation();
1568 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001569 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001570 } else if (Record->getTemplateSpecializationKind()
1571 == TSK_ExplicitSpecialization) {
1572 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001573 break;
1574 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001575
1576 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1577 T = Context.getTypeDeclType(Parent);
1578 else
1579 T = QualType();
1580 continue;
1581 }
1582
1583 if (const TemplateSpecializationType *TST
1584 = T->getAs<TemplateSpecializationType>()) {
1585 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1586 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1587 T = Context.getTypeDeclType(Parent);
1588 else
1589 T = QualType();
1590 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001591 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001592 }
1593
1594 // Look one step prior in a dependent template specialization type.
1595 if (const DependentTemplateSpecializationType *DependentTST
1596 = T->getAs<DependentTemplateSpecializationType>()) {
1597 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1598 T = QualType(NNS->getAsType(), 0);
1599 else
1600 T = QualType();
1601 continue;
1602 }
1603
1604 // Look one step prior in a dependent name type.
1605 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1606 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1607 T = QualType(NNS->getAsType(), 0);
1608 else
1609 T = QualType();
1610 continue;
1611 }
1612
1613 // Retrieve the parent of an enumeration type.
1614 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1615 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1616 // check here.
1617 EnumDecl *Enum = EnumT->getDecl();
1618
1619 // Get to the parent type.
1620 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1621 T = Context.getTypeDeclType(Parent);
1622 else
1623 T = QualType();
1624 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Douglas Gregorc8406492011-05-10 18:27:06 +00001627 T = QualType();
1628 }
1629 // Reverse the nested types list, since we want to traverse from the outermost
1630 // to the innermost while checking template-parameter-lists.
1631 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001632
Douglas Gregorc8406492011-05-10 18:27:06 +00001633 // C++0x [temp.expl.spec]p17:
1634 // A member or a member template may be nested within many
1635 // enclosing class templates. In an explicit specialization for
1636 // such a member, the member declaration shall be preceded by a
1637 // template<> for each enclosing class template that is
1638 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001639 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001640 unsigned ParamIdx = 0;
1641 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1642 ++TypeIdx) {
1643 T = NestedTypes[TypeIdx];
1644
1645 // Whether we expect a 'template<>' header.
1646 bool NeedEmptyTemplateHeader = false;
1647
1648 // Whether we expect a template header with parameters.
1649 bool NeedNonemptyTemplateHeader = false;
1650
1651 // For a dependent type, the set of template parameters that we
1652 // expect to see.
1653 TemplateParameterList *ExpectedTemplateParams = 0;
1654
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001655 // C++0x [temp.expl.spec]p15:
1656 // A member or a member template may be nested within many enclosing
1657 // class templates. In an explicit specialization for such a member, the
1658 // member declaration shall be preceded by a template<> for each
1659 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001660 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1661 if (ClassTemplatePartialSpecializationDecl *Partial
1662 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1663 ExpectedTemplateParams = Partial->getTemplateParameters();
1664 NeedNonemptyTemplateHeader = true;
1665 } else if (Record->isDependentType()) {
1666 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001667 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001668 ->getTemplateParameters();
1669 NeedNonemptyTemplateHeader = true;
1670 }
1671 } else if (ClassTemplateSpecializationDecl *Spec
1672 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1673 // C++0x [temp.expl.spec]p4:
1674 // Members of an explicitly specialized class template are defined
1675 // in the same manner as members of normal classes, and not using
1676 // the template<> syntax.
1677 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1678 NeedEmptyTemplateHeader = true;
1679 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001680 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001681 } else if (Record->getTemplateSpecializationKind()) {
1682 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001683 != TSK_ExplicitSpecialization &&
1684 TypeIdx == NumTypes - 1)
1685 IsExplicitSpecialization = true;
1686
1687 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001688 }
1689 } else if (const TemplateSpecializationType *TST
1690 = T->getAs<TemplateSpecializationType>()) {
1691 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1692 ExpectedTemplateParams = Template->getTemplateParameters();
1693 NeedNonemptyTemplateHeader = true;
1694 }
1695 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1696 // FIXME: We actually could/should check the template arguments here
1697 // against the corresponding template parameter list.
1698 NeedNonemptyTemplateHeader = false;
1699 }
1700
Douglas Gregor89b9f102011-06-06 15:22:55 +00001701 // C++ [temp.expl.spec]p16:
1702 // In an explicit specialization declaration for a member of a class
1703 // template or a member template that ap- pears in namespace scope, the
1704 // member template and some of its enclosing class templates may remain
1705 // unspecialized, except that the declaration shall not explicitly
1706 // specialize a class member template if its en- closing class templates
1707 // are not explicitly specialized as well.
1708 if (ParamIdx < NumParamLists) {
1709 if (ParamLists[ParamIdx]->size() == 0) {
1710 if (SawNonEmptyTemplateParameterList) {
1711 Diag(DeclLoc, diag::err_specialize_member_of_template)
1712 << ParamLists[ParamIdx]->getSourceRange();
1713 Invalid = true;
1714 IsExplicitSpecialization = false;
1715 return 0;
1716 }
1717 } else
1718 SawNonEmptyTemplateParameterList = true;
1719 }
1720
Douglas Gregorc8406492011-05-10 18:27:06 +00001721 if (NeedEmptyTemplateHeader) {
1722 // If we're on the last of the types, and we need a 'template<>' header
1723 // here, then it's an explicit specialization.
1724 if (TypeIdx == NumTypes - 1)
1725 IsExplicitSpecialization = true;
1726
1727 if (ParamIdx < NumParamLists) {
1728 if (ParamLists[ParamIdx]->size() > 0) {
1729 // The header has template parameters when it shouldn't. Complain.
1730 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1731 diag::err_template_param_list_matches_nontemplate)
1732 << T
1733 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1734 ParamLists[ParamIdx]->getRAngleLoc())
1735 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1736 Invalid = true;
1737 return 0;
1738 }
1739
1740 // Consume this template header.
1741 ++ParamIdx;
1742 continue;
1743 }
1744
1745 if (!IsFriend) {
1746 // We don't have a template header, but we should.
1747 SourceLocation ExpectedTemplateLoc;
1748 if (NumParamLists > 0)
1749 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1750 else
1751 ExpectedTemplateLoc = DeclStartLoc;
1752
1753 Diag(DeclLoc, diag::err_template_spec_needs_header)
1754 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1755 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1756 }
1757
1758 continue;
1759 }
1760
1761 if (NeedNonemptyTemplateHeader) {
1762 // In friend declarations we can have template-ids which don't
1763 // depend on the corresponding template parameter lists. But
1764 // assume that empty parameter lists are supposed to match this
1765 // template-id.
1766 if (IsFriend && T->isDependentType()) {
1767 if (ParamIdx < NumParamLists &&
1768 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1769 ExpectedTemplateParams = 0;
1770 else
1771 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001772 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001773
Douglas Gregorc8406492011-05-10 18:27:06 +00001774 if (ParamIdx < NumParamLists) {
1775 // Check the template parameter list, if we can.
1776 if (ExpectedTemplateParams &&
1777 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1778 ExpectedTemplateParams,
1779 true, TPL_TemplateMatch))
1780 Invalid = true;
1781
1782 if (!Invalid &&
1783 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1784 TPC_ClassTemplateMember))
1785 Invalid = true;
1786
1787 ++ParamIdx;
1788 continue;
1789 }
1790
1791 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1792 << T
1793 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1794 Invalid = true;
1795 continue;
1796 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001797 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001798
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001799 // If there were at least as many template-ids as there were template
1800 // parameter lists, then there are no template parameter lists remaining for
1801 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001802 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001803 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001805 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001806 if (ParamIdx < NumParamLists - 1) {
1807 bool HasAnyExplicitSpecHeader = false;
1808 bool AllExplicitSpecHeaders = true;
1809 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1810 if (ParamLists[I]->size() == 0)
1811 HasAnyExplicitSpecHeader = true;
1812 else
1813 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001814 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001815
1816 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1817 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1818 : diag::err_template_spec_extra_headers)
1819 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1820 ParamLists[NumParamLists - 2]->getRAngleLoc());
1821
1822 // If there was a specialization somewhere, such that 'template<>' is
1823 // not required, and there were any 'template<>' headers, note where the
1824 // specialization occurred.
1825 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1826 Diag(ExplicitSpecLoc,
1827 diag::note_explicit_template_spec_does_not_need_header)
1828 << NestedTypes.back();
1829
1830 // We have a template parameter list with no corresponding scope, which
1831 // means that the resulting template declaration can't be instantiated
1832 // properly (we'll end up with dependent nodes when we shouldn't).
1833 if (!AllExplicitSpecHeaders)
1834 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001835 }
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Douglas Gregor89b9f102011-06-06 15:22:55 +00001837 // C++ [temp.expl.spec]p16:
1838 // In an explicit specialization declaration for a member of a class
1839 // template or a member template that ap- pears in namespace scope, the
1840 // member template and some of its enclosing class templates may remain
1841 // unspecialized, except that the declaration shall not explicitly
1842 // specialize a class member template if its en- closing class templates
1843 // are not explicitly specialized as well.
1844 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1845 SawNonEmptyTemplateParameterList) {
1846 Diag(DeclLoc, diag::err_specialize_member_of_template)
1847 << ParamLists[ParamIdx]->getSourceRange();
1848 Invalid = true;
1849 IsExplicitSpecialization = false;
1850 return 0;
1851 }
1852
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001853 // Return the last template parameter list, which corresponds to the
1854 // entity being declared.
1855 return ParamLists[NumParamLists - 1];
1856}
1857
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001858void Sema::NoteAllFoundTemplates(TemplateName Name) {
1859 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1860 Diag(Template->getLocation(), diag::note_template_declared_here)
1861 << (isa<FunctionTemplateDecl>(Template)? 0
1862 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001863 : isa<TypeAliasTemplateDecl>(Template)? 2
1864 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001865 << Template->getDeclName();
1866 return;
1867 }
1868
1869 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1870 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1871 IEnd = OST->end();
1872 I != IEnd; ++I)
1873 Diag((*I)->getLocation(), diag::note_template_declared_here)
1874 << 0 << (*I)->getDeclName();
1875
1876 return;
1877 }
1878}
1879
1880
Douglas Gregor7532dc62009-03-30 22:58:21 +00001881QualType Sema::CheckTemplateIdType(TemplateName Name,
1882 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001883 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001884 DependentTemplateName *DTN
1885 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001886 if (DTN && DTN->isIdentifier())
1887 // When building a template-id where the template-name is dependent,
1888 // assume the template is a type template. Either our assumption is
1889 // correct, or the code is ill-formed and will be diagnosed when the
1890 // dependent name is substituted.
1891 return Context.getDependentTemplateSpecializationType(ETK_None,
1892 DTN->getQualifier(),
1893 DTN->getIdentifier(),
1894 TemplateArgs);
1895
Douglas Gregor7532dc62009-03-30 22:58:21 +00001896 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001897 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1898 // We might have a substituted template template parameter pack. If so,
1899 // build a template specialization type for it.
1900 if (Name.getAsSubstTemplateTemplateParmPack())
1901 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001902
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001903 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1904 << Name;
1905 NoteAllFoundTemplates(Name);
1906 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001907 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001908
Douglas Gregor40808ce2009-03-09 23:48:35 +00001909 // Check that the template argument list is well-formed for this
1910 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001911 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001912 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001913 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001914 return QualType();
1915
Douglas Gregor910f8002010-11-07 23:05:16 +00001916 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001917 "Converted template argument list is too short!");
1918
1919 QualType CanonType;
1920
Douglas Gregor561f8122011-07-01 01:22:09 +00001921 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001922 if (TypeAliasTemplateDecl *AliasTemplate
1923 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1924 // Find the canonical type for this type alias template specialization.
1925 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1926 if (Pattern->isInvalidDecl())
1927 return QualType();
1928
1929 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1930 Converted.data(), Converted.size());
1931
1932 // Only substitute for the innermost template argument list.
1933 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001934 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001935 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1936 for (unsigned I = 0; I < Depth; ++I)
1937 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001938
1939 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1940 CanonType = SubstType(Pattern->getUnderlyingType(),
1941 TemplateArgLists, AliasTemplate->getLocation(),
1942 AliasTemplate->getDeclName());
1943 if (CanonType.isNull())
1944 return QualType();
1945 } else if (Name.isDependent() ||
1946 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001947 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001948 // This class template specialization is a dependent
1949 // type. Therefore, its canonical type is another class template
1950 // specialization type that contains all of the converted
1951 // arguments in canonical form. This ensures that, e.g., A<T> and
1952 // A<T, T> have identical types when A is declared as:
1953 //
1954 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001955 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001956 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001957 Converted.data(),
1958 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Douglas Gregor1275ae02009-07-28 23:00:59 +00001960 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001961 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001962 // In the future, we need to teach getTemplateSpecializationType to only
1963 // build the canonical type and return that to us.
1964 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001965
1966 // This might work out to be a current instantiation, in which
1967 // case the canonical type needs to be the InjectedClassNameType.
1968 //
1969 // TODO: in theory this could be a simple hashtable lookup; most
1970 // changes to CurContext don't change the set of current
1971 // instantiations.
1972 if (isa<ClassTemplateDecl>(Template)) {
1973 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1974 // If we get out to a namespace, we're done.
1975 if (Ctx->isFileContext()) break;
1976
1977 // If this isn't a record, keep looking.
1978 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1979 if (!Record) continue;
1980
1981 // Look for one of the two cases with InjectedClassNameTypes
1982 // and check whether it's the same template.
1983 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1984 !Record->getDescribedClassTemplate())
1985 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001986
John McCall31f17ec2010-04-27 00:57:59 +00001987 // Fetch the injected class name type and check whether its
1988 // injected type is equal to the type we just built.
1989 QualType ICNT = Context.getTypeDeclType(Record);
1990 QualType Injected = cast<InjectedClassNameType>(ICNT)
1991 ->getInjectedSpecializationType();
1992
1993 if (CanonType != Injected->getCanonicalTypeInternal())
1994 continue;
1995
1996 // If so, the canonical type of this TST is the injected
1997 // class name type of the record we just found.
1998 assert(ICNT.isCanonical());
1999 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002000 break;
2001 }
2002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002004 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002005 // Find the class template specialization declaration that
2006 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002007 void *InsertPos = 0;
2008 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002009 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002010 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002011 if (!Decl) {
2012 // This is the first time we have referenced this class template
2013 // specialization. Create the canonical declaration and add it to
2014 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002015 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002016 ClassTemplate->getTemplatedDecl()->getTagKind(),
2017 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002018 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002019 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002020 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002021 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002022 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002023 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002024 Decl->setLexicalDeclContext(CurContext);
2025 }
2026
2027 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002028 assert(isa<RecordType>(CanonType) &&
2029 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002030 }
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Douglas Gregor40808ce2009-03-09 23:48:35 +00002032 // Build the fully-sugared type for this class template
2033 // specialization, which refers back to the class template
2034 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002035 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002036}
2037
John McCallf312b1e2010-08-26 23:41:50 +00002038TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002039Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2040 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002041 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002042 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002043 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002044 if (SS.isInvalid())
2045 return true;
2046
Douglas Gregor7532dc62009-03-30 22:58:21 +00002047 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002048
Douglas Gregor40808ce2009-03-09 23:48:35 +00002049 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002050 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002051 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002052
Douglas Gregora88f09f2011-02-28 17:23:35 +00002053 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2054 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2055 DTN->getQualifier(),
2056 DTN->getIdentifier(),
2057 TemplateArgs);
2058
2059 // Build type-source information.
2060 TypeLocBuilder TLB;
2061 DependentTemplateSpecializationTypeLoc SpecTL
2062 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002063 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002064 SpecTL.setNameLoc(TemplateLoc);
2065 SpecTL.setLAngleLoc(LAngleLoc);
2066 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002067 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002068 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2069 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2070 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2071 }
2072
John McCalld5532b62009-11-23 01:53:49 +00002073 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002074 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002075
2076 if (Result.isNull())
2077 return true;
2078
Douglas Gregor059101f2011-03-02 00:47:37 +00002079 // Build type-source information.
2080 TypeLocBuilder TLB;
2081 TemplateSpecializationTypeLoc SpecTL
2082 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2083 SpecTL.setTemplateNameLoc(TemplateLoc);
2084 SpecTL.setLAngleLoc(LAngleLoc);
2085 SpecTL.setRAngleLoc(RAngleLoc);
2086 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2087 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002088
Douglas Gregor059101f2011-03-02 00:47:37 +00002089 if (SS.isNotEmpty()) {
2090 // Create an elaborated-type-specifier containing the nested-name-specifier.
2091 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2092 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2093 ElabTL.setKeywordLoc(SourceLocation());
2094 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2095 }
2096
2097 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002098}
John McCallf1bbbb42009-09-04 01:14:41 +00002099
Douglas Gregor059101f2011-03-02 00:47:37 +00002100TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002101 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002102 SourceLocation TagLoc,
2103 CXXScopeSpec &SS,
2104 TemplateTy TemplateD,
2105 SourceLocation TemplateLoc,
2106 SourceLocation LAngleLoc,
2107 ASTTemplateArgsPtr TemplateArgsIn,
2108 SourceLocation RAngleLoc) {
2109 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2110
2111 // Translate the parser's template argument list in our AST format.
2112 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2113 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2114
2115 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002116 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002117 ElaboratedTypeKeyword Keyword
2118 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Douglas Gregor059101f2011-03-02 00:47:37 +00002120 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2121 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2122 DTN->getQualifier(),
2123 DTN->getIdentifier(),
2124 TemplateArgs);
2125
2126 // Build type-source information.
2127 TypeLocBuilder TLB;
2128 DependentTemplateSpecializationTypeLoc SpecTL
2129 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2130 SpecTL.setKeywordLoc(TagLoc);
2131 SpecTL.setNameLoc(TemplateLoc);
2132 SpecTL.setLAngleLoc(LAngleLoc);
2133 SpecTL.setRAngleLoc(RAngleLoc);
2134 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2135 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2136 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2137 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2138 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002139
2140 if (TypeAliasTemplateDecl *TAT =
2141 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2142 // C++0x [dcl.type.elab]p2:
2143 // If the identifier resolves to a typedef-name or the simple-template-id
2144 // resolves to an alias template specialization, the
2145 // elaborated-type-specifier is ill-formed.
2146 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2147 Diag(TAT->getLocation(), diag::note_declared_at);
2148 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002149
2150 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2151 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002152 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002153
2154 // Check the tag kind
2155 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002156 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002157
John McCall6b2becf2009-09-08 17:47:29 +00002158 IdentifierInfo *Id = D->getIdentifier();
2159 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002160
Richard Trieubbf34c02011-06-10 03:11:26 +00002161 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2162 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002163 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002164 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002165 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002166 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002167 }
2168 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002169
2170 // Provide source-location information for the template specialization.
2171 TypeLocBuilder TLB;
2172 TemplateSpecializationTypeLoc SpecTL
2173 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2174 SpecTL.setTemplateNameLoc(TemplateLoc);
2175 SpecTL.setLAngleLoc(LAngleLoc);
2176 SpecTL.setRAngleLoc(RAngleLoc);
2177 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2178 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002179
Douglas Gregor059101f2011-03-02 00:47:37 +00002180 // Construct an elaborated type containing the nested-name-specifier (if any)
2181 // and keyword.
2182 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2183 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2184 ElabTL.setKeywordLoc(TagLoc);
2185 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2186 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002187}
2188
John McCall60d7b3a2010-08-24 06:29:42 +00002189ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002190 LookupResult &R,
2191 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002192 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002193 // FIXME: Can we do any checking at this point? I guess we could check the
2194 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002195 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002196 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002197 // foo<int> could identify a single function unambiguously
2198 // This approach does NOT work, since f<int>(1);
2199 // gets resolved prior to resorting to overload resolution
2200 // i.e., template<class T> void f(double);
2201 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002202
2203 // These should be filtered out by our callers.
2204 assert(!R.empty() && "empty lookup results when building templateid");
2205 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2206
John McCallc373d482010-01-27 01:50:18 +00002207 // We don't want lookup warnings at this point.
2208 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002209
John McCallf7a1a742009-11-24 19:00:30 +00002210 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002211 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002212 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002213 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002214 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002215 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002216
2217 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002218}
2219
John McCallf7a1a742009-11-24 19:00:30 +00002220// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002221ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002222Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002223 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002224 const TemplateArgumentListInfo &TemplateArgs) {
2225 DeclContext *DC;
2226 if (!(DC = computeDeclContext(SS, false)) ||
2227 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002228 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002229 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002231 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002232 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002233 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2234 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002235
John McCallf7a1a742009-11-24 19:00:30 +00002236 if (R.isAmbiguous())
2237 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002238
John McCallf7a1a742009-11-24 19:00:30 +00002239 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002240 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2241 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002242 return ExprError();
2243 }
2244
2245 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002246 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2247 << (NestedNameSpecifier*) SS.getScopeRep()
2248 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002249 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2250 return ExprError();
2251 }
2252
2253 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002254}
2255
Douglas Gregorc45c2322009-03-31 00:43:58 +00002256/// \brief Form a dependent template name.
2257///
2258/// This action forms a dependent template name given the template
2259/// name and its (presumably dependent) scope specifier. For
2260/// example, given "MetaFun::template apply", the scope specifier \p
2261/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2262/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002263TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002264 SourceLocation TemplateKWLoc,
2265 CXXScopeSpec &SS,
2266 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002267 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002268 bool EnteringContext,
2269 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002270 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2271 Diag(TemplateKWLoc,
2272 getLangOptions().CPlusPlus0x ?
2273 diag::warn_cxx98_compat_template_outside_of_template :
2274 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002275 << FixItHint::CreateRemoval(TemplateKWLoc);
2276
Douglas Gregor0707bc52010-01-19 16:01:07 +00002277 DeclContext *LookupCtx = 0;
2278 if (SS.isSet())
2279 LookupCtx = computeDeclContext(SS, EnteringContext);
2280 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002281 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002282 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002283 // C++0x [temp.names]p5:
2284 // If a name prefixed by the keyword template is not the name of
2285 // a template, the program is ill-formed. [Note: the keyword
2286 // template may not be applied to non-template members of class
2287 // templates. -end note ] [ Note: as is the case with the
2288 // typename prefix, the template prefix is allowed in cases
2289 // where it is not strictly necessary; i.e., when the
2290 // nested-name-specifier or the expression on the left of the ->
2291 // or . is not dependent on a template-parameter, or the use
2292 // does not appear in the scope of a template. -end note]
2293 //
2294 // Note: C++03 was more strict here, because it banned the use of
2295 // the "template" keyword prior to a template-name that was not a
2296 // dependent name. C++ DR468 relaxed this requirement (the
2297 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002298 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002299 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002300 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2301 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002302 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002303 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2304 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002305 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2306 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002307 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002308 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002309 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002310 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002311 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002312 << Name.getSourceRange()
2313 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002314 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002315 } else {
2316 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002317 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002318 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002319 }
2320
Mike Stump1eb44332009-09-09 15:08:12 +00002321 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002322 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002323
Douglas Gregor014e88d2009-11-03 23:16:33 +00002324 switch (Name.getKind()) {
2325 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002326 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002327 Name.Identifier));
2328 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002329
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002330 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002331 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002332 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002333 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002334
2335 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002336 llvm_unreachable(
2337 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002338
Douglas Gregor014e88d2009-11-03 23:16:33 +00002339 default:
2340 break;
2341 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342
2343 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002344 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002345 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002346 << Name.getSourceRange()
2347 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002348 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002349}
2350
Mike Stump1eb44332009-09-09 15:08:12 +00002351bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002352 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002353 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002354 const TemplateArgument &Arg = AL.getArgument();
2355
Anders Carlsson436b1562009-06-13 00:33:33 +00002356 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002357 switch(Arg.getKind()) {
2358 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002359 // C++ [temp.arg.type]p1:
2360 // A template-argument for a template-parameter which is a
2361 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002362 break;
2363 case TemplateArgument::Template: {
2364 // We have a template type parameter but the template argument
2365 // is a template without any arguments.
2366 SourceRange SR = AL.getSourceRange();
2367 TemplateName Name = Arg.getAsTemplate();
2368 Diag(SR.getBegin(), diag::err_template_missing_args)
2369 << Name << SR;
2370 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2371 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002372
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002373 return true;
2374 }
2375 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002376 // We have a template type parameter but the template argument
2377 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002378 SourceRange SR = AL.getSourceRange();
2379 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002380 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Anders Carlsson436b1562009-06-13 00:33:33 +00002382 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002383 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002384 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002385
John McCalla93c9342009-12-07 02:54:59 +00002386 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002387 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002388
Anders Carlsson436b1562009-06-13 00:33:33 +00002389 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002390 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2391
2392 // Objective-C ARC:
2393 // If an explicitly-specified template argument type is a lifetime type
2394 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2395 if (getLangOptions().ObjCAutoRefCount &&
2396 ArgType->isObjCLifetimeType() &&
2397 !ArgType.getObjCLifetime()) {
2398 Qualifiers Qs;
2399 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2400 ArgType = Context.getQualifiedType(ArgType, Qs);
2401 }
2402
2403 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002404 return false;
2405}
2406
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002407/// \brief Substitute template arguments into the default template argument for
2408/// the given template type parameter.
2409///
2410/// \param SemaRef the semantic analysis object for which we are performing
2411/// the substitution.
2412///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002413/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002414/// for.
2415///
2416/// \param TemplateLoc the location of the template name that started the
2417/// template-id we are checking.
2418///
2419/// \param RAngleLoc the location of the right angle bracket ('>') that
2420/// terminates the template-id.
2421///
2422/// \param Param the template template parameter whose default we are
2423/// substituting into.
2424///
2425/// \param Converted the list of template arguments provided for template
2426/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002427/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002428static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002429SubstDefaultTemplateArgument(Sema &SemaRef,
2430 TemplateDecl *Template,
2431 SourceLocation TemplateLoc,
2432 SourceLocation RAngleLoc,
2433 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002434 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002435 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002436
2437 // If the argument type is dependent, instantiate it now based
2438 // on the previously-computed template arguments.
2439 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002440 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002441 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002442
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002443 MultiLevelTemplateArgumentList AllTemplateArgs
2444 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2445
2446 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002447 Template, Converted.data(),
2448 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002449 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002450
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002451 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2452 Param->getDefaultArgumentLoc(),
2453 Param->getDeclName());
2454 }
2455
2456 return ArgType;
2457}
2458
2459/// \brief Substitute template arguments into the default template argument for
2460/// the given non-type template parameter.
2461///
2462/// \param SemaRef the semantic analysis object for which we are performing
2463/// the substitution.
2464///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002465/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002466/// for.
2467///
2468/// \param TemplateLoc the location of the template name that started the
2469/// template-id we are checking.
2470///
2471/// \param RAngleLoc the location of the right angle bracket ('>') that
2472/// terminates the template-id.
2473///
Douglas Gregor788cd062009-11-11 01:00:40 +00002474/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002475/// substituting into.
2476///
2477/// \param Converted the list of template arguments provided for template
2478/// parameters that precede \p Param in the template parameter list.
2479///
2480/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002481static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002482SubstDefaultTemplateArgument(Sema &SemaRef,
2483 TemplateDecl *Template,
2484 SourceLocation TemplateLoc,
2485 SourceLocation RAngleLoc,
2486 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002487 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002488 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002489 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002490
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002491 MultiLevelTemplateArgumentList AllTemplateArgs
2492 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002493
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002494 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002495 Template, Converted.data(),
2496 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002497 SourceRange(TemplateLoc, RAngleLoc));
2498
2499 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2500}
2501
Douglas Gregor788cd062009-11-11 01:00:40 +00002502/// \brief Substitute template arguments into the default template argument for
2503/// the given template template parameter.
2504///
2505/// \param SemaRef the semantic analysis object for which we are performing
2506/// the substitution.
2507///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002508/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002509/// for.
2510///
2511/// \param TemplateLoc the location of the template name that started the
2512/// template-id we are checking.
2513///
2514/// \param RAngleLoc the location of the right angle bracket ('>') that
2515/// terminates the template-id.
2516///
2517/// \param Param the template template parameter whose default we are
2518/// substituting into.
2519///
2520/// \param Converted the list of template arguments provided for template
2521/// parameters that precede \p Param in the template parameter list.
2522///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002523/// \param QualifierLoc Will be set to the nested-name-specifier (with
2524/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002525///
Douglas Gregor788cd062009-11-11 01:00:40 +00002526/// \returns the substituted template argument, or NULL if an error occurred.
2527static TemplateName
2528SubstDefaultTemplateArgument(Sema &SemaRef,
2529 TemplateDecl *Template,
2530 SourceLocation TemplateLoc,
2531 SourceLocation RAngleLoc,
2532 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002533 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002534 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002536 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002537
Douglas Gregor788cd062009-11-11 01:00:40 +00002538 MultiLevelTemplateArgumentList AllTemplateArgs
2539 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002540
Douglas Gregor788cd062009-11-11 01:00:40 +00002541 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002542 Template, Converted.data(),
2543 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002544 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002545
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002546 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002547 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002548 if (QualifierLoc) {
2549 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2550 AllTemplateArgs);
2551 if (!QualifierLoc)
2552 return TemplateName();
2553 }
2554
Douglas Gregor1d752d72011-03-02 18:46:51 +00002555 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002556 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002557 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002558 AllTemplateArgs);
2559}
2560
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002561/// \brief If the given template parameter has a default template
2562/// argument, substitute into that default template argument and
2563/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002564TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002565Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2566 SourceLocation TemplateLoc,
2567 SourceLocation RAngleLoc,
2568 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002569 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002570 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002571 if (!TypeParm->hasDefaultArgument())
2572 return TemplateArgumentLoc();
2573
John McCalla93c9342009-12-07 02:54:59 +00002574 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002575 TemplateLoc,
2576 RAngleLoc,
2577 TypeParm,
2578 Converted);
2579 if (DI)
2580 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2581
2582 return TemplateArgumentLoc();
2583 }
2584
2585 if (NonTypeTemplateParmDecl *NonTypeParm
2586 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2587 if (!NonTypeParm->hasDefaultArgument())
2588 return TemplateArgumentLoc();
2589
John McCall60d7b3a2010-08-24 06:29:42 +00002590 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002591 TemplateLoc,
2592 RAngleLoc,
2593 NonTypeParm,
2594 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002595 if (Arg.isInvalid())
2596 return TemplateArgumentLoc();
2597
2598 Expr *ArgE = Arg.takeAs<Expr>();
2599 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2600 }
2601
2602 TemplateTemplateParmDecl *TempTempParm
2603 = cast<TemplateTemplateParmDecl>(Param);
2604 if (!TempTempParm->hasDefaultArgument())
2605 return TemplateArgumentLoc();
2606
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002607
Douglas Gregor1d752d72011-03-02 18:46:51 +00002608 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002609 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002610 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002611 RAngleLoc,
2612 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002613 Converted,
2614 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002615 if (TName.isNull())
2616 return TemplateArgumentLoc();
2617
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002618 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002619 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002620 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2621}
2622
Douglas Gregore7526412009-11-11 19:31:23 +00002623/// \brief Check that the given template argument corresponds to the given
2624/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002625///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002626/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002627/// checked.
2628///
2629/// \param Arg The template argument.
2630///
2631/// \param Template The template in which the template argument resides.
2632///
2633/// \param TemplateLoc The location of the template name for the template
2634/// whose argument list we're matching.
2635///
2636/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2637/// the template argument list.
2638///
2639/// \param ArgumentPackIndex The index into the argument pack where this
2640/// argument will be placed. Only valid if the parameter is a parameter pack.
2641///
2642/// \param Converted The checked, converted argument will be added to the
2643/// end of this small vector.
2644///
2645/// \param CTAK Describes how we arrived at this particular template argument:
2646/// explicitly written, deduced, etc.
2647///
2648/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002649bool Sema::CheckTemplateArgument(NamedDecl *Param,
2650 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002651 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002652 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002653 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002654 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002655 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002656 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002657 // Check template type parameters.
2658 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002659 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002660
Douglas Gregord9e15302009-11-11 19:41:09 +00002661 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002662 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002663 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002664 // with the template arguments we've seen thus far. But if the
2665 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002666 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002667 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2668 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002669
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002670 if (NTTPType->isDependentType() &&
2671 !isa<TemplateTemplateParmDecl>(Template) &&
2672 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002673 // Do substitution on the type of the non-type template parameter.
2674 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002675 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002676 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002677
2678 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002679 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002680 NTTPType = SubstType(NTTPType,
2681 MultiLevelTemplateArgumentList(TemplateArgs),
2682 NTTP->getLocation(),
2683 NTTP->getDeclName());
2684 // If that worked, check the non-type template parameter type
2685 // for validity.
2686 if (!NTTPType.isNull())
2687 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2688 NTTP->getLocation());
2689 if (NTTPType.isNull())
2690 return true;
2691 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002692
Douglas Gregore7526412009-11-11 19:31:23 +00002693 switch (Arg.getArgument().getKind()) {
2694 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002695 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696
Douglas Gregore7526412009-11-11 19:31:23 +00002697 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002698 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002699 ExprResult Res =
2700 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2701 Result, CTAK);
2702 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002703 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002704
Douglas Gregor910f8002010-11-07 23:05:16 +00002705 Converted.push_back(Result);
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 case TemplateArgument::Declaration:
2710 case TemplateArgument::Integral:
2711 // We've already checked this template argument, so just copy
2712 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002713 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002714 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002715
Douglas Gregore7526412009-11-11 19:31:23 +00002716 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002717 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002718 // We were given a template template argument. It may not be ill-formed;
2719 // see below.
2720 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002721 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2722 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002723 // We have a template argument such as \c T::template X, which we
2724 // parsed as a template template argument. However, since we now
2725 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002726 // template name into an expression.
2727
2728 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2729 Arg.getTemplateNameLoc());
2730
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002731 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002732 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002733 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002734 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002735 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002736
Douglas Gregora7fc9012011-01-05 18:58:31 +00002737 // If we parsed the template argument as a pack expansion, create a
2738 // pack expansion expression.
2739 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002740 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2741 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002742 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002743 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002744
Douglas Gregore7526412009-11-11 19:31:23 +00002745 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002746 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2747 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002748 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002749
Douglas Gregor910f8002010-11-07 23:05:16 +00002750 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002751 break;
2752 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002753
Douglas Gregore7526412009-11-11 19:31:23 +00002754 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002755 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002756 // therefore cannot be a non-type template argument.
2757 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2758 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002759
Douglas Gregore7526412009-11-11 19:31:23 +00002760 Diag(Param->getLocation(), diag::note_template_param_here);
2761 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002762
Douglas Gregore7526412009-11-11 19:31:23 +00002763 case TemplateArgument::Type: {
2764 // We have a non-type template parameter but the template
2765 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002766
Douglas Gregore7526412009-11-11 19:31:23 +00002767 // C++ [temp.arg]p2:
2768 // In a template-argument, an ambiguity between a type-id and
2769 // an expression is resolved to a type-id, regardless of the
2770 // form of the corresponding template-parameter.
2771 //
2772 // We warn specifically about this case, since it can be rather
2773 // confusing for users.
2774 QualType T = Arg.getArgument().getAsType();
2775 SourceRange SR = Arg.getSourceRange();
2776 if (T->isFunctionType())
2777 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2778 else
2779 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2780 Diag(Param->getLocation(), diag::note_template_param_here);
2781 return true;
2782 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002783
Douglas Gregore7526412009-11-11 19:31:23 +00002784 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002785 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002786 break;
2787 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002788
Douglas Gregore7526412009-11-11 19:31:23 +00002789 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002790 }
2791
2792
Douglas Gregore7526412009-11-11 19:31:23 +00002793 // Check template template parameters.
2794 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002795
Douglas Gregore7526412009-11-11 19:31:23 +00002796 // Substitute into the template parameter list of the template
2797 // template parameter, since previously-supplied template arguments
2798 // may appear within the template template parameter.
2799 {
2800 // Set up a template instantiation context.
2801 LocalInstantiationScope Scope(*this);
2802 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002803 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002804 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002805
2806 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002807 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002808 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002809 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002810 MultiLevelTemplateArgumentList(TemplateArgs)));
2811 if (!TempParm)
2812 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002813 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002814
Douglas Gregore7526412009-11-11 19:31:23 +00002815 switch (Arg.getArgument().getKind()) {
2816 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002817 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002818
Douglas Gregore7526412009-11-11 19:31:23 +00002819 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002820 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002821 if (CheckTemplateArgument(TempParm, Arg))
2822 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002823
Douglas Gregor910f8002010-11-07 23:05:16 +00002824 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002825 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002826
Douglas Gregore7526412009-11-11 19:31:23 +00002827 case TemplateArgument::Expression:
2828 case TemplateArgument::Type:
2829 // We have a template template parameter but the template
2830 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002831 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2832 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002833 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002834
Douglas Gregore7526412009-11-11 19:31:23 +00002835 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002836 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002837 "Declaration argument with template template parameter");
2838 break;
2839 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002840 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002841 "Integral argument with template template parameter");
2842 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002843
Douglas Gregore7526412009-11-11 19:31:23 +00002844 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002845 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002846 break;
2847 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002848
Douglas Gregore7526412009-11-11 19:31:23 +00002849 return false;
2850}
2851
Douglas Gregorc15cb382009-02-09 23:23:08 +00002852/// \brief Check that the given template argument list is well-formed
2853/// for specializing the given template.
2854bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2855 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002856 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002857 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002858 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002859 TemplateParameterList *Params = Template->getTemplateParameters();
2860 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002861 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002862 bool Invalid = false;
2863
John McCalld5532b62009-11-23 01:53:49 +00002864 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2865
Mike Stump1eb44332009-09-09 15:08:12 +00002866 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002867 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002869 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002870 (NumArgs < Params->getMinRequiredArguments() &&
2871 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002872 // FIXME: point at either the first arg beyond what we can handle,
2873 // or the '>', depending on whether we have too many or too few
2874 // arguments.
2875 SourceRange Range;
2876 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002877 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002878 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2879 << (NumArgs > NumParams)
2880 << (isa<ClassTemplateDecl>(Template)? 0 :
2881 isa<FunctionTemplateDecl>(Template)? 1 :
2882 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2883 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002884 Diag(Template->getLocation(), diag::note_template_decl_here)
2885 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002886 Invalid = true;
2887 }
Mike Stump1eb44332009-09-09 15:08:12 +00002888
2889 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002890 // [...] The type and form of each template-argument specified in
2891 // a template-id shall match the type and form specified for the
2892 // corresponding parameter declared by the template in its
2893 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002894 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002895 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002896 TemplateParameterList::iterator Param = Params->begin(),
2897 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002898 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002899 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002900 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002901 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002902 // If we have an expanded parameter pack, make sure we don't have too
2903 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002904 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002905 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002906 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002907 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2908 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2909 << true
2910 << (isa<ClassTemplateDecl>(Template)? 0 :
2911 isa<FunctionTemplateDecl>(Template)? 1 :
2912 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2913 << Template;
2914 Diag(Template->getLocation(), diag::note_template_decl_here)
2915 << Params->getSourceRange();
2916 return true;
2917 }
2918 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002919
Douglas Gregorf35f8282009-11-11 21:54:23 +00002920 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002921 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2922 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002923 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002924 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002925
Douglas Gregor14be16b2010-12-20 16:57:52 +00002926 if ((*Param)->isTemplateParameterPack()) {
2927 // The template parameter was a template parameter pack, so take the
2928 // deduced argument and place it on the argument pack. Note that we
2929 // stay on the same template parameter so that we can deduce more
2930 // arguments.
2931 ArgumentPack.push_back(Converted.back());
2932 Converted.pop_back();
2933 } else {
2934 // Move to the next template parameter.
2935 ++Param;
2936 }
2937 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002938 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002939 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002940
Douglas Gregor8735b292011-06-03 02:59:40 +00002941 // If we're checking a partial template argument list, we're done.
2942 if (PartialTemplateArgs) {
2943 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2944 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2945 ArgumentPack.data(),
2946 ArgumentPack.size()));
2947
2948 return Invalid;
2949 }
2950
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002951 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002952 // arguments, just break out now and we'll fill in the argument pack below.
2953 if ((*Param)->isTemplateParameterPack())
2954 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002955
Douglas Gregorf35f8282009-11-11 21:54:23 +00002956 // We have a default template argument that we will use.
2957 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002958
Douglas Gregorf35f8282009-11-11 21:54:23 +00002959 // Retrieve the default template argument from the template
2960 // parameter. For each kind of template parameter, we substitute the
2961 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002962 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002963 // the default argument.
2964 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2965 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002966 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002967 break;
2968 }
2969
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002970 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002971 Template,
2972 TemplateLoc,
2973 RAngleLoc,
2974 TTP,
2975 Converted);
2976 if (!ArgType)
2977 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002978
Douglas Gregorf35f8282009-11-11 21:54:23 +00002979 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2980 ArgType);
2981 } else if (NonTypeTemplateParmDecl *NTTP
2982 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2983 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002984 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002985 break;
2986 }
2987
John McCall60d7b3a2010-08-24 06:29:42 +00002988 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002989 TemplateLoc,
2990 RAngleLoc,
2991 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002992 Converted);
2993 if (E.isInvalid())
2994 return true;
2995
2996 Expr *Ex = E.takeAs<Expr>();
2997 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2998 } else {
2999 TemplateTemplateParmDecl *TempParm
3000 = cast<TemplateTemplateParmDecl>(*Param);
3001
3002 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003003 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00003004 break;
3005 }
3006
Douglas Gregor1d752d72011-03-02 18:46:51 +00003007 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003008 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003009 TemplateLoc,
3010 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003011 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003012 Converted,
3013 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003014 if (Name.isNull())
3015 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003016
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003017 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3018 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003019 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003020
Douglas Gregorf35f8282009-11-11 21:54:23 +00003021 // Introduce an instantiation record that describes where we are using
3022 // the default template argument.
3023 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003024 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003025 SourceRange(TemplateLoc, RAngleLoc));
3026
Douglas Gregorf35f8282009-11-11 21:54:23 +00003027 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003028 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003029 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003030 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031
Douglas Gregor67714232011-03-03 02:41:12 +00003032 // Core issue 150 (assumed resolution): if this is a template template
3033 // parameter, keep track of the default template arguments from the
3034 // template definition.
3035 if (isTemplateTemplateParameter)
3036 TemplateArgs.addArgument(Arg);
3037
Douglas Gregor14be16b2010-12-20 16:57:52 +00003038 // Move to the next template parameter and argument.
3039 ++Param;
3040 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003041 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003042
Douglas Gregor14be16b2010-12-20 16:57:52 +00003043 // Form argument packs for each of the parameter packs remaining.
3044 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003045 // If we're checking a partial list of template arguments, don't fill
3046 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003047
3048 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003049 if (!HasParameterPack)
3050 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003051 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003052 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003053 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003054 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3055 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003056 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003057 ArgumentPack.clear();
3058 }
3059 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003060
Douglas Gregor14be16b2010-12-20 16:57:52 +00003061 ++Param;
3062 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003063
Douglas Gregorc15cb382009-02-09 23:23:08 +00003064 return Invalid;
3065}
3066
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003067namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003068 class UnnamedLocalNoLinkageFinder
3069 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003070 {
3071 Sema &S;
3072 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003073
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003074 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003075
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003076 public:
3077 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3078
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003079 bool Visit(QualType T) {
3080 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003081 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003082
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003083#define TYPE(Class, Parent) \
3084 bool Visit##Class##Type(const Class##Type *);
3085#define ABSTRACT_TYPE(Class, Parent) \
3086 bool Visit##Class##Type(const Class##Type *) { return false; }
3087#define NON_CANONICAL_TYPE(Class, Parent) \
3088 bool Visit##Class##Type(const Class##Type *) { return false; }
3089#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003090
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003091 bool VisitTagDecl(const TagDecl *Tag);
3092 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3093 };
3094}
3095
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003096bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003097 return false;
3098}
3099
3100bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3101 return Visit(T->getElementType());
3102}
3103
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003104bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003105 return Visit(T->getPointeeType());
3106}
3107
3108bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003109 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003110 return Visit(T->getPointeeType());
3111}
3112
3113bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003114 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003115 return Visit(T->getPointeeType());
3116}
3117
3118bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003119 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003120 return Visit(T->getPointeeType());
3121}
3122
3123bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003124 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003125 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3126}
3127
3128bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003129 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003130 return Visit(T->getElementType());
3131}
3132
3133bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003134 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003135 return Visit(T->getElementType());
3136}
3137
3138bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003139 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003140 return Visit(T->getElementType());
3141}
3142
3143bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003144 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003145 return Visit(T->getElementType());
3146}
3147
3148bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003149 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003150 return Visit(T->getElementType());
3151}
3152
3153bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3154 return Visit(T->getElementType());
3155}
3156
3157bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3158 return Visit(T->getElementType());
3159}
3160
3161bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3162 const FunctionProtoType* T) {
3163 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003164 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003165 A != AEnd; ++A) {
3166 if (Visit(*A))
3167 return true;
3168 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003169
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003170 return Visit(T->getResultType());
3171}
3172
3173bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3174 const FunctionNoProtoType* T) {
3175 return Visit(T->getResultType());
3176}
3177
3178bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3179 const UnresolvedUsingType*) {
3180 return false;
3181}
3182
3183bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3184 return false;
3185}
3186
3187bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3188 return Visit(T->getUnderlyingType());
3189}
3190
3191bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3192 return false;
3193}
3194
Sean Huntca63c202011-05-24 22:41:36 +00003195bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3196 const UnaryTransformType*) {
3197 return false;
3198}
3199
Richard Smith34b41d92011-02-20 03:19:35 +00003200bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3201 return Visit(T->getDeducedType());
3202}
3203
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003204bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3205 return VisitTagDecl(T->getDecl());
3206}
3207
3208bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3209 return VisitTagDecl(T->getDecl());
3210}
3211
3212bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3213 const TemplateTypeParmType*) {
3214 return false;
3215}
3216
Douglas Gregorc3069d62011-01-14 02:55:32 +00003217bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3218 const SubstTemplateTypeParmPackType *) {
3219 return false;
3220}
3221
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003222bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3223 const TemplateSpecializationType*) {
3224 return false;
3225}
3226
3227bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3228 const InjectedClassNameType* T) {
3229 return VisitTagDecl(T->getDecl());
3230}
3231
3232bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3233 const DependentNameType* T) {
3234 return VisitNestedNameSpecifier(T->getQualifier());
3235}
3236
3237bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3238 const DependentTemplateSpecializationType* T) {
3239 return VisitNestedNameSpecifier(T->getQualifier());
3240}
3241
Douglas Gregor7536dd52010-12-20 02:24:11 +00003242bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3243 const PackExpansionType* T) {
3244 return Visit(T->getPattern());
3245}
3246
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003247bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3248 return false;
3249}
3250
3251bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3252 const ObjCInterfaceType *) {
3253 return false;
3254}
3255
3256bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3257 const ObjCObjectPointerType *) {
3258 return false;
3259}
3260
Eli Friedmanb001de72011-10-06 23:00:33 +00003261bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3262 return Visit(T->getValueType());
3263}
3264
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003265bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3266 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003267 S.Diag(SR.getBegin(),
3268 S.getLangOptions().CPlusPlus0x ?
3269 diag::warn_cxx98_compat_template_arg_local_type :
3270 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003271 << S.Context.getTypeDeclType(Tag) << SR;
3272 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003273 }
3274
Richard Smith162e1c12011-04-15 14:24:37 +00003275 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003276 S.Diag(SR.getBegin(),
3277 S.getLangOptions().CPlusPlus0x ?
3278 diag::warn_cxx98_compat_template_arg_unnamed_type :
3279 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003280 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3281 return true;
3282 }
3283
3284 return false;
3285}
3286
3287bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3288 NestedNameSpecifier *NNS) {
3289 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3290 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003291
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003292 switch (NNS->getKind()) {
3293 case NestedNameSpecifier::Identifier:
3294 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003295 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003296 case NestedNameSpecifier::Global:
3297 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003298
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003299 case NestedNameSpecifier::TypeSpec:
3300 case NestedNameSpecifier::TypeSpecWithTemplate:
3301 return Visit(QualType(NNS->getAsType(), 0));
3302 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003303 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003304}
3305
3306
Douglas Gregorc15cb382009-02-09 23:23:08 +00003307/// \brief Check a template argument against its corresponding
3308/// template type parameter.
3309///
3310/// This routine implements the semantics of C++ [temp.arg.type]. It
3311/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003312bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003313 TypeSourceInfo *ArgInfo) {
3314 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003315 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003316 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003317
3318 if (Arg->isVariablyModifiedType()) {
3319 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003320 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003321 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003322 }
3323
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003324 // C++03 [temp.arg.type]p2:
3325 // A local type, a type with no linkage, an unnamed type or a type
3326 // compounded from any of these types shall not be used as a
3327 // template-argument for a template type-parameter.
3328 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003329 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003330 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003331 if (LangOpts.CPlusPlus0x ?
3332 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3333 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3334 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3335 SR.getBegin()) != DiagnosticsEngine::Ignored :
3336 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003337 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3338 (void)Finder.Visit(Context.getCanonicalType(Arg));
3339 }
3340
Douglas Gregorc15cb382009-02-09 23:23:08 +00003341 return false;
3342}
3343
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003344/// \brief Checks whether the given template argument is the address
3345/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003346static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003347CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3348 NonTypeTemplateParmDecl *Param,
3349 QualType ParamType,
3350 Expr *ArgIn,
3351 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003352 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003353 Expr *Arg = ArgIn;
3354 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003355
3356 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003357 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003358
3359 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003360 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003361 // A template-argument for a non-type, non-template
3362 // template-parameter shall be one of: [...]
3363 //
3364 // -- the address of an object or function with external
3365 // linkage, including function templates and function
3366 // template-ids but excluding non-static class members,
3367 // expressed as & id-expression where the & is optional if
3368 // the name refers to a function or array, or if the
3369 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003370
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003371 // In C++98/03 mode, give an extension warning on any extra parentheses.
3372 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3373 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003374 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003375 if (!Invalid && !ExtraParens) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003376 S.Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003377 S.getLangOptions().CPlusPlus0x ?
3378 diag::warn_cxx98_compat_template_arg_extra_parens :
3379 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003380 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003381 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003382 }
3383
3384 Arg = Parens->getSubExpr();
3385 }
3386
John McCall91a57552011-07-15 05:09:51 +00003387 while (SubstNonTypeTemplateParmExpr *subst =
3388 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3389 Arg = subst->getReplacement()->IgnoreImpCasts();
3390
Douglas Gregorb7a09262010-04-01 18:32:35 +00003391 bool AddressTaken = false;
3392 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003393 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003394 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003395 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003396 AddressTaken = true;
3397 AddrOpLoc = UnOp->getOperatorLoc();
3398 }
Francois Picheta343a412011-04-29 09:08:14 +00003399 }
John McCall91a57552011-07-15 05:09:51 +00003400
Francois Pichet62ec1f22011-09-17 17:15:52 +00003401 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003402 Converted = TemplateArgument(ArgIn);
3403 return false;
3404 }
3405
3406 while (SubstNonTypeTemplateParmExpr *subst =
3407 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3408 Arg = subst->getReplacement()->IgnoreImpCasts();
3409
3410 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003411 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003412 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3413 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003414 S.Diag(Param->getLocation(), diag::note_template_param_here);
3415 return true;
3416 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003417
3418 // Stop checking the precise nature of the argument if it is value dependent,
3419 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003420 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003421 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003422 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003423 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003424
Douglas Gregorb7a09262010-04-01 18:32:35 +00003425 if (!isa<ValueDecl>(DRE->getDecl())) {
3426 S.Diag(Arg->getSourceRange().getBegin(),
3427 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003428 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003429 S.Diag(Param->getLocation(), diag::note_template_param_here);
3430 return true;
3431 }
3432
3433 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003434
3435 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003436 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3437 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003438 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003439 S.Diag(Param->getLocation(), diag::note_template_param_here);
3440 return true;
3441 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003442
3443 // Cannot refer to non-static member functions
3444 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003445 if (!Method->isStatic()) {
3446 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003447 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003448 S.Diag(Param->getLocation(), diag::note_template_param_here);
3449 return true;
3450 }
Mike Stump1eb44332009-09-09 15:08:12 +00003451
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003452 // Functions must have external linkage.
3453 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003454 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003455 S.Diag(Arg->getSourceRange().getBegin(),
3456 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003457 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003458 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003459 << true;
3460 return true;
3461 }
3462
3463 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003464 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003465
Douglas Gregorb7a09262010-04-01 18:32:35 +00003466 // If the template parameter has pointer type, the function decays.
3467 if (ParamType->isPointerType() && !AddressTaken)
3468 ArgType = S.Context.getPointerType(Func->getType());
3469 else if (AddressTaken && ParamType->isReferenceType()) {
3470 // If we originally had an address-of operator, but the
3471 // parameter has reference type, complain and (if things look
3472 // like they will work) drop the address-of operator.
3473 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3474 ParamType.getNonReferenceType())) {
3475 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3476 << ParamType;
3477 S.Diag(Param->getLocation(), diag::note_template_param_here);
3478 return true;
3479 }
3480
3481 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3482 << ParamType
3483 << FixItHint::CreateRemoval(AddrOpLoc);
3484 S.Diag(Param->getLocation(), diag::note_template_param_here);
3485
3486 ArgType = Func->getType();
3487 }
3488 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003489 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003490 S.Diag(Arg->getSourceRange().getBegin(),
3491 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003492 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003493 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003494 << true;
3495 return true;
3496 }
3497
Douglas Gregorb7a09262010-04-01 18:32:35 +00003498 // A value of reference type is not an object.
3499 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003500 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003501 diag::err_template_arg_reference_var)
3502 << Var->getType() << Arg->getSourceRange();
3503 S.Diag(Param->getLocation(), diag::note_template_param_here);
3504 return true;
3505 }
3506
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003507 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003508 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003509
3510 // If the template parameter has pointer type, we must have taken
3511 // the address of this object.
3512 if (ParamType->isReferenceType()) {
3513 if (AddressTaken) {
3514 // If we originally had an address-of operator, but the
3515 // parameter has reference type, complain and (if things look
3516 // like they will work) drop the address-of operator.
3517 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3518 ParamType.getNonReferenceType())) {
3519 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3520 << ParamType;
3521 S.Diag(Param->getLocation(), diag::note_template_param_here);
3522 return true;
3523 }
3524
3525 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3526 << ParamType
3527 << FixItHint::CreateRemoval(AddrOpLoc);
3528 S.Diag(Param->getLocation(), diag::note_template_param_here);
3529
3530 ArgType = Var->getType();
3531 }
3532 } else if (!AddressTaken && ParamType->isPointerType()) {
3533 if (Var->getType()->isArrayType()) {
3534 // Array-to-pointer decay.
3535 ArgType = S.Context.getArrayDecayedType(Var->getType());
3536 } else {
3537 // If the template parameter has pointer type but the address of
3538 // this object was not taken, complain and (possibly) recover by
3539 // taking the address of the entity.
3540 ArgType = S.Context.getPointerType(Var->getType());
3541 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3542 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3543 << ParamType;
3544 S.Diag(Param->getLocation(), diag::note_template_param_here);
3545 return true;
3546 }
3547
3548 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3549 << ParamType
3550 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3551
3552 S.Diag(Param->getLocation(), diag::note_template_param_here);
3553 }
3554 }
3555 } else {
3556 // We found something else, but we don't know specifically what it is.
3557 S.Diag(Arg->getSourceRange().getBegin(),
3558 diag::err_template_arg_not_object_or_func)
3559 << Arg->getSourceRange();
3560 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3561 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003562 }
Mike Stump1eb44332009-09-09 15:08:12 +00003563
John McCallf85e1932011-06-15 23:02:42 +00003564 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003565 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003566 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003567 S.IsQualificationConversion(ArgType, ParamType, false,
3568 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003569 // For pointer-to-object types, qualification conversions are
3570 // permitted.
3571 } else {
3572 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3573 if (!ParamRef->getPointeeType()->isFunctionType()) {
3574 // C++ [temp.arg.nontype]p5b3:
3575 // For a non-type template-parameter of type reference to
3576 // object, no conversions apply. The type referred to by the
3577 // reference may be more cv-qualified than the (otherwise
3578 // identical) type of the template- argument. The
3579 // template-parameter is bound directly to the
3580 // template-argument, which shall be an lvalue.
3581
3582 // FIXME: Other qualifiers?
3583 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3584 unsigned ArgQuals = ArgType.getCVRQualifiers();
3585
3586 if ((ParamQuals | ArgQuals) != ParamQuals) {
3587 S.Diag(Arg->getSourceRange().getBegin(),
3588 diag::err_template_arg_ref_bind_ignores_quals)
3589 << ParamType << Arg->getType()
3590 << Arg->getSourceRange();
3591 S.Diag(Param->getLocation(), diag::note_template_param_here);
3592 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003593 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003594 }
3595 }
3596
3597 // At this point, the template argument refers to an object or
3598 // function with external linkage. We now need to check whether the
3599 // argument and parameter types are compatible.
3600 if (!S.Context.hasSameUnqualifiedType(ArgType,
3601 ParamType.getNonReferenceType())) {
3602 // We can't perform this conversion or binding.
3603 if (ParamType->isReferenceType())
3604 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003605 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003606 else
3607 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003608 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003609 S.Diag(Param->getLocation(), diag::note_template_param_here);
3610 return true;
3611 }
3612 }
3613
3614 // Create the template argument.
3615 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003616 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003617 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003618}
3619
3620/// \brief Checks whether the given template argument is a pointer to
3621/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003622bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003623 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003624 bool Invalid = false;
3625
3626 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003627 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003628 Arg = Cast->getSubExpr();
3629
3630 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003631 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003632 // A template-argument for a non-type, non-template
3633 // template-parameter shall be one of: [...]
3634 //
3635 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003636 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003637
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003638 // In C++98/03 mode, give an extension warning on any extra parentheses.
3639 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3640 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003641 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003642 if (!Invalid && !ExtraParens) {
Mike Stump1eb44332009-09-09 15:08:12 +00003643 Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003644 getLangOptions().CPlusPlus0x ?
3645 diag::warn_cxx98_compat_template_arg_extra_parens :
3646 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003647 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003648 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003649 }
3650
3651 Arg = Parens->getSubExpr();
3652 }
3653
John McCall91a57552011-07-15 05:09:51 +00003654 while (SubstNonTypeTemplateParmExpr *subst =
3655 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3656 Arg = subst->getReplacement()->IgnoreImpCasts();
3657
Douglas Gregorcaddba02009-11-12 18:38:13 +00003658 // A pointer-to-member constant written &Class::member.
3659 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003660 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003661 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3662 if (DRE && !DRE->getQualifier())
3663 DRE = 0;
3664 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003665 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003666 // A constant of pointer-to-member type.
3667 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3668 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3669 if (VD->getType()->isMemberPointerType()) {
3670 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003671 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003672 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3673 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003674 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003675 else
3676 Converted = TemplateArgument(VD->getCanonicalDecl());
3677 return Invalid;
3678 }
3679 }
3680 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003681
Douglas Gregorcaddba02009-11-12 18:38:13 +00003682 DRE = 0;
3683 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003684
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003685 if (!DRE)
3686 return Diag(Arg->getSourceRange().getBegin(),
3687 diag::err_template_arg_not_pointer_to_member_form)
3688 << Arg->getSourceRange();
3689
3690 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3691 assert((isa<FieldDecl>(DRE->getDecl()) ||
3692 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3693 "Only non-static member pointers can make it here");
3694
3695 // Okay: this is the address of a non-static member, and therefore
3696 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003697 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003698 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003699 else
3700 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003701 return Invalid;
3702 }
3703
3704 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003705 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003706 diag::err_template_arg_not_pointer_to_member_form)
3707 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003708 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003709 diag::note_template_arg_refers_here);
3710 return true;
3711}
3712
Douglas Gregorc15cb382009-02-09 23:23:08 +00003713/// \brief Check a template argument against its corresponding
3714/// non-type template parameter.
3715///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003716/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003717/// If an error occurred, it returns ExprError(); otherwise, it
3718/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003719/// InstantiatedParamType is the type of the non-type template
3720/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003721ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3722 QualType InstantiatedParamType, Expr *Arg,
3723 TemplateArgument &Converted,
3724 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003725 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3726
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003727 // If either the parameter has a dependent type or the argument is
3728 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003729 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3730 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003731 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003732 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003733 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003734
3735 // C++ [temp.arg.nontype]p5:
3736 // The following conversions are performed on each expression used
3737 // as a non-type template-argument. If a non-type
3738 // template-argument cannot be converted to the type of the
3739 // corresponding template-parameter then the program is
3740 // ill-formed.
3741 //
3742 // -- for a non-type template-parameter of integral or
3743 // enumeration type, integral promotions (4.5) and integral
3744 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003745 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003746 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003747 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003748 // C++ [temp.arg.nontype]p1:
3749 // A template-argument for a non-type, non-template
3750 // template-parameter shall be one of:
3751 //
3752 // -- an integral constant-expression of integral or enumeration
3753 // type; or
3754 // -- the name of a non-type template-parameter; or
3755 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003756 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003757 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003758 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003759 diag::err_template_arg_not_integral_or_enumeral)
3760 << ArgType << Arg->getSourceRange();
3761 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003762 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003763 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003764 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003765 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3766 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003767 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003768 }
3769
Douglas Gregor02024a92010-03-28 02:42:43 +00003770 // From here on out, all we care about are the unqualified forms
3771 // of the parameter and argument types.
3772 ParamType = ParamType.getUnqualifiedType();
3773 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003774
3775 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003776 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003777 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003778 } else if (CTAK == CTAK_Deduced) {
3779 // C++ [temp.deduct.type]p17:
3780 // If, in the declaration of a function template with a non-type
3781 // template-parameter, the non-type template- parameter is used
3782 // in an expression in the function parameter-list and, if the
3783 // corresponding template-argument is deduced, the
3784 // template-argument type shall match the type of the
3785 // template-parameter exactly, except that a template-argument
3786 // deduced from an array bound may be of any integral type.
3787 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3788 << ArgType << ParamType;
3789 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003790 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003791 } else if (ParamType->isBooleanType()) {
3792 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003793 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003794 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3795 !ParamType->isEnumeralType()) {
3796 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003797 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003798 } else {
3799 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003800 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003801 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003802 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003803 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003804 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003805 }
3806
Douglas Gregorc7469372011-05-04 21:55:00 +00003807 // Add the value of this argument to the list of converted
3808 // arguments. We use the bitwidth and signedness of the template
3809 // parameter.
3810 if (Arg->isValueDependent()) {
3811 // The argument is value-dependent. Create a new
3812 // TemplateArgument with the converted expression.
3813 Converted = TemplateArgument(Arg);
3814 return Owned(Arg);
3815 }
3816
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003817 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003818 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003819 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003820
Douglas Gregorc7469372011-05-04 21:55:00 +00003821 if (ParamType->isBooleanType()) {
3822 // Value must be zero or one.
3823 Value = Value != 0;
3824 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3825 if (Value.getBitWidth() != AllowedBits)
3826 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003827 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003828 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003829 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003830
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003831 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003832 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003833 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003834 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003835 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003836 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003837
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003838 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003839 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003840 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003841 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3842 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3843 << Arg->getSourceRange();
3844 Diag(Param->getLocation(), diag::note_template_param_here);
3845 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003846
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003847 // Complain if we overflowed the template parameter's type.
3848 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003849 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003850 RequiredBits = OldValue.getActiveBits();
3851 else if (OldValue.isUnsigned())
3852 RequiredBits = OldValue.getActiveBits() + 1;
3853 else
3854 RequiredBits = OldValue.getMinSignedBits();
3855 if (RequiredBits > AllowedBits) {
3856 Diag(Arg->getSourceRange().getBegin(),
3857 diag::warn_template_arg_too_large)
3858 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3859 << Arg->getSourceRange();
3860 Diag(Param->getLocation(), diag::note_template_param_here);
3861 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003862 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003863
John McCall833ca992009-10-29 08:12:44 +00003864 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003865 ParamType->isEnumeralType()
3866 ? Context.getCanonicalType(ParamType)
3867 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003868 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003869 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003870
John McCall6bb80172010-03-30 21:47:33 +00003871 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3872
Douglas Gregorb7a09262010-04-01 18:32:35 +00003873 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3874 // from a template argument of type std::nullptr_t to a non-type
3875 // template parameter of type pointer to object, pointer to
3876 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003877 if (ArgType->isNullPtrType()) {
3878 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3879 Converted = TemplateArgument((NamedDecl *)0);
3880 return Owned(Arg);
3881 }
3882
3883 if (ParamType->isNullPtrType()) {
3884 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3885 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3886 return Owned(Arg);
3887 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003888 }
3889
Douglas Gregorb86b0572009-02-11 01:18:59 +00003890 // Handle pointer-to-function, reference-to-function, and
3891 // pointer-to-member-function all in (roughly) the same way.
3892 if (// -- For a non-type template-parameter of type pointer to
3893 // function, only the function-to-pointer conversion (4.3) is
3894 // applied. If the template-argument represents a set of
3895 // overloaded functions (or a pointer to such), the matching
3896 // function is selected from the set (13.4).
3897 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003898 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003899 // -- For a non-type template-parameter of type reference to
3900 // function, no conversions apply. If the template-argument
3901 // represents a set of overloaded functions, the matching
3902 // function is selected from the set (13.4).
3903 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003904 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003905 // -- For a non-type template-parameter of type pointer to
3906 // member function, no conversions apply. If the
3907 // template-argument represents a set of overloaded member
3908 // functions, the matching member function is selected from
3909 // the set (13.4).
3910 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003911 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003912 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003913
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003914 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003915 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003916 true,
3917 FoundResult)) {
3918 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003919 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003920
3921 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3922 ArgType = Arg->getType();
3923 } else
John Wiegley429bb272011-04-08 18:41:53 +00003924 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003925 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003926
John Wiegley429bb272011-04-08 18:41:53 +00003927 if (!ParamType->isMemberPointerType()) {
3928 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3929 ParamType,
3930 Arg, Converted))
3931 return ExprError();
3932 return Owned(Arg);
3933 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003934
John McCallf85e1932011-06-15 23:02:42 +00003935 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003936 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003937 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00003938 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
3939 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003940 } else if (!Context.hasSameUnqualifiedType(ArgType,
3941 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003942 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003943 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003944 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003945 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003946 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003947 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003948 }
Mike Stump1eb44332009-09-09 15:08:12 +00003949
John Wiegley429bb272011-04-08 18:41:53 +00003950 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3951 return ExprError();
3952 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003953 }
3954
Chris Lattnerfe90de72009-02-20 21:37:53 +00003955 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003956 // -- for a non-type template-parameter of type pointer to
3957 // object, qualification conversions (4.4) and the
3958 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003959 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003960 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003961 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003962
John Wiegley429bb272011-04-08 18:41:53 +00003963 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3964 ParamType,
3965 Arg, Converted))
3966 return ExprError();
3967 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003968 }
Mike Stump1eb44332009-09-09 15:08:12 +00003969
Ted Kremenek6217b802009-07-29 21:53:49 +00003970 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003971 // -- For a non-type template-parameter of type reference to
3972 // object, no conversions apply. The type referred to by the
3973 // reference may be more cv-qualified than the (otherwise
3974 // identical) type of the template-argument. The
3975 // template-parameter is bound directly to the
3976 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003977 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003978 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003979
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003980 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003981 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3982 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003983 true,
3984 FoundResult)) {
3985 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003986 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003987
3988 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3989 ArgType = Arg->getType();
3990 } else
John Wiegley429bb272011-04-08 18:41:53 +00003991 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003992 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003993
John Wiegley429bb272011-04-08 18:41:53 +00003994 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3995 ParamType,
3996 Arg, Converted))
3997 return ExprError();
3998 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003999 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004000
4001 // -- For a non-type template-parameter of type pointer to data
4002 // member, qualification conversions (4.4) are applied.
4003 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4004
John McCallf85e1932011-06-15 23:02:42 +00004005 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004006 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004007 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004008 } else if (IsQualificationConversion(ArgType, ParamType, false,
4009 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004010 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4011 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004012 } else {
4013 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004014 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004015 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004016 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004017 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004018 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004019 }
4020
John Wiegley429bb272011-04-08 18:41:53 +00004021 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4022 return ExprError();
4023 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004024}
4025
4026/// \brief Check a template argument against its corresponding
4027/// template template parameter.
4028///
4029/// This routine implements the semantics of C++ [temp.arg.template].
4030/// It returns true if an error occurred, and false otherwise.
4031bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004032 const TemplateArgumentLoc &Arg) {
4033 TemplateName Name = Arg.getArgument().getAsTemplate();
4034 TemplateDecl *Template = Name.getAsTemplateDecl();
4035 if (!Template) {
4036 // Any dependent template name is fine.
4037 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4038 return false;
4039 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004040
Richard Smith3e4c6c42011-05-05 21:57:07 +00004041 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004042 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004043 // the name of a class template or an alias template, expressed as an
4044 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004045 // primary class templates are considered when matching the
4046 // template template argument with the corresponding parameter;
4047 // partial specializations are not considered even if their
4048 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004049 //
4050 // Note that we also allow template template parameters here, which
4051 // will happen when we are dealing with, e.g., class template
4052 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004053 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004054 !isa<TemplateTemplateParmDecl>(Template) &&
4055 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004056 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004057 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004058 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004059 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004060 << Template;
4061 }
4062
4063 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4064 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004065 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004066 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004067 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004068}
4069
Douglas Gregor02024a92010-03-28 02:42:43 +00004070/// \brief Given a non-type template argument that refers to a
4071/// declaration and the type of its corresponding non-type template
4072/// parameter, produce an expression that properly refers to that
4073/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004074ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004075Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4076 QualType ParamType,
4077 SourceLocation Loc) {
4078 assert(Arg.getKind() == TemplateArgument::Declaration &&
4079 "Only declaration template arguments permitted here");
4080 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4081
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004082 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004083 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4084 // If the value is a class member, we might have a pointer-to-member.
4085 // Determine whether the non-type template template parameter is of
4086 // pointer-to-member type. If so, we need to build an appropriate
4087 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4088 // would refer to the member itself.
4089 if (ParamType->isMemberPointerType()) {
4090 QualType ClassType
4091 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4092 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004093 = NestedNameSpecifier::Create(Context, 0, false,
4094 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004095 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004096 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004097
4098 // The actual value-ness of this is unimportant, but for
4099 // internal consistency's sake, references to instance methods
4100 // are r-values.
4101 ExprValueKind VK = VK_LValue;
4102 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4103 VK = VK_RValue;
4104
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004105 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004106 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004107 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004108 Loc,
4109 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004110 if (RefExpr.isInvalid())
4111 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004112
John McCall2de56d12010-08-25 11:45:40 +00004113 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004114
Douglas Gregorc0c83002010-04-30 21:46:38 +00004115 // We might need to perform a trailing qualification conversion, since
4116 // the element type on the parameter could be more qualified than the
4117 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004118 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004119 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004120 ParamType.getUnqualifiedType(), false,
4121 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004122 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004123
Douglas Gregor02024a92010-03-28 02:42:43 +00004124 assert(!RefExpr.isInvalid() &&
4125 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004126 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004127 return move(RefExpr);
4128 }
4129 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004130
Douglas Gregor02024a92010-03-28 02:42:43 +00004131 QualType T = VD->getType().getNonReferenceType();
4132 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004133 // When the non-type template parameter is a pointer, take the
4134 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004135 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004136 if (RefExpr.isInvalid())
4137 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004138
4139 if (T->isFunctionType() || T->isArrayType()) {
4140 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004141 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4142 if (RefExpr.isInvalid())
4143 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004144
4145 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004146 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004147
Douglas Gregorb7a09262010-04-01 18:32:35 +00004148 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004149 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004150 }
4151
John McCallf89e55a2010-11-18 06:31:45 +00004152 ExprValueKind VK = VK_RValue;
4153
Douglas Gregor02024a92010-03-28 02:42:43 +00004154 // If the non-type template parameter has reference type, qualify the
4155 // resulting declaration reference with the extra qualifiers on the
4156 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004157 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4158 VK = VK_LValue;
4159 T = Context.getQualifiedType(T,
4160 TargetRef->getPointeeType().getQualifiers());
4161 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004162
John McCallf89e55a2010-11-18 06:31:45 +00004163 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004164}
4165
4166/// \brief Construct a new expression that refers to the given
4167/// integral template argument with the given source-location
4168/// information.
4169///
4170/// This routine takes care of the mapping from an integral template
4171/// argument (which may have any integral type) to the appropriate
4172/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004173ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004174Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4175 SourceLocation Loc) {
4176 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004177 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004178 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004179 if (T->isAnyCharacterType()) {
4180 CharacterLiteral::CharacterKind Kind;
4181 if (T->isWideCharType())
4182 Kind = CharacterLiteral::Wide;
4183 else if (T->isChar16Type())
4184 Kind = CharacterLiteral::UTF16;
4185 else if (T->isChar32Type())
4186 Kind = CharacterLiteral::UTF32;
4187 else
4188 Kind = CharacterLiteral::Ascii;
4189
Douglas Gregor02024a92010-03-28 02:42:43 +00004190 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004191 Arg.getAsIntegral()->getZExtValue(),
4192 Kind, T, Loc));
4193 }
4194
Douglas Gregor02024a92010-03-28 02:42:43 +00004195 if (T->isBooleanType())
4196 return Owned(new (Context) CXXBoolLiteralExpr(
4197 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004198 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004199
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004200 if (T->isNullPtrType())
4201 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4202
Chris Lattner223de242011-04-25 20:37:58 +00004203 // If this is an enum type that we're instantiating, we need to use an integer
4204 // type the same size as the enumerator. We don't want to build an
4205 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004206 QualType BT;
4207 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004208 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004209 else
4210 BT = T;
4211
John McCall4e9272d2011-07-15 07:47:58 +00004212 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4213 if (T->isEnumeralType()) {
4214 // FIXME: This is a hack. We need a better way to handle substituted
4215 // non-type template parameters.
4216 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4217 Context.getTrivialTypeSourceInfo(T, Loc),
4218 Loc, Loc);
4219 }
4220
4221 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004222}
4223
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004224/// \brief Match two template parameters within template parameter lists.
4225static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4226 bool Complain,
4227 Sema::TemplateParameterListEqualKind Kind,
4228 SourceLocation TemplateArgLoc) {
4229 // Check the actual kind (type, non-type, template).
4230 if (Old->getKind() != New->getKind()) {
4231 if (Complain) {
4232 unsigned NextDiag = diag::err_template_param_different_kind;
4233 if (TemplateArgLoc.isValid()) {
4234 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4235 NextDiag = diag::note_template_param_different_kind;
4236 }
4237 S.Diag(New->getLocation(), NextDiag)
4238 << (Kind != Sema::TPL_TemplateMatch);
4239 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4240 << (Kind != Sema::TPL_TemplateMatch);
4241 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004242
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004243 return false;
4244 }
4245
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004246 // Check that both are parameter packs are neither are parameter packs.
4247 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004248 // template template parameter, the template template parameter can have
4249 // a parameter pack where the template template argument does not.
4250 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4251 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4252 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004253 if (Complain) {
4254 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4255 if (TemplateArgLoc.isValid()) {
4256 S.Diag(TemplateArgLoc,
4257 diag::err_template_arg_template_params_mismatch);
4258 NextDiag = diag::note_template_parameter_pack_non_pack;
4259 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004260
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004261 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4262 : isa<NonTypeTemplateParmDecl>(New)? 1
4263 : 2;
4264 S.Diag(New->getLocation(), NextDiag)
4265 << ParamKind << New->isParameterPack();
4266 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4267 << ParamKind << Old->isParameterPack();
4268 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004269
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004270 return false;
4271 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004272
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004273 // For non-type template parameters, check the type of the parameter.
4274 if (NonTypeTemplateParmDecl *OldNTTP
4275 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4276 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004277
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004278 // If we are matching a template template argument to a template
4279 // template parameter and one of the non-type template parameter types
4280 // is dependent, then we must wait until template instantiation time
4281 // to actually compare the arguments.
4282 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4283 (OldNTTP->getType()->isDependentType() ||
4284 NewNTTP->getType()->isDependentType()))
4285 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004286
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004287 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4288 if (Complain) {
4289 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4290 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004291 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004292 diag::err_template_arg_template_params_mismatch);
4293 NextDiag = diag::note_template_nontype_parm_different_type;
4294 }
4295 S.Diag(NewNTTP->getLocation(), NextDiag)
4296 << NewNTTP->getType()
4297 << (Kind != Sema::TPL_TemplateMatch);
4298 S.Diag(OldNTTP->getLocation(),
4299 diag::note_template_nontype_parm_prev_declaration)
4300 << OldNTTP->getType();
4301 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004302
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004303 return false;
4304 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004305
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004306 return true;
4307 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004308
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004309 // For template template parameters, check the template parameter types.
4310 // The template parameter lists of template template
4311 // parameters must agree.
4312 if (TemplateTemplateParmDecl *OldTTP
4313 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004314 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004315 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4316 OldTTP->getTemplateParameters(),
4317 Complain,
4318 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004319 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004320 : Kind),
4321 TemplateArgLoc);
4322 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004323
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004324 return true;
4325}
Douglas Gregor02024a92010-03-28 02:42:43 +00004326
Douglas Gregora0347822011-01-13 00:08:50 +00004327/// \brief Diagnose a known arity mismatch when comparing template argument
4328/// lists.
4329static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004330void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004331 TemplateParameterList *New,
4332 TemplateParameterList *Old,
4333 Sema::TemplateParameterListEqualKind Kind,
4334 SourceLocation TemplateArgLoc) {
4335 unsigned NextDiag = diag::err_template_param_list_different_arity;
4336 if (TemplateArgLoc.isValid()) {
4337 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4338 NextDiag = diag::note_template_param_list_different_arity;
4339 }
4340 S.Diag(New->getTemplateLoc(), NextDiag)
4341 << (New->size() > Old->size())
4342 << (Kind != Sema::TPL_TemplateMatch)
4343 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4344 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4345 << (Kind != Sema::TPL_TemplateMatch)
4346 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4347}
4348
Douglas Gregorddc29e12009-02-06 22:42:48 +00004349/// \brief Determine whether the given template parameter lists are
4350/// equivalent.
4351///
Mike Stump1eb44332009-09-09 15:08:12 +00004352/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004353/// source code as part of a new template declaration.
4354///
4355/// \param Old The old template parameter list, typically found via
4356/// name lookup of the template declared with this template parameter
4357/// list.
4358///
4359/// \param Complain If true, this routine will produce a diagnostic if
4360/// the template parameter lists are not equivalent.
4361///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004362/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004363///
4364/// \param TemplateArgLoc If this source location is valid, then we
4365/// are actually checking the template parameter list of a template
4366/// argument (New) against the template parameter list of its
4367/// corresponding template template parameter (Old). We produce
4368/// slightly different diagnostics in this scenario.
4369///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004370/// \returns True if the template parameter lists are equal, false
4371/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004372bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004373Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4374 TemplateParameterList *Old,
4375 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004376 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004377 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004378 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4379 if (Complain)
4380 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4381 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004382
4383 return false;
4384 }
4385
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004386 // C++0x [temp.arg.template]p3:
4387 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004388 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004389 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004390 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004391 // template-parameter-list of P. [...]
4392 TemplateParameterList::iterator NewParm = New->begin();
4393 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004394 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004395 OldParmEnd = Old->end();
4396 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004397 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4398 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004399 if (NewParm == NewParmEnd) {
4400 if (Complain)
4401 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4402 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004403
Douglas Gregora0347822011-01-13 00:08:50 +00004404 return false;
4405 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004406
Douglas Gregora0347822011-01-13 00:08:50 +00004407 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4408 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004409 return false;
4410
Douglas Gregora0347822011-01-13 00:08:50 +00004411 ++NewParm;
4412 continue;
4413 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004414
Douglas Gregora0347822011-01-13 00:08:50 +00004415 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004416 // [...] When P's template- parameter-list contains a template parameter
4417 // pack (14.5.3), the template parameter pack will match zero or more
4418 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004419 // template-parameter-list of A with the same type and form as the
4420 // template parameter pack in P (ignoring whether those template
4421 // parameters are template parameter packs).
4422 for (; NewParm != NewParmEnd; ++NewParm) {
4423 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4424 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004425 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004426 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004427 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004428
Douglas Gregora0347822011-01-13 00:08:50 +00004429 // Make sure we exhausted all of the arguments.
4430 if (NewParm != NewParmEnd) {
4431 if (Complain)
4432 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4433 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004434
Douglas Gregora0347822011-01-13 00:08:50 +00004435 return false;
4436 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004437
Douglas Gregorddc29e12009-02-06 22:42:48 +00004438 return true;
4439}
4440
4441/// \brief Check whether a template can be declared within this scope.
4442///
4443/// If the template declaration is valid in this scope, returns
4444/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004445bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004446Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004447 // Find the nearest enclosing declaration scope.
4448 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4449 (S->getFlags() & Scope::TemplateParamScope) != 0)
4450 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004451
Douglas Gregorddc29e12009-02-06 22:42:48 +00004452 // C++ [temp]p2:
4453 // A template-declaration can appear only as a namespace scope or
4454 // class scope declaration.
4455 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004456 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4457 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004458 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004459 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004460
Eli Friedman1503f772009-07-31 01:43:05 +00004461 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004462 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004463
4464 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4465 return false;
4466
Mike Stump1eb44332009-09-09 15:08:12 +00004467 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004468 diag::err_template_outside_namespace_or_class_scope)
4469 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004470}
Douglas Gregorcc636682009-02-17 23:15:12 +00004471
Douglas Gregord5cb8762009-10-07 00:13:32 +00004472/// \brief Determine what kind of template specialization the given declaration
4473/// is.
4474static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4475 if (!D)
4476 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004477
Douglas Gregorf6b11852009-10-08 15:14:33 +00004478 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4479 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004480 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4481 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004482 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4483 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004484
Douglas Gregord5cb8762009-10-07 00:13:32 +00004485 return TSK_Undeclared;
4486}
4487
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004488/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004489/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004490///
Douglas Gregor9302da62009-10-14 23:50:59 +00004491/// This routine determines whether a template specialization can be declared
4492/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004493///
4494/// \param S the semantic analysis object for which this check is being
4495/// performed.
4496///
4497/// \param Specialized the entity being specialized or instantiated, which
4498/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004499/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004500/// member class).
4501///
4502/// \param PrevDecl the previous declaration of this entity, if any.
4503///
4504/// \param Loc the location of the explicit specialization or instantiation of
4505/// this entity.
4506///
4507/// \param IsPartialSpecialization whether this is a partial specialization of
4508/// a class template.
4509///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004510/// \returns true if there was an error that we cannot recover from, false
4511/// otherwise.
4512static bool CheckTemplateSpecializationScope(Sema &S,
4513 NamedDecl *Specialized,
4514 NamedDecl *PrevDecl,
4515 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004516 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004517 // Keep these "kind" numbers in sync with the %select statements in the
4518 // various diagnostics emitted by this routine.
4519 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004520 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004521 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004522 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004523 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004524 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004525 EntityKind = 3;
4526 else if (isa<VarDecl>(Specialized))
4527 EntityKind = 4;
4528 else if (isa<RecordDecl>(Specialized))
4529 EntityKind = 5;
4530 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004531 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4532 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004533 return true;
4534 }
4535
Douglas Gregor88b70942009-02-25 22:02:03 +00004536 // C++ [temp.expl.spec]p2:
4537 // An explicit specialization shall be declared in the namespace
4538 // of which the template is a member, or, for member templates, in
4539 // the namespace of which the enclosing class or enclosing class
4540 // template is a member. An explicit specialization of a member
4541 // function, member class or static data member of a class
4542 // template shall be declared in the namespace of which the class
4543 // template is a member. Such a declaration may also be a
4544 // definition. If the declaration is not a definition, the
4545 // specialization may be defined later in the name- space in which
4546 // the explicit specialization was declared, or in a namespace
4547 // that encloses the one in which the explicit specialization was
4548 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004549 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004550 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004551 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004552 return true;
4553 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004554
Douglas Gregor0a407472009-10-07 17:30:37 +00004555 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004556 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004557 // Do not warn for class scope explicit specialization during
4558 // instantiation, warning was already emitted during pattern
4559 // semantic analysis.
4560 if (!S.ActiveTemplateInstantiations.size())
4561 S.Diag(Loc, diag::ext_function_specialization_in_class)
4562 << Specialized;
4563 } else {
4564 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4565 << Specialized;
4566 return true;
4567 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004568 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004569
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004570 if (S.CurContext->isRecord() &&
4571 !S.CurContext->Equals(Specialized->getDeclContext())) {
4572 // Make sure that we're specializing in the right record context.
4573 // Otherwise, things can go horribly wrong.
4574 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4575 << Specialized;
4576 return true;
4577 }
4578
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004579 // C++ [temp.class.spec]p6:
4580 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004581 // in any namespace scope in which its definition may be defined (14.5.1
4582 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004583 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004584 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004585 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004586 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004587 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004588 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4589 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004590 // C++ [temp.exp.spec]p2:
4591 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004592 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004593 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004594 // An explicit specialization of a member function, member class or
4595 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004596 // namespace of which the class template is a member.
4597 //
4598 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004599 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004600 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004601 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4602 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4603 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4604 assert(!IsCPlusPlus0xExtension &&
4605 "DC encloses TU but isn't in enclosing namespace set");
4606 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004607 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004608 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4609 int Diag;
4610 if (!IsCPlusPlus0xExtension)
4611 Diag = diag::err_template_spec_decl_out_of_scope;
4612 else if (!S.getLangOptions().CPlusPlus0x)
4613 Diag = diag::ext_template_spec_decl_out_of_scope;
4614 else
4615 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4616 S.Diag(Loc, Diag)
4617 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4618 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004619
Douglas Gregor9302da62009-10-14 23:50:59 +00004620 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004621 ComplainedAboutScope =
4622 !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004623 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004624 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004625
4626 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004627 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004628 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004629 // specializations of function templates, static data members, and member
4630 // functions, so we skip the check here for those kinds of entities.
4631 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004632 // Should we refactor that check, so that it occurs later?
4633 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004634 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4635 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004636 if (isa<TranslationUnitDecl>(SpecializedContext))
4637 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4638 << EntityKind << Specialized;
4639 else if (isa<NamespaceDecl>(SpecializedContext))
4640 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4641 << EntityKind << Specialized
4642 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004643
Douglas Gregor9302da62009-10-14 23:50:59 +00004644 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004645 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004646
Douglas Gregord5cb8762009-10-07 00:13:32 +00004647 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004648
Douglas Gregor88b70942009-02-25 22:02:03 +00004649 return false;
4650}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004651
Douglas Gregorbacb9492011-01-03 21:13:47 +00004652/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4653/// that checks non-type template partial specialization arguments.
4654static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4655 NonTypeTemplateParmDecl *Param,
4656 const TemplateArgument *Args,
4657 unsigned NumArgs) {
4658 for (unsigned I = 0; I != NumArgs; ++I) {
4659 if (Args[I].getKind() == TemplateArgument::Pack) {
4660 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004661 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004662 Args[I].pack_size()))
4663 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004664
Douglas Gregore94866f2009-06-12 21:21:02 +00004665 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004667
Douglas Gregorbacb9492011-01-03 21:13:47 +00004668 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004669 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004670 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004671 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004672
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004673 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004674 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4675 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004676
4677 // Strip off any implicit casts we added as part of type checking.
4678 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4679 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004680
Douglas Gregore94866f2009-06-12 21:21:02 +00004681 // C++ [temp.class.spec]p8:
4682 // A non-type argument is non-specialized if it is the name of a
4683 // non-type parameter. All other non-type arguments are
4684 // specialized.
4685 //
4686 // Below, we check the two conditions that only apply to
4687 // specialized non-type arguments, so skip any non-specialized
4688 // arguments.
4689 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004690 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004691 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004692
Douglas Gregore94866f2009-06-12 21:21:02 +00004693 // C++ [temp.class.spec]p9:
4694 // Within the argument list of a class template partial
4695 // specialization, the following restrictions apply:
4696 // -- A partially specialized non-type argument expression
4697 // shall not involve a template parameter of the partial
4698 // specialization except when the argument expression is a
4699 // simple identifier.
4700 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004701 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004702 diag::err_dependent_non_type_arg_in_partial_spec)
4703 << ArgExpr->getSourceRange();
4704 return true;
4705 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004706
Douglas Gregore94866f2009-06-12 21:21:02 +00004707 // -- The type of a template parameter corresponding to a
4708 // specialized non-type argument shall not be dependent on a
4709 // parameter of the specialization.
4710 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004711 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004712 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4713 << Param->getType()
4714 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004715 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004716 return true;
4717 }
4718 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004719
Douglas Gregorbacb9492011-01-03 21:13:47 +00004720 return false;
4721}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004722
Douglas Gregorbacb9492011-01-03 21:13:47 +00004723/// \brief Check the non-type template arguments of a class template
4724/// partial specialization according to C++ [temp.class.spec]p9.
4725///
4726/// \param TemplateParams the template parameters of the primary class
4727/// template.
4728///
4729/// \param TemplateArg the template arguments of the class template
4730/// partial specialization.
4731///
4732/// \returns true if there was an error, false otherwise.
4733static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4734 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004735 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004736 const TemplateArgument *ArgList = TemplateArgs.data();
4737
4738 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4739 NonTypeTemplateParmDecl *Param
4740 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4741 if (!Param)
4742 continue;
4743
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004744 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004745 &ArgList[I], 1))
4746 return true;
4747 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004748
4749 return false;
4750}
4751
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004752/// \brief Retrieve the previous declaration of the given declaration.
4753static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4754 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4755 return VD->getPreviousDeclaration();
4756 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4757 return FD->getPreviousDeclaration();
4758 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4759 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004760 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004761 return TD->getPreviousDeclaration();
4762 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4763 return FTD->getPreviousDeclaration();
4764 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4765 return CTD->getPreviousDeclaration();
4766 return 0;
4767}
4768
John McCalld226f652010-08-21 09:40:31 +00004769DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004770Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4771 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004772 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004773 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004774 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004775 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004776 SourceLocation TemplateNameLoc,
4777 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004778 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004779 SourceLocation RAngleLoc,
4780 AttributeList *Attr,
4781 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004782 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004783
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004784 // NOTE: KWLoc is the location of the tag keyword. This will instead
4785 // store the location of the outermost template keyword in the declaration.
4786 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4787 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4788
Douglas Gregorcc636682009-02-17 23:15:12 +00004789 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004790 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004791 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004792 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4793
4794 if (!ClassTemplate) {
4795 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004796 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004797 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4798 return true;
4799 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004800
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004801 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004802 bool isPartialSpecialization = false;
4803
Douglas Gregor88b70942009-02-25 22:02:03 +00004804 // Check the validity of the template headers that introduce this
4805 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004806 // FIXME: We probably shouldn't complain about these headers for
4807 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004808 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004809 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004810 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4811 TemplateNameLoc,
4812 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004813 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004814 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004815 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004816 isExplicitSpecialization,
4817 Invalid);
4818 if (Invalid)
4819 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004820
Douglas Gregor05396e22009-08-25 17:23:04 +00004821 if (TemplateParams && TemplateParams->size() > 0) {
4822 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004823
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004824 if (TUK == TUK_Friend) {
4825 Diag(KWLoc, diag::err_partial_specialization_friend)
4826 << SourceRange(LAngleLoc, RAngleLoc);
4827 return true;
4828 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004829
Douglas Gregor05396e22009-08-25 17:23:04 +00004830 // C++ [temp.class.spec]p10:
4831 // The template parameter list of a specialization shall not
4832 // contain default template argument values.
4833 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4834 Decl *Param = TemplateParams->getParam(I);
4835 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4836 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004837 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004838 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004839 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004840 }
4841 } else if (NonTypeTemplateParmDecl *NTTP
4842 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4843 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004844 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004845 diag::err_default_arg_in_partial_spec)
4846 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004847 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004848 }
4849 } else {
4850 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004851 if (TTP->hasDefaultArgument()) {
4852 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004853 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004854 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004855 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004856 }
4857 }
4858 }
Douglas Gregora735b202009-10-13 14:39:41 +00004859 } else if (TemplateParams) {
4860 if (TUK == TUK_Friend)
4861 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004862 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004863 SourceRange(TemplateParams->getTemplateLoc(),
4864 TemplateParams->getRAngleLoc()))
4865 << SourceRange(LAngleLoc, RAngleLoc);
4866 else
4867 isExplicitSpecialization = true;
4868 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004869 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004870 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004871 isExplicitSpecialization = true;
4872 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004873
Douglas Gregorcc636682009-02-17 23:15:12 +00004874 // Check that the specialization uses the same tag kind as the
4875 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004876 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4877 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004878 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004879 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004880 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004881 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004882 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004883 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004884 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004885 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004886 diag::note_previous_use);
4887 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4888 }
4889
Douglas Gregor40808ce2009-03-09 23:48:35 +00004890 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004891 TemplateArgumentListInfo TemplateArgs;
4892 TemplateArgs.setLAngleLoc(LAngleLoc);
4893 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004894 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004895
Douglas Gregor925910d2011-01-03 20:35:03 +00004896 // Check for unexpanded parameter packs in any of the template arguments.
4897 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004898 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004899 UPPC_PartialSpecialization))
4900 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004901
Douglas Gregorcc636682009-02-17 23:15:12 +00004902 // Check that the template argument list is well-formed for this
4903 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004904 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004905 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4906 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004907 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004908
Douglas Gregor910f8002010-11-07 23:05:16 +00004909 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004910 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004911
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004912 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004913 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004914 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004915 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004916 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004917 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004918 return true;
4919
Douglas Gregor561f8122011-07-01 01:22:09 +00004920 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004921 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004922 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004923 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004924 TemplateArgs.size(),
4925 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004926 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4927 << ClassTemplate->getDeclName();
4928 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004929 }
4930 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004931
Douglas Gregorcc636682009-02-17 23:15:12 +00004932 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004933 ClassTemplateSpecializationDecl *PrevDecl = 0;
4934
4935 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004936 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004937 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004938 = ClassTemplate->findPartialSpecialization(Converted.data(),
4939 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004940 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004941 else
4942 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004943 = ClassTemplate->findSpecialization(Converted.data(),
4944 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004945
4946 ClassTemplateSpecializationDecl *Specialization = 0;
4947
Douglas Gregor88b70942009-02-25 22:02:03 +00004948 // Check whether we can declare a class template specialization in
4949 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004950 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004951 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4952 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004953 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004954 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004955
Douglas Gregorb88e8882009-07-30 17:40:51 +00004956 // The canonical type
4957 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004958 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004959 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004960 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004961 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004962 // arguments was referenced but not declared, or we're only
4963 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004964 // declaration node as our own, updating its source location and
4965 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004966 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004967 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004968 if (TemplateParameterLists.size() > 0) {
4969 Specialization->setTemplateParameterListsInfo(Context,
4970 TemplateParameterLists.size(),
4971 (TemplateParameterList**) TemplateParameterLists.release());
4972 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004973 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004974 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004975 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004976 // Build the canonical type that describes the converted template
4977 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004978 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4979 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004980 Converted.data(),
4981 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004982
4983 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004984 ClassTemplate->getInjectedClassNameSpecialization())) {
4985 // C++ [temp.class.spec]p9b3:
4986 //
4987 // -- The argument list of the specialization shall not be identical
4988 // to the implicit argument list of the primary template.
4989 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00004990 << (TUK == TUK_Definition)
4991 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00004992 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4993 ClassTemplate->getIdentifier(),
4994 TemplateNameLoc,
4995 Attr,
4996 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00004997 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004998 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004999 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005000 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005001
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005002 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005003 ClassTemplatePartialSpecializationDecl *PrevPartial
5004 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005005 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005006 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005007 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005008 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005009 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005010 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005011 TemplateParams,
5012 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005013 Converted.data(),
5014 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005015 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005016 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005017 PrevPartial,
5018 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005019 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005020 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005021 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005022 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005023 (TemplateParameterList**) TemplateParameterLists.release());
5024 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005025
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005026 if (!PrevPartial)
5027 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005028 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005029
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005030 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005031 // template specialization, make a note of that.
5032 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5033 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005034
Douglas Gregor031a5882009-06-13 00:26:55 +00005035 // Check that all of the template parameters of the class template
5036 // partial specialization are deducible from the template
5037 // arguments. If not, this class template partial specialization
5038 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005039 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00005040 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005041 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005042 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005043 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005044 unsigned NumNonDeducible = 0;
5045 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
5046 if (!DeducibleParams[I])
5047 ++NumNonDeducible;
5048
5049 if (NumNonDeducible) {
5050 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5051 << (NumNonDeducible > 1)
5052 << SourceRange(TemplateNameLoc, RAngleLoc);
5053 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5054 if (!DeducibleParams[I]) {
5055 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5056 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005057 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005058 diag::note_partial_spec_unused_parameter)
5059 << Param->getDeclName();
5060 else
Mike Stump1eb44332009-09-09 15:08:12 +00005061 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005062 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005063 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005064 }
5065 }
5066 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005067 } else {
5068 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005069 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005070 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005071 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005072 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005073 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005074 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005075 Converted.data(),
5076 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005077 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005078 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005079 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005080 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005081 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005082 (TemplateParameterList**) TemplateParameterLists.release());
5083 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005084
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005085 if (!PrevDecl)
5086 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005087
5088 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005089 }
5090
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005091 // C++ [temp.expl.spec]p6:
5092 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005093 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005094 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005095 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005096 // use occurs; no diagnostic is required.
5097 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005098 bool Okay = false;
5099 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5100 // Is there any previous explicit specialization declaration?
5101 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5102 Okay = true;
5103 break;
5104 }
5105 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005106
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005107 if (!Okay) {
5108 SourceRange Range(TemplateNameLoc, RAngleLoc);
5109 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5110 << Context.getTypeDeclType(Specialization) << Range;
5111
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005112 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005113 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005114 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005115 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005116 return true;
5117 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005118 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005119
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005120 // If this is not a friend, note that this is an explicit specialization.
5121 if (TUK != TUK_Friend)
5122 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005123
5124 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005125 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005126 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005127 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005128 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005129 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005130 Diag(Def->getLocation(), diag::note_previous_definition);
5131 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005132 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005133 }
5134 }
5135
John McCall7f1b9872010-12-18 03:30:47 +00005136 if (Attr)
5137 ProcessDeclAttributeList(S, Specialization, Attr);
5138
Douglas Gregord023aec2011-09-09 20:53:38 +00005139 if (ModulePrivateLoc.isValid())
5140 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5141 << (isPartialSpecialization? 1 : 0)
5142 << FixItHint::CreateRemoval(ModulePrivateLoc);
5143
Douglas Gregorfc705b82009-02-26 22:19:44 +00005144 // Build the fully-sugared type for this class template
5145 // specialization as the user wrote in the specialization
5146 // itself. This means that we'll pretty-print the type retrieved
5147 // from the specialization's declaration the way that the user
5148 // actually wrote the specialization, rather than formatting the
5149 // name based on the "canonical" representation used to store the
5150 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005151 TypeSourceInfo *WrittenTy
5152 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5153 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005154 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005155 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005156 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005157 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005158 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005159
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005160 // C++ [temp.expl.spec]p9:
5161 // A template explicit specialization is in the scope of the
5162 // namespace in which the template was defined.
5163 //
5164 // We actually implement this paragraph where we set the semantic
5165 // context (in the creation of the ClassTemplateSpecializationDecl),
5166 // but we also maintain the lexical context where the actual
5167 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005168 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005169
Douglas Gregorcc636682009-02-17 23:15:12 +00005170 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005171 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005172 Specialization->startDefinition();
5173
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005174 if (TUK == TUK_Friend) {
5175 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5176 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005177 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005178 /*FIXME:*/KWLoc);
5179 Friend->setAccess(AS_public);
5180 CurContext->addDecl(Friend);
5181 } else {
5182 // Add the specialization into its lexical context, so that it can
5183 // be seen when iterating through the list of declarations in that
5184 // context. However, specializations are not found by name lookup.
5185 CurContext->addDecl(Specialization);
5186 }
John McCalld226f652010-08-21 09:40:31 +00005187 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005188}
Douglas Gregord57959a2009-03-27 23:10:48 +00005189
John McCalld226f652010-08-21 09:40:31 +00005190Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005191 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005192 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005193 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005194}
5195
John McCalld226f652010-08-21 09:40:31 +00005196Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005197 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005198 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005199 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005200 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005201
Douglas Gregor52591bf2009-06-24 00:54:41 +00005202 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005203 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005204 }
Mike Stump1eb44332009-09-09 15:08:12 +00005205
Douglas Gregor52591bf2009-06-24 00:54:41 +00005206 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005207
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005208 D.setFunctionDefinition(true);
John McCalld226f652010-08-21 09:40:31 +00005209 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005210 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005211 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005212 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005213 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005214 FunctionTemplate->getTemplatedDecl());
5215 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5216 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5217 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005218}
5219
John McCall75042392010-02-11 01:33:53 +00005220/// \brief Strips various properties off an implicit instantiation
5221/// that has just been explicitly specialized.
5222static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005223 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005224
5225 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5226 FD->setInlineSpecified(false);
5227 }
5228}
5229
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005230/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005231/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005232/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005233/// new specialization/instantiation will have any effect.
5234///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005235/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005236/// instantiation.
5237///
5238/// \param NewTSK the kind of the new explicit specialization or instantiation.
5239///
5240/// \param PrevDecl the previous declaration of the entity.
5241///
5242/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5243///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005244/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005245/// declaration was instantiated (either implicitly or explicitly).
5246///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005247/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005248/// specialization or instantiation has no effect and should be ignored.
5249///
5250/// \returns true if there was an error that should prevent the introduction of
5251/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005252bool
5253Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5254 TemplateSpecializationKind NewTSK,
5255 NamedDecl *PrevDecl,
5256 TemplateSpecializationKind PrevTSK,
5257 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005258 bool &HasNoEffect) {
5259 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005260
Douglas Gregor454885e2009-10-15 15:54:05 +00005261 switch (NewTSK) {
5262 case TSK_Undeclared:
5263 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005264 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005265
Douglas Gregor454885e2009-10-15 15:54:05 +00005266 case TSK_ExplicitSpecialization:
5267 switch (PrevTSK) {
5268 case TSK_Undeclared:
5269 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005270 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005271 // explicitly specialized or has merely been mentioned without any
5272 // instantiation.
5273 return false;
5274
5275 case TSK_ImplicitInstantiation:
5276 if (PrevPointOfInstantiation.isInvalid()) {
5277 // The declaration itself has not actually been instantiated, so it is
5278 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005279 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005280 return false;
5281 }
5282 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005283
Douglas Gregor454885e2009-10-15 15:54:05 +00005284 case TSK_ExplicitInstantiationDeclaration:
5285 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005286 assert((PrevTSK == TSK_ImplicitInstantiation ||
5287 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005288 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005289
Douglas Gregor454885e2009-10-15 15:54:05 +00005290 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005291 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005292 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005293 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005294 // implicit instantiation to take place, in every translation unit in
5295 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005296 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5297 // Is there any previous explicit specialization declaration?
5298 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5299 return false;
5300 }
5301
Douglas Gregor0d035142009-10-27 18:42:08 +00005302 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005303 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005304 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005305 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005306
Douglas Gregor454885e2009-10-15 15:54:05 +00005307 return true;
5308 }
5309 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005310
Douglas Gregor454885e2009-10-15 15:54:05 +00005311 case TSK_ExplicitInstantiationDeclaration:
5312 switch (PrevTSK) {
5313 case TSK_ExplicitInstantiationDeclaration:
5314 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005315 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005316 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005317
Douglas Gregor454885e2009-10-15 15:54:05 +00005318 case TSK_Undeclared:
5319 case TSK_ImplicitInstantiation:
5320 // We're explicitly instantiating something that may have already been
5321 // implicitly instantiated; that's fine.
5322 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005323
Douglas Gregor454885e2009-10-15 15:54:05 +00005324 case TSK_ExplicitSpecialization:
5325 // C++0x [temp.explicit]p4:
5326 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005327 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005328 // specialization for that template, the explicit instantiation has no
5329 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005330 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005331 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005332
Douglas Gregor454885e2009-10-15 15:54:05 +00005333 case TSK_ExplicitInstantiationDefinition:
5334 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005335 // If an entity is the subject of both an explicit instantiation
5336 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005337 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005338 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005339 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005340 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005341 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005342 assert(PrevPointOfInstantiation.isValid() &&
5343 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005344 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005345 return false;
5346 }
5347 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005348
Douglas Gregor454885e2009-10-15 15:54:05 +00005349 case TSK_ExplicitInstantiationDefinition:
5350 switch (PrevTSK) {
5351 case TSK_Undeclared:
5352 case TSK_ImplicitInstantiation:
5353 // We're explicitly instantiating something that may have already been
5354 // implicitly instantiated; that's fine.
5355 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005356
Douglas Gregor454885e2009-10-15 15:54:05 +00005357 case TSK_ExplicitSpecialization:
5358 // C++ DR 259, C++0x [temp.explicit]p4:
5359 // For a given set of template parameters, if an explicit
5360 // instantiation of a template appears after a declaration of
5361 // an explicit specialization for that template, the explicit
5362 // instantiation has no effect.
5363 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005364 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005365 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005366 // has been explicitly specialized.
Richard Smithebaf0e62011-10-18 20:49:44 +00005367 Diag(NewLoc, getLangOptions().CPlusPlus0x ?
5368 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5369 diag::ext_explicit_instantiation_after_specialization)
5370 << PrevDecl;
5371 Diag(PrevDecl->getLocation(),
5372 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005373 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005374 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005375
Douglas Gregor454885e2009-10-15 15:54:05 +00005376 case TSK_ExplicitInstantiationDeclaration:
5377 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005378 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005379 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005380
Douglas Gregor454885e2009-10-15 15:54:05 +00005381 case TSK_ExplicitInstantiationDefinition:
5382 // C++0x [temp.spec]p5:
5383 // For a given template and a given set of template-arguments,
5384 // - an explicit instantiation definition shall appear at most once
5385 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005386 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005387 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005388 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005389 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005390 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005391 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005392 }
5393 break;
5394 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005395
David Blaikieb219cfc2011-09-23 05:06:16 +00005396 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005397}
5398
John McCallaf2094e2010-04-08 09:05:18 +00005399/// \brief Perform semantic analysis for the given dependent function
5400/// template specialization. The only possible way to get a dependent
5401/// function template specialization is with a friend declaration,
5402/// like so:
5403///
5404/// template <class T> void foo(T);
5405/// template <class T> class A {
5406/// friend void foo<>(T);
5407/// };
5408///
5409/// There really isn't any useful analysis we can do here, so we
5410/// just store the information.
5411bool
5412Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5413 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5414 LookupResult &Previous) {
5415 // Remove anything from Previous that isn't a function template in
5416 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005417 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005418 LookupResult::Filter F = Previous.makeFilter();
5419 while (F.hasNext()) {
5420 NamedDecl *D = F.next()->getUnderlyingDecl();
5421 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005422 !FDLookupContext->InEnclosingNamespaceSetOf(
5423 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005424 F.erase();
5425 }
5426 F.done();
5427
5428 // Should this be diagnosed here?
5429 if (Previous.empty()) return true;
5430
5431 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5432 ExplicitTemplateArgs);
5433 return false;
5434}
5435
Abramo Bagnarae03db982010-05-20 15:32:11 +00005436/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005437/// specialization.
5438///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005439/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005440/// explicit function template specialization. On successful completion,
5441/// the function declaration \p FD will become a function template
5442/// specialization.
5443///
5444/// \param FD the function declaration, which will be updated to become a
5445/// function template specialization.
5446///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005447/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5448/// if any. Note that this may be valid info even when 0 arguments are
5449/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5450/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005451///
Francois Pichet59e7c562011-07-08 06:21:47 +00005452/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005453/// this function specialization.
5454bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005455Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005456 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005457 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005458 // The set of function template specializations that could match this
5459 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005460 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005461
Sebastian Redl7a126a42010-08-31 00:36:30 +00005462 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005463 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5464 I != E; ++I) {
5465 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5466 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005467 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005468 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005469 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5470 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005471 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005472
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005473 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005474 // A trailing template-argument can be left unspecified in the
5475 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005476 // provided it can be deduced from the function argument type.
5477 // Perform template argument deduction to determine whether we may be
5478 // specializing this template.
5479 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005480 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005481 FunctionDecl *Specialization = 0;
5482 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005483 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005484 FD->getType(),
5485 Specialization,
5486 Info)) {
5487 // FIXME: Template argument deduction failed; record why it failed, so
5488 // that we can provide nifty diagnostics.
5489 (void)TDK;
5490 continue;
5491 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005492
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005493 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005494 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005495 }
5496 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005497
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005498 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005499 UnresolvedSetIterator Result
5500 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005501 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005502 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005503 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005504 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005505 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005506 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005507 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005508 return true;
John McCallc373d482010-01-27 01:50:18 +00005509
5510 // Ignore access information; it doesn't figure into redeclaration checking.
5511 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005512
5513 FunctionTemplateSpecializationInfo *SpecInfo
5514 = Specialization->getTemplateSpecializationInfo();
5515 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005516
5517 // Note: do not overwrite location info if previous template
5518 // specialization kind was explicit.
5519 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5520 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5521 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005522
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005523 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005524 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005525
5526 // If this is a friend declaration, then we're not really declaring
5527 // an explicit specialization.
5528 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005529
Douglas Gregord5cb8762009-10-07 00:13:32 +00005530 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005531 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005532 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005533 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005534 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005535 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005536 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005537
5538 // C++ [temp.expl.spec]p6:
5539 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005540 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005541 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005542 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005543 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005544 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005545 if (!isFriend &&
5546 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005547 TSK_ExplicitSpecialization,
5548 Specialization,
5549 SpecInfo->getTemplateSpecializationKind(),
5550 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005551 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005552 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005553
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005554 // Mark the prior declaration as an explicit specialization, so that later
5555 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005556 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005557 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005558 MarkUnusedFileScopedDecl(Specialization);
5559 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005560
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005561 // Turn the given function declaration into a function template
5562 // specialization, with the template arguments from the previous
5563 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005564 // Take copies of (semantic and syntactic) template argument lists.
5565 const TemplateArgumentList* TemplArgs = new (Context)
5566 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005567 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005568 TemplArgs, /*InsertPos=*/0,
5569 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005570 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005571 FD->setStorageClass(Specialization->getStorageClass());
5572
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005573 // The "previous declaration" for this function template specialization is
5574 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005575 Previous.clear();
5576 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005577 return false;
5578}
5579
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005580/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005581/// specialization.
5582///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005583/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005584/// explicit member function specialization. On successful completion,
5585/// the function declaration \p FD will become a member function
5586/// specialization.
5587///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005588/// \param Member the member declaration, which will be updated to become a
5589/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005590///
John McCall68263142009-11-18 22:49:29 +00005591/// \param Previous the set of declarations, one of which may be specialized
5592/// by this function specialization; the set will be modified to contain the
5593/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005594bool
John McCall68263142009-11-18 22:49:29 +00005595Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005596 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005597
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005598 // Try to find the member we are instantiating.
5599 NamedDecl *Instantiation = 0;
5600 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005601 MemberSpecializationInfo *MSInfo = 0;
5602
John McCall68263142009-11-18 22:49:29 +00005603 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005604 // Nowhere to look anyway.
5605 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005606 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5607 I != E; ++I) {
5608 NamedDecl *D = (*I)->getUnderlyingDecl();
5609 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005610 if (Context.hasSameType(Function->getType(), Method->getType())) {
5611 Instantiation = Method;
5612 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005613 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005614 break;
5615 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005616 }
5617 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005618 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005619 VarDecl *PrevVar;
5620 if (Previous.isSingleResult() &&
5621 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005622 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005623 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005624 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005625 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005626 }
5627 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005628 CXXRecordDecl *PrevRecord;
5629 if (Previous.isSingleResult() &&
5630 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5631 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005632 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005633 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005634 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005635 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005636
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005637 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005638 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005639 // specializations are always out-of-line, the caller will complain about
5640 // this mismatch later.
5641 return false;
5642 }
John McCall77e8b112010-04-13 20:37:33 +00005643
5644 // If this is a friend, just bail out here before we start turning
5645 // things into explicit specializations.
5646 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5647 // Preserve instantiation information.
5648 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5649 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5650 cast<CXXMethodDecl>(InstantiatedFrom),
5651 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5652 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5653 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5654 cast<CXXRecordDecl>(InstantiatedFrom),
5655 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5656 }
5657
5658 Previous.clear();
5659 Previous.addDecl(Instantiation);
5660 return false;
5661 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005662
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005663 // Make sure that this is a specialization of a member.
5664 if (!InstantiatedFrom) {
5665 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5666 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005667 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5668 return true;
5669 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005670
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005671 // C++ [temp.expl.spec]p6:
5672 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005673 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005674 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005675 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005676 // use occurs; no diagnostic is required.
5677 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005678
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005679 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005680 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5681 TSK_ExplicitSpecialization,
5682 Instantiation,
5683 MSInfo->getTemplateSpecializationKind(),
5684 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005685 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005686 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005687
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005688 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005689 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005690 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005691 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005692 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005693 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005694
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005695 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005696 // the original declaration to note that it is an explicit specialization
5697 // (if it was previously an implicit instantiation). This latter step
5698 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005699 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005700 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5701 if (InstantiationFunction->getTemplateSpecializationKind() ==
5702 TSK_ImplicitInstantiation) {
5703 InstantiationFunction->setTemplateSpecializationKind(
5704 TSK_ExplicitSpecialization);
5705 InstantiationFunction->setLocation(Member->getLocation());
5706 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005707
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005708 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5709 cast<CXXMethodDecl>(InstantiatedFrom),
5710 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005711 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005712 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005713 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5714 if (InstantiationVar->getTemplateSpecializationKind() ==
5715 TSK_ImplicitInstantiation) {
5716 InstantiationVar->setTemplateSpecializationKind(
5717 TSK_ExplicitSpecialization);
5718 InstantiationVar->setLocation(Member->getLocation());
5719 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005720
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005721 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5722 cast<VarDecl>(InstantiatedFrom),
5723 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005724 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005725 } else {
5726 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005727 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5728 if (InstantiationClass->getTemplateSpecializationKind() ==
5729 TSK_ImplicitInstantiation) {
5730 InstantiationClass->setTemplateSpecializationKind(
5731 TSK_ExplicitSpecialization);
5732 InstantiationClass->setLocation(Member->getLocation());
5733 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005734
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005735 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005736 cast<CXXRecordDecl>(InstantiatedFrom),
5737 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005738 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005739
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005740 // Save the caller the trouble of having to figure out which declaration
5741 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005742 Previous.clear();
5743 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005744 return false;
5745}
5746
Douglas Gregor558c0322009-10-14 23:41:34 +00005747/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005748///
5749/// \returns true if a serious error occurs, false otherwise.
5750static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005751 SourceLocation InstLoc,
5752 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005753 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5754 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005755
Douglas Gregor669eed82010-07-13 00:10:04 +00005756 if (CurContext->isRecord()) {
5757 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5758 << D;
5759 return true;
5760 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005761
Richard Smith3e2e91e2011-10-18 02:28:33 +00005762 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005763 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005764 // template. If the name declared in the explicit instantiation is an
5765 // unqualified name, the explicit instantiation shall appear in the
5766 // namespace where its template is declared or, if that namespace is inline
5767 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005768 //
5769 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005770 if (WasQualifiedName) {
5771 if (CurContext->Encloses(OrigContext))
5772 return false;
5773 } else {
5774 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5775 return false;
5776 }
5777
5778 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5779 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005780 S.Diag(InstLoc,
5781 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005782 diag::err_explicit_instantiation_out_of_scope :
5783 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005784 << D << NS;
5785 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005786 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005787 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005788 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5789 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5790 << D << NS;
5791 } else
5792 S.Diag(InstLoc,
5793 S.getLangOptions().CPlusPlus0x?
5794 diag::err_explicit_instantiation_must_be_global :
5795 diag::warn_explicit_instantiation_must_be_global_0x)
5796 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005797 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005798 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005799}
5800
5801/// \brief Determine whether the given scope specifier has a template-id in it.
5802static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5803 if (!SS.isSet())
5804 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005805
Richard Smith3e2e91e2011-10-18 02:28:33 +00005806 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005807 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005808 // or a static data member of a class template specialization, the name of
5809 // the class template specialization in the qualified-id for the member
5810 // name shall be a simple-template-id.
5811 //
5812 // C++98 has the same restriction, just worded differently.
5813 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5814 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005815 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005816 if (isa<TemplateSpecializationType>(T))
5817 return true;
5818
5819 return false;
5820}
5821
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005822// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005823DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005824Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005825 SourceLocation ExternLoc,
5826 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005827 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005828 SourceLocation KWLoc,
5829 const CXXScopeSpec &SS,
5830 TemplateTy TemplateD,
5831 SourceLocation TemplateNameLoc,
5832 SourceLocation LAngleLoc,
5833 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005834 SourceLocation RAngleLoc,
5835 AttributeList *Attr) {
5836 // Find the class template we're specializing
5837 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005838 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005839 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5840
5841 // Check that the specialization uses the same tag kind as the
5842 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005843 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5844 assert(Kind != TTK_Enum &&
5845 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005846 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005847 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005848 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005849 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005850 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005851 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005852 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005853 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005854 diag::note_previous_use);
5855 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5856 }
5857
Douglas Gregor558c0322009-10-14 23:41:34 +00005858 // C++0x [temp.explicit]p2:
5859 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005860 // definition and an explicit instantiation declaration. An explicit
5861 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005862 TemplateSpecializationKind TSK
5863 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5864 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005865
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005866 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005867 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005868 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005869
5870 // Check that the template argument list is well-formed for this
5871 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005872 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005873 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5874 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005875 return true;
5876
Douglas Gregor910f8002010-11-07 23:05:16 +00005877 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005878 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005879
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005880 // Find the class template specialization declaration that
5881 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005882 void *InsertPos = 0;
5883 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005884 = ClassTemplate->findSpecialization(Converted.data(),
5885 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005886
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005887 TemplateSpecializationKind PrevDecl_TSK
5888 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5889
Douglas Gregord5cb8762009-10-07 00:13:32 +00005890 // C++0x [temp.explicit]p2:
5891 // [...] An explicit instantiation shall appear in an enclosing
5892 // namespace of its template. [...]
5893 //
5894 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005895 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5896 SS.isSet()))
5897 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005898
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005899 ClassTemplateSpecializationDecl *Specialization = 0;
5900
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005901 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005902 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005903 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005904 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005905 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005906 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005907 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005908
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005909 // Even though HasNoEffect == true means that this explicit instantiation
5910 // has no effect on semantics, we go on to put its syntax in the AST.
5911
5912 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5913 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005914 // Since the only prior class template specialization with these
5915 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005916 // declaration node as our own, updating the source location
5917 // for the template name to reflect our new declaration.
5918 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005919 Specialization = PrevDecl;
5920 Specialization->setLocation(TemplateNameLoc);
5921 PrevDecl = 0;
5922 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005923 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005924
Douglas Gregor52604ab2009-09-11 21:19:12 +00005925 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005926 // Create a new class template specialization declaration node for
5927 // this explicit specialization.
5928 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005929 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005930 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005931 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005932 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005933 Converted.data(),
5934 Converted.size(),
5935 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005936 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005937
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005938 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005939 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005940 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005941 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005942 }
5943
5944 // Build the fully-sugared type for this explicit instantiation as
5945 // the user wrote in the explicit instantiation itself. This means
5946 // that we'll pretty-print the type retrieved from the
5947 // specialization's declaration the way that the user actually wrote
5948 // the explicit instantiation, rather than formatting the name based
5949 // on the "canonical" representation used to store the template
5950 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005951 TypeSourceInfo *WrittenTy
5952 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5953 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005954 Context.getTypeDeclType(Specialization));
5955 Specialization->setTypeAsWritten(WrittenTy);
5956 TemplateArgsIn.release();
5957
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005958 // Set source locations for keywords.
5959 Specialization->setExternLoc(ExternLoc);
5960 Specialization->setTemplateKeywordLoc(TemplateLoc);
5961
5962 // Add the explicit instantiation into its lexical context. However,
5963 // since explicit instantiations are never found by name lookup, we
5964 // just put it into the declaration context directly.
5965 Specialization->setLexicalDeclContext(CurContext);
5966 CurContext->addDecl(Specialization);
5967
5968 // Syntax is now OK, so return if it has no other effect on semantics.
5969 if (HasNoEffect) {
5970 // Set the template specialization kind.
5971 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005972 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005973 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005974
5975 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005976 // A definition of a class template or class member template
5977 // shall be in scope at the point of the explicit instantiation of
5978 // the class template or class member template.
5979 //
5980 // This check comes when we actually try to perform the
5981 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005982 ClassTemplateSpecializationDecl *Def
5983 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005984 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005985 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005986 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005987 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005988 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005989 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5990 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005991
Douglas Gregor0d035142009-10-27 18:42:08 +00005992 // Instantiate the members of this class template specialization.
5993 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005994 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005995 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005996 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5997
5998 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5999 // TSK_ExplicitInstantiationDefinition
6000 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6001 TSK == TSK_ExplicitInstantiationDefinition)
6002 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006003
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006004 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006005 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006006
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006007 // Set the template specialization kind.
6008 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006009 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006010}
6011
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006012// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006013DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006014Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006015 SourceLocation ExternLoc,
6016 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006017 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006018 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006019 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006020 IdentifierInfo *Name,
6021 SourceLocation NameLoc,
6022 AttributeList *Attr) {
6023
Douglas Gregor402abb52009-05-28 23:31:59 +00006024 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006025 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006026 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006027 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006028 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006029 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00006030 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006031 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006032 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6033
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006034 if (!TagD)
6035 return true;
6036
John McCalld226f652010-08-21 09:40:31 +00006037 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006038 if (Tag->isEnum()) {
6039 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6040 << Context.getTypeDeclType(Tag);
6041 return true;
6042 }
6043
Douglas Gregord0c87372009-05-27 17:30:49 +00006044 if (Tag->isInvalidDecl())
6045 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006046
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006047 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6048 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6049 if (!Pattern) {
6050 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6051 << Context.getTypeDeclType(Record);
6052 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6053 return true;
6054 }
6055
Douglas Gregor558c0322009-10-14 23:41:34 +00006056 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006057 // If the explicit instantiation is for a class or member class, the
6058 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006059 // simple-template-id.
6060 //
6061 // C++98 has the same restriction, just worded differently.
6062 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006063 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006064 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006065
Douglas Gregor558c0322009-10-14 23:41:34 +00006066 // C++0x [temp.explicit]p2:
6067 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006068 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006069 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006070 TemplateSpecializationKind TSK
6071 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6072 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006073
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006074 // C++0x [temp.explicit]p2:
6075 // [...] An explicit instantiation shall appear in an enclosing
6076 // namespace of its template. [...]
6077 //
6078 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006079 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006080
Douglas Gregor454885e2009-10-15 15:54:05 +00006081 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006082 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00006083 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00006084 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006085 PrevDecl = Record;
6086 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006087 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006088 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006089 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006090 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006091 PrevDecl,
6092 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006093 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006094 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006095 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006096 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006097 return TagD;
6098 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006099
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006100 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006101 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006102 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006103 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006104 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006105 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006106 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006107 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006108 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006109 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6110 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006111 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6112 << Pattern;
6113 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006114 } else {
6115 if (InstantiateClass(NameLoc, Record, Def,
6116 getTemplateInstantiationArgs(Record),
6117 TSK))
6118 return true;
6119
Douglas Gregor952b0172010-02-11 01:04:33 +00006120 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006121 if (!RecordDef)
6122 return true;
6123 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006124 }
6125
Douglas Gregor0d035142009-10-27 18:42:08 +00006126 // Instantiate all of the members of the class.
6127 InstantiateClassMembers(NameLoc, RecordDef,
6128 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006129
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006130 if (TSK == TSK_ExplicitInstantiationDefinition)
6131 MarkVTableUsed(NameLoc, RecordDef, true);
6132
Mike Stump390b4cc2009-05-16 07:39:55 +00006133 // FIXME: We don't have any representation for explicit instantiations of
6134 // member classes. Such a representation is not needed for compilation, but it
6135 // should be available for clients that want to see all of the declarations in
6136 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006137 return TagD;
6138}
6139
John McCallf312b1e2010-08-26 23:41:50 +00006140DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6141 SourceLocation ExternLoc,
6142 SourceLocation TemplateLoc,
6143 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006144 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006145 // TODO: check if/when DNInfo should replace Name.
6146 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6147 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006148 if (!Name) {
6149 if (!D.isInvalidType())
6150 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6151 diag::err_explicit_instantiation_requires_name)
6152 << D.getDeclSpec().getSourceRange()
6153 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006154
Douglas Gregord5a423b2009-09-25 18:43:00 +00006155 return true;
6156 }
6157
6158 // The scope passed in may not be a decl scope. Zip up the scope tree until
6159 // we find one that is.
6160 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6161 (S->getFlags() & Scope::TemplateParamScope) != 0)
6162 S = S->getParent();
6163
6164 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006165 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6166 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006167 if (R.isNull())
6168 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006169
Douglas Gregore885e182011-05-21 18:53:30 +00006170 // C++ [dcl.stc]p1:
6171 // A storage-class-specifier shall not be specified in [...] an explicit
6172 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006173 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006174 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6175 << Name;
6176 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006177 } else if (D.getDeclSpec().getStorageClassSpec()
6178 != DeclSpec::SCS_unspecified) {
6179 // Complain about then remove the storage class specifier.
6180 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6181 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6182
6183 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006184 }
6185
Douglas Gregor663b5a02009-10-14 20:14:33 +00006186 // C++0x [temp.explicit]p1:
6187 // [...] An explicit instantiation of a function template shall not use the
6188 // inline or constexpr specifiers.
6189 // Presumably, this also applies to member functions of class templates as
6190 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006191 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006192 Diag(D.getDeclSpec().getInlineSpecLoc(),
Richard Smith2dc7ece2011-10-18 03:44:03 +00006193 getLangOptions().CPlusPlus0x ?
6194 diag::err_explicit_instantiation_inline :
6195 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006196 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6197 if (D.getDeclSpec().isConstexprSpecified())
6198 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6199 // not already specified.
6200 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6201 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006202
Douglas Gregor558c0322009-10-14 23:41:34 +00006203 // C++0x [temp.explicit]p2:
6204 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006205 // definition and an explicit instantiation declaration. An explicit
6206 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006207 TemplateSpecializationKind TSK
6208 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6209 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006210
Abramo Bagnara25777432010-08-11 22:01:17 +00006211 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006212 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006213
6214 if (!R->isFunctionType()) {
6215 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006216 // A [...] static data member of a class template can be explicitly
6217 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006218 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006219 if (Previous.isAmbiguous())
6220 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006221
John McCall1bcee0a2009-12-02 08:25:40 +00006222 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006223 if (!Prev || !Prev->isStaticDataMember()) {
6224 // We expect to see a data data member here.
6225 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6226 << Name;
6227 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6228 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006229 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006230 return true;
6231 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006232
Douglas Gregord5a423b2009-09-25 18:43:00 +00006233 if (!Prev->getInstantiatedFromStaticDataMember()) {
6234 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006235 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006236 diag::err_explicit_instantiation_data_member_not_instantiated)
6237 << Prev;
6238 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6239 // FIXME: Can we provide a note showing where this was declared?
6240 return true;
6241 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006242
Douglas Gregor558c0322009-10-14 23:41:34 +00006243 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006244 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006245 // or a static data member of a class template specialization, the name of
6246 // the class template specialization in the qualified-id for the member
6247 // name shall be a simple-template-id.
6248 //
6249 // C++98 has the same restriction, just worded differently.
6250 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006251 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006252 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006253 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006254
Douglas Gregor558c0322009-10-14 23:41:34 +00006255 // Check the scope of this explicit instantiation.
6256 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006257
Douglas Gregor454885e2009-10-15 15:54:05 +00006258 // Verify that it is okay to explicitly instantiate here.
6259 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6260 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006261 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006262 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006263 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006264 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006265 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006266 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006267 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006268 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006269
Douglas Gregord5a423b2009-09-25 18:43:00 +00006270 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006271 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006272 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006273 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006274
Douglas Gregord5a423b2009-09-25 18:43:00 +00006275 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006276 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006278
6279 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006280 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006281 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006282 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006283 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6284 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006285 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6286 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006287 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6288 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006289 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006290 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006291 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006292 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006293 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006294
Douglas Gregord5a423b2009-09-25 18:43:00 +00006295 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006296 // A [...] function [...] can be explicitly instantiated from its template.
6297 // A member function [...] of a class template can be explicitly
6298 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006299 // template.
John McCallc373d482010-01-27 01:50:18 +00006300 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006301 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6302 P != PEnd; ++P) {
6303 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006304 if (!HasExplicitTemplateArgs) {
6305 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6306 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6307 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006308
John McCallc373d482010-01-27 01:50:18 +00006309 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006310 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6311 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006312 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006313 }
6314 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006315
Douglas Gregord5a423b2009-09-25 18:43:00 +00006316 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6317 if (!FunTmpl)
6318 continue;
6319
John McCall5769d612010-02-08 23:07:23 +00006320 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006321 FunctionDecl *Specialization = 0;
6322 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006323 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006324 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006325 R, Specialization, Info)) {
6326 // FIXME: Keep track of almost-matches?
6327 (void)TDK;
6328 continue;
6329 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006330
John McCallc373d482010-01-27 01:50:18 +00006331 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006332 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006333
Douglas Gregord5a423b2009-09-25 18:43:00 +00006334 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006335 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006336 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006337 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006338 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6339 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6340 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006341
John McCallc373d482010-01-27 01:50:18 +00006342 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006343 return true;
John McCallc373d482010-01-27 01:50:18 +00006344
6345 // Ignore access control bits, we don't need them for redeclaration checking.
6346 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006347
Douglas Gregor0a897e32009-10-15 17:21:20 +00006348 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006349 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006350 diag::err_explicit_instantiation_member_function_not_instantiated)
6351 << Specialization
6352 << (Specialization->getTemplateSpecializationKind() ==
6353 TSK_ExplicitSpecialization);
6354 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6355 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006356 }
6357
Douglas Gregor0a897e32009-10-15 17:21:20 +00006358 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006359 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6360 PrevDecl = Specialization;
6361
Douglas Gregor0a897e32009-10-15 17:21:20 +00006362 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006363 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006364 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006365 PrevDecl,
6366 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006367 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006368 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006369 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006370
Douglas Gregor0a897e32009-10-15 17:21:20 +00006371 // FIXME: We may still want to build some representation of this
6372 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006373 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006374 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006375 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006376
6377 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006378
Douglas Gregor0a897e32009-10-15 17:21:20 +00006379 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006380 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006381
Douglas Gregor558c0322009-10-14 23:41:34 +00006382 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006383 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006384 // or a static data member of a class template specialization, the name of
6385 // the class template specialization in the qualified-id for the member
6386 // name shall be a simple-template-id.
6387 //
6388 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006389 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006390 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006391 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006392 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006393 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006394 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006395 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006396
Douglas Gregor558c0322009-10-14 23:41:34 +00006397 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006398 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006399 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006400 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006401 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006402
Douglas Gregord5a423b2009-09-25 18:43:00 +00006403 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006404 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006405}
6406
John McCallf312b1e2010-08-26 23:41:50 +00006407TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006408Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6409 const CXXScopeSpec &SS, IdentifierInfo *Name,
6410 SourceLocation TagLoc, SourceLocation NameLoc) {
6411 // This has to hold, because SS is expected to be defined.
6412 assert(Name && "Expected a name in a dependent tag");
6413
6414 NestedNameSpecifier *NNS
6415 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6416 if (!NNS)
6417 return true;
6418
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006419 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006420
Douglas Gregor48c89f42010-04-24 16:38:41 +00006421 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6422 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006423 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006424 return true;
6425 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006426
Douglas Gregor059101f2011-03-02 00:47:37 +00006427 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006428 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006429 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6430
6431 // Create type-source location information for this type.
6432 TypeLocBuilder TLB;
6433 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6434 TL.setKeywordLoc(TagLoc);
6435 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6436 TL.setNameLoc(NameLoc);
6437 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006438}
6439
John McCallf312b1e2010-08-26 23:41:50 +00006440TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006441Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6442 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006443 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006444 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006445 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006446
Richard Smithebaf0e62011-10-18 20:49:44 +00006447 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6448 Diag(TypenameLoc,
6449 getLangOptions().CPlusPlus0x ?
6450 diag::warn_cxx98_compat_typename_outside_of_template :
6451 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006452 << FixItHint::CreateRemoval(TypenameLoc);
6453
Douglas Gregor2494dd02011-03-01 01:34:45 +00006454 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006455 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6456 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006457 if (T.isNull())
6458 return true;
John McCall63b43852010-04-29 23:50:39 +00006459
6460 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6461 if (isa<DependentNameType>(T)) {
6462 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006463 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006464 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006465 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006466 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006467 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006468 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006469 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006470 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006471 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006472
John McCallb3d87482010-08-24 05:47:05 +00006473 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006474}
6475
John McCallf312b1e2010-08-26 23:41:50 +00006476TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006477Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6478 const CXXScopeSpec &SS,
6479 SourceLocation TemplateLoc,
6480 TemplateTy TemplateIn,
6481 SourceLocation TemplateNameLoc,
6482 SourceLocation LAngleLoc,
6483 ASTTemplateArgsPtr TemplateArgsIn,
6484 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006485 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6486 Diag(TypenameLoc,
6487 getLangOptions().CPlusPlus0x ?
6488 diag::warn_cxx98_compat_typename_outside_of_template :
6489 diag::ext_typename_outside_of_template)
6490 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006491
6492 // Translate the parser's template argument list in our AST format.
6493 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6494 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6495
6496 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006497 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6498 // Construct a dependent template specialization type.
6499 assert(DTN && "dependent template has non-dependent name?");
6500 assert(DTN->getQualifier()
6501 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6502 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6503 DTN->getQualifier(),
6504 DTN->getIdentifier(),
6505 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006506
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006507 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006508 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006509 DependentTemplateSpecializationTypeLoc SpecTL
6510 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006511 SpecTL.setLAngleLoc(LAngleLoc);
6512 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006513 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006514 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006515 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006516 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6517 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006518 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006519 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006520
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006521 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6522 if (T.isNull())
6523 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006524
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006525 // Provide source-location information for the template specialization
6526 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006527 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006528 TemplateSpecializationTypeLoc SpecTL
6529 = Builder.push<TemplateSpecializationTypeLoc>(T);
6530
6531 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006532 SpecTL.setLAngleLoc(LAngleLoc);
6533 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006534 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006535 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6536 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6537
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006538 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6539 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6540 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006541 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6542
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006543 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6544 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006545}
6546
Douglas Gregora02411e2011-02-27 22:46:49 +00006547
Douglas Gregord57959a2009-03-27 23:10:48 +00006548/// \brief Build the type that describes a C++ typename specifier,
6549/// e.g., "typename T::type".
6550QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006551Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6552 SourceLocation KeywordLoc,
6553 NestedNameSpecifierLoc QualifierLoc,
6554 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006555 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006556 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006557 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006558
John McCall77bb1aa2010-05-01 00:40:08 +00006559 DeclContext *Ctx = computeDeclContext(SS);
6560 if (!Ctx) {
6561 // If the nested-name-specifier is dependent and couldn't be
6562 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006563 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6564 return Context.getDependentNameType(Keyword,
6565 QualifierLoc.getNestedNameSpecifier(),
6566 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006567 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006568
John McCall77bb1aa2010-05-01 00:40:08 +00006569 // If the nested-name-specifier refers to the current instantiation,
6570 // the "typename" keyword itself is superfluous. In C++03, the
6571 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6572 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006573 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006574
John McCall77bb1aa2010-05-01 00:40:08 +00006575 if (RequireCompleteDeclContext(SS, Ctx))
6576 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006577
6578 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006579 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006580 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006581 unsigned DiagID = 0;
6582 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006583 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006584 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006585 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006586 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006587
6588 case LookupResult::FoundUnresolvedValue: {
6589 // We found a using declaration that is a value. Most likely, the using
6590 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006591 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006592 IILoc);
6593 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6594 << Name << Ctx << FullRange;
6595 if (UnresolvedUsingValueDecl *Using
6596 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006597 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006598 Diag(Loc, diag::note_using_value_decl_missing_typename)
6599 << FixItHint::CreateInsertion(Loc, "typename ");
6600 }
6601 }
6602 // Fall through to create a dependent typename type, from which we can recover
6603 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006604
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006605 case LookupResult::NotFoundInCurrentInstantiation:
6606 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006607 return Context.getDependentNameType(Keyword,
6608 QualifierLoc.getNestedNameSpecifier(),
6609 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006610
6611 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006612 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006613 // We found a type. Build an ElaboratedType, since the
6614 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006615 return Context.getElaboratedType(ETK_Typename,
6616 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006617 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006618 }
6619
6620 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006621 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006622 break;
6623
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006624
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006625 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006626 return QualType();
6627
Douglas Gregord57959a2009-03-27 23:10:48 +00006628 case LookupResult::FoundOverloaded:
6629 DiagID = diag::err_typename_nested_not_type;
6630 Referenced = *Result.begin();
6631 break;
6632
John McCall6e247262009-10-10 05:48:19 +00006633 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006634 return QualType();
6635 }
6636
6637 // If we get here, it's because name lookup did not find a
6638 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006639 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006640 IILoc);
6641 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006642 if (Referenced)
6643 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6644 << Name;
6645 return QualType();
6646}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006647
6648namespace {
6649 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006650 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006651 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006652 SourceLocation Loc;
6653 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006654
Douglas Gregor4a959d82009-08-06 16:20:37 +00006655 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006656 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006657
Mike Stump1eb44332009-09-09 15:08:12 +00006658 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006659 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006660 DeclarationName Entity)
6661 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006662 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006663
6664 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006665 /// transformed.
6666 ///
6667 /// For the purposes of type reconstruction, a type has already been
6668 /// transformed if it is NULL or if it is not dependent.
6669 bool AlreadyTransformed(QualType T) {
6670 return T.isNull() || !T->isDependentType();
6671 }
Mike Stump1eb44332009-09-09 15:08:12 +00006672
6673 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006674 /// rebuilt.
6675 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006676
Douglas Gregor4a959d82009-08-06 16:20:37 +00006677 /// \brief Returns the name of the entity whose type is being rebuilt.
6678 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006679
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006680 /// \brief Sets the "base" location and entity when that
6681 /// information is known based on another transformation.
6682 void setBase(SourceLocation Loc, DeclarationName Entity) {
6683 this->Loc = Loc;
6684 this->Entity = Entity;
6685 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006686 };
6687}
6688
Douglas Gregor4a959d82009-08-06 16:20:37 +00006689/// \brief Rebuilds a type within the context of the current instantiation.
6690///
Mike Stump1eb44332009-09-09 15:08:12 +00006691/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006692/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006693/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006694/// partial specialization thereof). This routine will rebuild that type now
6695/// that we have entered the declarator's scope, which may produce different
6696/// canonical types, e.g.,
6697///
6698/// \code
6699/// template<typename T>
6700/// struct X {
6701/// typedef T* pointer;
6702/// pointer data();
6703/// };
6704///
6705/// template<typename T>
6706/// typename X<T>::pointer X<T>::data() { ... }
6707/// \endcode
6708///
Douglas Gregor4714c122010-03-31 17:34:00 +00006709/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006710/// since we do not know that we can look into X<T> when we parsed the type.
6711/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006712/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006713/// as the canonical type of T*, allowing the return types of the out-of-line
6714/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006715TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6716 SourceLocation Loc,
6717 DeclarationName Name) {
6718 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006719 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006720
Douglas Gregor4a959d82009-08-06 16:20:37 +00006721 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6722 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006723}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006724
John McCall60d7b3a2010-08-24 06:29:42 +00006725ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006726 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6727 DeclarationName());
6728 return Rebuilder.TransformExpr(E);
6729}
6730
John McCall63b43852010-04-29 23:50:39 +00006731bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006732 if (SS.isInvalid())
6733 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006734
Douglas Gregor7e384942011-02-25 16:07:42 +00006735 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006736 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6737 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006738 NestedNameSpecifierLoc Rebuilt
6739 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6740 if (!Rebuilt)
6741 return true;
John McCall63b43852010-04-29 23:50:39 +00006742
Douglas Gregor7e384942011-02-25 16:07:42 +00006743 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006744 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006745}
6746
Douglas Gregor20606502011-10-14 15:31:12 +00006747/// \brief Rebuild the template parameters now that we know we're in a current
6748/// instantiation.
6749bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6750 TemplateParameterList *Params) {
6751 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6752 Decl *Param = Params->getParam(I);
6753
6754 // There is nothing to rebuild in a type parameter.
6755 if (isa<TemplateTypeParmDecl>(Param))
6756 continue;
6757
6758 // Rebuild the template parameter list of a template template parameter.
6759 if (TemplateTemplateParmDecl *TTP
6760 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6761 if (RebuildTemplateParamsInCurrentInstantiation(
6762 TTP->getTemplateParameters()))
6763 return true;
6764
6765 continue;
6766 }
6767
6768 // Rebuild the type of a non-type template parameter.
6769 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6770 TypeSourceInfo *NewTSI
6771 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6772 NTTP->getLocation(),
6773 NTTP->getDeclName());
6774 if (!NewTSI)
6775 return true;
6776
6777 if (NewTSI != NTTP->getTypeSourceInfo()) {
6778 NTTP->setTypeSourceInfo(NewTSI);
6779 NTTP->setType(NewTSI->getType());
6780 }
6781 }
6782
6783 return false;
6784}
6785
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006786/// \brief Produces a formatted string that describes the binding of
6787/// template parameters to template arguments.
6788std::string
6789Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6790 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006791 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006792}
6793
6794std::string
6795Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6796 const TemplateArgument *Args,
6797 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006798 llvm::SmallString<128> Str;
6799 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006800
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006801 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006802 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006803
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006804 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006805 if (I >= NumArgs)
6806 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006807
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006808 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006809 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006810 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006811 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006812
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006813 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006814 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006815 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006816 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006817 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006818
Douglas Gregor87dd6972010-12-20 16:52:59 +00006819 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006820 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006821 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006822
6823 Out << ']';
6824 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006825}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006826
6827void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6828 if (!FD)
6829 return;
6830 FD->setLateTemplateParsed(Flag);
6831}
6832
6833bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6834 DeclContext *DC = CurContext;
6835
6836 while (DC) {
6837 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6838 const FunctionDecl *FD = RD->isLocalClass();
6839 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6840 } else if (DC->isTranslationUnit() || DC->isNamespace())
6841 return false;
6842
6843 DC = DC->getParent();
6844 }
6845 return false;
6846}