blob: 8ec4c195f919428056753d4c235281b5ab1ea631 [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 &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +0000992 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +0000993 Diag(NameLoc, diag::err_member_def_does_not_match)
994 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +0000995 Invalid = true;
996 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000997 }
998
Mike Stump1eb44332009-09-09 15:08:12 +0000999 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001000 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001001 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001002 PrevClassTemplate->getTemplatedDecl() : 0,
1003 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001004 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001005 if (NumOuterTemplateParamLists > 0)
1006 NewClass->setTemplateParameterListsInfo(Context,
1007 NumOuterTemplateParamLists,
1008 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001009
1010 ClassTemplateDecl *NewTemplate
1011 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1012 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001013 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001014 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001015
1016 if (PrevClassTemplate && PrevClassTemplate->isModulePrivate()) {
1017 NewTemplate->setModulePrivate();
Douglas Gregore7612302011-09-09 19:05:14 +00001018 } else if (ModulePrivateLoc.isValid()) {
1019 if (PrevClassTemplate && !PrevClassTemplate->isModulePrivate())
1020 diagnoseModulePrivateRedeclaration(NewTemplate, PrevClassTemplate,
1021 ModulePrivateLoc);
1022 else
1023 NewTemplate->setModulePrivate();
1024 }
Douglas Gregor8d267c52011-09-09 02:06:17 +00001025
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001026 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001027 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001028 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001029 assert(T->isDependentType() && "Class template type is not dependent?");
1030 (void)T;
1031
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001032 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001033 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001034 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001035 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1036 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001037
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001038 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001039 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001040 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Douglas Gregorddc29e12009-02-06 22:42:48 +00001042 // Set the lexical context of these templates
1043 NewClass->setLexicalDeclContext(CurContext);
1044 NewTemplate->setLexicalDeclContext(CurContext);
1045
John McCall0f434ec2009-07-31 02:45:11 +00001046 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001047 NewClass->startDefinition();
1048
1049 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001050 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001051
John McCall05b23ea2009-09-14 21:59:20 +00001052 if (TUK != TUK_Friend)
1053 PushOnScopeChains(NewTemplate, S);
1054 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001055 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001056 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001057 NewClass->setAccess(PrevClassTemplate->getAccess());
1058 }
John McCall05b23ea2009-09-14 21:59:20 +00001059
Douglas Gregord85bea22009-09-26 06:47:28 +00001060 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1061 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001062
John McCall05b23ea2009-09-14 21:59:20 +00001063 // Friend templates are visible in fairly strange ways.
1064 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001065 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001066 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1067 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1068 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001069 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001070 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001071
Douglas Gregord85bea22009-09-26 06:47:28 +00001072 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1073 NewClass->getLocation(),
1074 NewTemplate,
1075 /*FIXME:*/NewClass->getLocation());
1076 Friend->setAccess(AS_public);
1077 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001078 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001079
Douglas Gregord684b002009-02-10 19:49:53 +00001080 if (Invalid) {
1081 NewTemplate->setInvalidDecl();
1082 NewClass->setInvalidDecl();
1083 }
John McCalld226f652010-08-21 09:40:31 +00001084 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001085}
1086
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001087/// \brief Diagnose the presence of a default template argument on a
1088/// template parameter, which is ill-formed in certain contexts.
1089///
1090/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001091static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001092 Sema::TemplateParamListContext TPC,
1093 SourceLocation ParamLoc,
1094 SourceRange DefArgRange) {
1095 switch (TPC) {
1096 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001097 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001098 return false;
1099
1100 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001101 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001102 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001103 // A default template-argument shall not be specified in a
1104 // function template declaration or a function template
1105 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001106 // If a friend function template declaration specifies a default
1107 // template-argument, that declaration shall be a definition and shall be
1108 // the only declaration of the function template in the translation unit.
1109 // (C++98/03 doesn't have this wording; see DR226).
Richard Smithebaf0e62011-10-18 20:49:44 +00001110 S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
1111 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1112 : diag::ext_template_parameter_default_in_function_template)
1113 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001114 return false;
1115
1116 case Sema::TPC_ClassTemplateMember:
1117 // C++0x [temp.param]p9:
1118 // A default template-argument shall not be specified in the
1119 // template-parameter-lists of the definition of a member of a
1120 // class template that appears outside of the member's class.
1121 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1122 << DefArgRange;
1123 return true;
1124
1125 case Sema::TPC_FriendFunctionTemplate:
1126 // C++ [temp.param]p9:
1127 // A default template-argument shall not be specified in a
1128 // friend template declaration.
1129 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1130 << DefArgRange;
1131 return true;
1132
1133 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1134 // for friend function templates if there is only a single
1135 // declaration (and it is a definition). Strange!
1136 }
1137
1138 return false;
1139}
1140
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001141/// \brief Check for unexpanded parameter packs within the template parameters
1142/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001143static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1144 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001145 TemplateParameterList *Params = TTP->getTemplateParameters();
1146 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1147 NamedDecl *P = Params->getParam(I);
1148 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001149 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001150 NTTP->getTypeSourceInfo(),
1151 Sema::UPPC_NonTypeTemplateParameterType))
1152 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001153
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001154 continue;
1155 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001156
1157 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001158 = dyn_cast<TemplateTemplateParmDecl>(P))
1159 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1160 return true;
1161 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001162
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001163 return false;
1164}
1165
Douglas Gregord684b002009-02-10 19:49:53 +00001166/// \brief Checks the validity of a template parameter list, possibly
1167/// considering the template parameter list from a previous
1168/// declaration.
1169///
1170/// If an "old" template parameter list is provided, it must be
1171/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1172/// template parameter list.
1173///
1174/// \param NewParams Template parameter list for a new template
1175/// declaration. This template parameter list will be updated with any
1176/// default arguments that are carried through from the previous
1177/// template parameter list.
1178///
1179/// \param OldParams If provided, template parameter list from a
1180/// previous declaration of the same template. Default template
1181/// arguments will be merged from the old template parameter list to
1182/// the new template parameter list.
1183///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001184/// \param TPC Describes the context in which we are checking the given
1185/// template parameter list.
1186///
Douglas Gregord684b002009-02-10 19:49:53 +00001187/// \returns true if an error occurred, false otherwise.
1188bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001189 TemplateParameterList *OldParams,
1190 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001191 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Douglas Gregord684b002009-02-10 19:49:53 +00001193 // C++ [temp.param]p10:
1194 // The set of default template-arguments available for use with a
1195 // template declaration or definition is obtained by merging the
1196 // default arguments from the definition (if in scope) and all
1197 // declarations in scope in the same way default function
1198 // arguments are (8.3.6).
1199 bool SawDefaultArgument = false;
1200 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001201
Mike Stump1a35fde2009-02-11 23:03:27 +00001202 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001203 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001204 if (OldParams)
1205 OldParam = OldParams->begin();
1206
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001207 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001208 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1209 NewParamEnd = NewParams->end();
1210 NewParam != NewParamEnd; ++NewParam) {
1211 // Variables used to diagnose redundant default arguments
1212 bool RedundantDefaultArg = false;
1213 SourceLocation OldDefaultLoc;
1214 SourceLocation NewDefaultLoc;
1215
David Blaikie1368e582011-10-19 05:19:50 +00001216 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001217 bool MissingDefaultArg = false;
1218
David Blaikie1368e582011-10-19 05:19:50 +00001219 // Variable used to diagnose non-final parameter packs
1220 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001221
Douglas Gregord684b002009-02-10 19:49:53 +00001222 if (TemplateTypeParmDecl *NewTypeParm
1223 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001224 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001225 if (NewTypeParm->hasDefaultArgument() &&
1226 DiagnoseDefaultTemplateArgument(*this, TPC,
1227 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001228 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001229 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001230 NewTypeParm->removeDefaultArgument();
1231
1232 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001233 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001234 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Anders Carlsson49d25572009-06-12 23:20:15 +00001236 if (NewTypeParm->isParameterPack()) {
1237 assert(!NewTypeParm->hasDefaultArgument() &&
1238 "Parameter packs can't have a default argument!");
1239 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001240 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001241 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001242 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1243 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1244 SawDefaultArgument = true;
1245 RedundantDefaultArg = true;
1246 PreviousDefaultArgLoc = NewDefaultLoc;
1247 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1248 // Merge the default argument from the old declaration to the
1249 // new declaration.
1250 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001251 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001252 true);
1253 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1254 } else if (NewTypeParm->hasDefaultArgument()) {
1255 SawDefaultArgument = true;
1256 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1257 } else if (SawDefaultArgument)
1258 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001259 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001260 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001261 // Check for unexpanded parameter packs.
1262 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001263 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001264 UPPC_NonTypeTemplateParameterType)) {
1265 Invalid = true;
1266 continue;
1267 }
1268
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001269 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001270 if (NewNonTypeParm->hasDefaultArgument() &&
1271 DiagnoseDefaultTemplateArgument(*this, TPC,
1272 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001273 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001274 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001275 }
1276
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001277 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001278 NonTypeTemplateParmDecl *OldNonTypeParm
1279 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001280 if (NewNonTypeParm->isParameterPack()) {
1281 assert(!NewNonTypeParm->hasDefaultArgument() &&
1282 "Parameter packs can't have a default argument!");
1283 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001284 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001285 NewNonTypeParm->hasDefaultArgument()) {
1286 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1287 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1288 SawDefaultArgument = true;
1289 RedundantDefaultArg = true;
1290 PreviousDefaultArgLoc = NewDefaultLoc;
1291 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1292 // Merge the default argument from the old declaration to the
1293 // new declaration.
1294 SawDefaultArgument = true;
1295 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001296 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001297 // parameter.
1298 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001299 OldNonTypeParm->getDefaultArgument(),
1300 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001301 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1302 } else if (NewNonTypeParm->hasDefaultArgument()) {
1303 SawDefaultArgument = true;
1304 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1305 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001306 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001307 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001308 TemplateTemplateParmDecl *NewTemplateParm
1309 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001310
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001311 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001312 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001313 Invalid = true;
1314 continue;
1315 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001316
David Blaikie1368e582011-10-19 05:19:50 +00001317 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001318 if (NewTemplateParm->hasDefaultArgument() &&
1319 DiagnoseDefaultTemplateArgument(*this, TPC,
1320 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001321 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001322 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001323
1324 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001325 TemplateTemplateParmDecl *OldTemplateParm
1326 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001327 if (NewTemplateParm->isParameterPack()) {
1328 assert(!NewTemplateParm->hasDefaultArgument() &&
1329 "Parameter packs can't have a default argument!");
1330 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001331 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001332 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001333 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1334 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001335 SawDefaultArgument = true;
1336 RedundantDefaultArg = true;
1337 PreviousDefaultArgLoc = NewDefaultLoc;
1338 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1339 // Merge the default argument from the old declaration to the
1340 // new declaration.
1341 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001342 // FIXME: We need to create a new kind of "default argument" expression
1343 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001344 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001345 OldTemplateParm->getDefaultArgument(),
1346 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001347 PreviousDefaultArgLoc
1348 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001349 } else if (NewTemplateParm->hasDefaultArgument()) {
1350 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001351 PreviousDefaultArgLoc
1352 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001353 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001354 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001355 }
1356
David Blaikie1368e582011-10-19 05:19:50 +00001357 // C++0x [temp.param]p11:
1358 // If a template parameter of a primary class template or alias template
1359 // is a template parameter pack, it shall be the last template parameter.
1360 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1361 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1362 Diag((*NewParam)->getLocation(),
1363 diag::err_template_param_pack_must_be_last_template_parameter);
1364 Invalid = true;
1365 }
1366
Douglas Gregord684b002009-02-10 19:49:53 +00001367 if (RedundantDefaultArg) {
1368 // C++ [temp.param]p12:
1369 // A template-parameter shall not be given default arguments
1370 // by two different declarations in the same scope.
1371 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1372 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1373 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001374 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001375 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001376 // If a template-parameter of a class template has a default
1377 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001378 // have a default template-argument supplied or be a template parameter
1379 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001380 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001381 diag::err_template_param_default_arg_missing);
1382 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1383 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001384 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001385 }
1386
1387 // If we have an old template parameter list that we're merging
1388 // in, move on to the next parameter.
1389 if (OldParams)
1390 ++OldParam;
1391 }
1392
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001393 // We were missing some default arguments at the end of the list, so remove
1394 // all of the default arguments.
1395 if (RemoveDefaultArguments) {
1396 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1397 NewParamEnd = NewParams->end();
1398 NewParam != NewParamEnd; ++NewParam) {
1399 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1400 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001401 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001402 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1403 NTTP->removeDefaultArgument();
1404 else
1405 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1406 }
1407 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001408
Douglas Gregord684b002009-02-10 19:49:53 +00001409 return Invalid;
1410}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001411
John McCall4e2cbb22010-10-20 05:44:58 +00001412namespace {
1413
1414/// A class which looks for a use of a certain level of template
1415/// parameter.
1416struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1417 typedef RecursiveASTVisitor<DependencyChecker> super;
1418
1419 unsigned Depth;
1420 bool Match;
1421
1422 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1423 NamedDecl *ND = Params->getParam(0);
1424 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1425 Depth = PD->getDepth();
1426 } else if (NonTypeTemplateParmDecl *PD =
1427 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1428 Depth = PD->getDepth();
1429 } else {
1430 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1431 }
1432 }
1433
1434 bool Matches(unsigned ParmDepth) {
1435 if (ParmDepth >= Depth) {
1436 Match = true;
1437 return true;
1438 }
1439 return false;
1440 }
1441
1442 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1443 return !Matches(T->getDepth());
1444 }
1445
1446 bool TraverseTemplateName(TemplateName N) {
1447 if (TemplateTemplateParmDecl *PD =
1448 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1449 if (Matches(PD->getDepth())) return false;
1450 return super::TraverseTemplateName(N);
1451 }
1452
1453 bool VisitDeclRefExpr(DeclRefExpr *E) {
1454 if (NonTypeTemplateParmDecl *PD =
1455 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1456 if (PD->getDepth() == Depth) {
1457 Match = true;
1458 return false;
1459 }
1460 }
1461 return super::VisitDeclRefExpr(E);
1462 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001463
1464 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1465 return TraverseType(T->getInjectedSpecializationType());
1466 }
John McCall4e2cbb22010-10-20 05:44:58 +00001467};
1468}
1469
Douglas Gregorc8406492011-05-10 18:27:06 +00001470/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001471/// list.
1472static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001473DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001474 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001475 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001476 return Checker.Match;
1477}
1478
Douglas Gregorc8406492011-05-10 18:27:06 +00001479// Find the source range corresponding to the named type in the given
1480// nested-name-specifier, if any.
1481static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1482 QualType T,
1483 const CXXScopeSpec &SS) {
1484 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1485 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1486 if (const Type *CurType = NNS->getAsType()) {
1487 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1488 return NNSLoc.getTypeLoc().getSourceRange();
1489 } else
1490 break;
1491
1492 NNSLoc = NNSLoc.getPrefix();
1493 }
1494
1495 return SourceRange();
1496}
1497
Mike Stump1eb44332009-09-09 15:08:12 +00001498/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001499/// specifier, returning the template parameter list that applies to the
1500/// name.
1501///
1502/// \param DeclStartLoc the start of the declaration that has a scope
1503/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001504///
Douglas Gregorc8406492011-05-10 18:27:06 +00001505/// \param DeclLoc The location of the declaration itself.
1506///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001507/// \param SS the scope specifier that will be matched to the given template
1508/// parameter lists. This scope specifier precedes a qualified name that is
1509/// being declared.
1510///
1511/// \param ParamLists the template parameter lists, from the outermost to the
1512/// innermost template parameter lists.
1513///
1514/// \param NumParamLists the number of template parameter lists in ParamLists.
1515///
John McCall77e8b112010-04-13 20:37:33 +00001516/// \param IsFriend Whether to apply the slightly different rules for
1517/// matching template parameters to scope specifiers in friend
1518/// declarations.
1519///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001520/// \param IsExplicitSpecialization will be set true if the entity being
1521/// declared is an explicit specialization, false otherwise.
1522///
Mike Stump1eb44332009-09-09 15:08:12 +00001523/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001524/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001525/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001526/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001527/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001528/// itself a template).
1529TemplateParameterList *
1530Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001531 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001532 const CXXScopeSpec &SS,
1533 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001534 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001535 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001536 bool &IsExplicitSpecialization,
1537 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001538 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001539 Invalid = false;
1540
1541 // The sequence of nested types to which we will match up the template
1542 // parameter lists. We first build this list by starting with the type named
1543 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001544 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001545 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001546 if (SS.getScopeRep()) {
1547 if (CXXRecordDecl *Record
1548 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1549 T = Context.getTypeDeclType(Record);
1550 else
1551 T = QualType(SS.getScopeRep()->getAsType(), 0);
1552 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001553
1554 // If we found an explicit specialization that prevents us from needing
1555 // 'template<>' headers, this will be set to the location of that
1556 // explicit specialization.
1557 SourceLocation ExplicitSpecLoc;
1558
1559 while (!T.isNull()) {
1560 NestedTypes.push_back(T);
1561
1562 // Retrieve the parent of a record type.
1563 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1564 // If this type is an explicit specialization, we're done.
1565 if (ClassTemplateSpecializationDecl *Spec
1566 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1567 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1568 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1569 ExplicitSpecLoc = Spec->getLocation();
1570 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001571 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001572 } else if (Record->getTemplateSpecializationKind()
1573 == TSK_ExplicitSpecialization) {
1574 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001575 break;
1576 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001577
1578 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1579 T = Context.getTypeDeclType(Parent);
1580 else
1581 T = QualType();
1582 continue;
1583 }
1584
1585 if (const TemplateSpecializationType *TST
1586 = T->getAs<TemplateSpecializationType>()) {
1587 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1588 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1589 T = Context.getTypeDeclType(Parent);
1590 else
1591 T = QualType();
1592 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001593 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001594 }
1595
1596 // Look one step prior in a dependent template specialization type.
1597 if (const DependentTemplateSpecializationType *DependentTST
1598 = T->getAs<DependentTemplateSpecializationType>()) {
1599 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1600 T = QualType(NNS->getAsType(), 0);
1601 else
1602 T = QualType();
1603 continue;
1604 }
1605
1606 // Look one step prior in a dependent name type.
1607 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1608 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1609 T = QualType(NNS->getAsType(), 0);
1610 else
1611 T = QualType();
1612 continue;
1613 }
1614
1615 // Retrieve the parent of an enumeration type.
1616 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1617 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1618 // check here.
1619 EnumDecl *Enum = EnumT->getDecl();
1620
1621 // Get to the parent type.
1622 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1623 T = Context.getTypeDeclType(Parent);
1624 else
1625 T = QualType();
1626 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001627 }
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Douglas Gregorc8406492011-05-10 18:27:06 +00001629 T = QualType();
1630 }
1631 // Reverse the nested types list, since we want to traverse from the outermost
1632 // to the innermost while checking template-parameter-lists.
1633 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001634
Douglas Gregorc8406492011-05-10 18:27:06 +00001635 // C++0x [temp.expl.spec]p17:
1636 // A member or a member template may be nested within many
1637 // enclosing class templates. In an explicit specialization for
1638 // such a member, the member declaration shall be preceded by a
1639 // template<> for each enclosing class template that is
1640 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001641 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001642 unsigned ParamIdx = 0;
1643 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1644 ++TypeIdx) {
1645 T = NestedTypes[TypeIdx];
1646
1647 // Whether we expect a 'template<>' header.
1648 bool NeedEmptyTemplateHeader = false;
1649
1650 // Whether we expect a template header with parameters.
1651 bool NeedNonemptyTemplateHeader = false;
1652
1653 // For a dependent type, the set of template parameters that we
1654 // expect to see.
1655 TemplateParameterList *ExpectedTemplateParams = 0;
1656
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001657 // C++0x [temp.expl.spec]p15:
1658 // A member or a member template may be nested within many enclosing
1659 // class templates. In an explicit specialization for such a member, the
1660 // member declaration shall be preceded by a template<> for each
1661 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001662 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1663 if (ClassTemplatePartialSpecializationDecl *Partial
1664 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1665 ExpectedTemplateParams = Partial->getTemplateParameters();
1666 NeedNonemptyTemplateHeader = true;
1667 } else if (Record->isDependentType()) {
1668 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001669 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001670 ->getTemplateParameters();
1671 NeedNonemptyTemplateHeader = true;
1672 }
1673 } else if (ClassTemplateSpecializationDecl *Spec
1674 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1675 // C++0x [temp.expl.spec]p4:
1676 // Members of an explicitly specialized class template are defined
1677 // in the same manner as members of normal classes, and not using
1678 // the template<> syntax.
1679 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1680 NeedEmptyTemplateHeader = true;
1681 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001682 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001683 } else if (Record->getTemplateSpecializationKind()) {
1684 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001685 != TSK_ExplicitSpecialization &&
1686 TypeIdx == NumTypes - 1)
1687 IsExplicitSpecialization = true;
1688
1689 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001690 }
1691 } else if (const TemplateSpecializationType *TST
1692 = T->getAs<TemplateSpecializationType>()) {
1693 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1694 ExpectedTemplateParams = Template->getTemplateParameters();
1695 NeedNonemptyTemplateHeader = true;
1696 }
1697 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1698 // FIXME: We actually could/should check the template arguments here
1699 // against the corresponding template parameter list.
1700 NeedNonemptyTemplateHeader = false;
1701 }
1702
Douglas Gregor89b9f102011-06-06 15:22:55 +00001703 // C++ [temp.expl.spec]p16:
1704 // In an explicit specialization declaration for a member of a class
1705 // template or a member template that ap- pears in namespace scope, the
1706 // member template and some of its enclosing class templates may remain
1707 // unspecialized, except that the declaration shall not explicitly
1708 // specialize a class member template if its en- closing class templates
1709 // are not explicitly specialized as well.
1710 if (ParamIdx < NumParamLists) {
1711 if (ParamLists[ParamIdx]->size() == 0) {
1712 if (SawNonEmptyTemplateParameterList) {
1713 Diag(DeclLoc, diag::err_specialize_member_of_template)
1714 << ParamLists[ParamIdx]->getSourceRange();
1715 Invalid = true;
1716 IsExplicitSpecialization = false;
1717 return 0;
1718 }
1719 } else
1720 SawNonEmptyTemplateParameterList = true;
1721 }
1722
Douglas Gregorc8406492011-05-10 18:27:06 +00001723 if (NeedEmptyTemplateHeader) {
1724 // If we're on the last of the types, and we need a 'template<>' header
1725 // here, then it's an explicit specialization.
1726 if (TypeIdx == NumTypes - 1)
1727 IsExplicitSpecialization = true;
1728
1729 if (ParamIdx < NumParamLists) {
1730 if (ParamLists[ParamIdx]->size() > 0) {
1731 // The header has template parameters when it shouldn't. Complain.
1732 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1733 diag::err_template_param_list_matches_nontemplate)
1734 << T
1735 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1736 ParamLists[ParamIdx]->getRAngleLoc())
1737 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1738 Invalid = true;
1739 return 0;
1740 }
1741
1742 // Consume this template header.
1743 ++ParamIdx;
1744 continue;
1745 }
1746
1747 if (!IsFriend) {
1748 // We don't have a template header, but we should.
1749 SourceLocation ExpectedTemplateLoc;
1750 if (NumParamLists > 0)
1751 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1752 else
1753 ExpectedTemplateLoc = DeclStartLoc;
1754
1755 Diag(DeclLoc, diag::err_template_spec_needs_header)
1756 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1757 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1758 }
1759
1760 continue;
1761 }
1762
1763 if (NeedNonemptyTemplateHeader) {
1764 // In friend declarations we can have template-ids which don't
1765 // depend on the corresponding template parameter lists. But
1766 // assume that empty parameter lists are supposed to match this
1767 // template-id.
1768 if (IsFriend && T->isDependentType()) {
1769 if (ParamIdx < NumParamLists &&
1770 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1771 ExpectedTemplateParams = 0;
1772 else
1773 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001774 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001775
Douglas Gregorc8406492011-05-10 18:27:06 +00001776 if (ParamIdx < NumParamLists) {
1777 // Check the template parameter list, if we can.
1778 if (ExpectedTemplateParams &&
1779 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1780 ExpectedTemplateParams,
1781 true, TPL_TemplateMatch))
1782 Invalid = true;
1783
1784 if (!Invalid &&
1785 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1786 TPC_ClassTemplateMember))
1787 Invalid = true;
1788
1789 ++ParamIdx;
1790 continue;
1791 }
1792
1793 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1794 << T
1795 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1796 Invalid = true;
1797 continue;
1798 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001799 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001800
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001801 // If there were at least as many template-ids as there were template
1802 // parameter lists, then there are no template parameter lists remaining for
1803 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001804 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001805 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001807 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001808 if (ParamIdx < NumParamLists - 1) {
1809 bool HasAnyExplicitSpecHeader = false;
1810 bool AllExplicitSpecHeaders = true;
1811 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1812 if (ParamLists[I]->size() == 0)
1813 HasAnyExplicitSpecHeader = true;
1814 else
1815 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001816 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001817
1818 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1819 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1820 : diag::err_template_spec_extra_headers)
1821 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1822 ParamLists[NumParamLists - 2]->getRAngleLoc());
1823
1824 // If there was a specialization somewhere, such that 'template<>' is
1825 // not required, and there were any 'template<>' headers, note where the
1826 // specialization occurred.
1827 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1828 Diag(ExplicitSpecLoc,
1829 diag::note_explicit_template_spec_does_not_need_header)
1830 << NestedTypes.back();
1831
1832 // We have a template parameter list with no corresponding scope, which
1833 // means that the resulting template declaration can't be instantiated
1834 // properly (we'll end up with dependent nodes when we shouldn't).
1835 if (!AllExplicitSpecHeaders)
1836 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001837 }
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Douglas Gregor89b9f102011-06-06 15:22:55 +00001839 // C++ [temp.expl.spec]p16:
1840 // In an explicit specialization declaration for a member of a class
1841 // template or a member template that ap- pears in namespace scope, the
1842 // member template and some of its enclosing class templates may remain
1843 // unspecialized, except that the declaration shall not explicitly
1844 // specialize a class member template if its en- closing class templates
1845 // are not explicitly specialized as well.
1846 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1847 SawNonEmptyTemplateParameterList) {
1848 Diag(DeclLoc, diag::err_specialize_member_of_template)
1849 << ParamLists[ParamIdx]->getSourceRange();
1850 Invalid = true;
1851 IsExplicitSpecialization = false;
1852 return 0;
1853 }
1854
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001855 // Return the last template parameter list, which corresponds to the
1856 // entity being declared.
1857 return ParamLists[NumParamLists - 1];
1858}
1859
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001860void Sema::NoteAllFoundTemplates(TemplateName Name) {
1861 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1862 Diag(Template->getLocation(), diag::note_template_declared_here)
1863 << (isa<FunctionTemplateDecl>(Template)? 0
1864 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001865 : isa<TypeAliasTemplateDecl>(Template)? 2
1866 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001867 << Template->getDeclName();
1868 return;
1869 }
1870
1871 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1872 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1873 IEnd = OST->end();
1874 I != IEnd; ++I)
1875 Diag((*I)->getLocation(), diag::note_template_declared_here)
1876 << 0 << (*I)->getDeclName();
1877
1878 return;
1879 }
1880}
1881
1882
Douglas Gregor7532dc62009-03-30 22:58:21 +00001883QualType Sema::CheckTemplateIdType(TemplateName Name,
1884 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001885 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001886 DependentTemplateName *DTN
1887 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001888 if (DTN && DTN->isIdentifier())
1889 // When building a template-id where the template-name is dependent,
1890 // assume the template is a type template. Either our assumption is
1891 // correct, or the code is ill-formed and will be diagnosed when the
1892 // dependent name is substituted.
1893 return Context.getDependentTemplateSpecializationType(ETK_None,
1894 DTN->getQualifier(),
1895 DTN->getIdentifier(),
1896 TemplateArgs);
1897
Douglas Gregor7532dc62009-03-30 22:58:21 +00001898 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001899 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1900 // We might have a substituted template template parameter pack. If so,
1901 // build a template specialization type for it.
1902 if (Name.getAsSubstTemplateTemplateParmPack())
1903 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001904
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001905 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1906 << Name;
1907 NoteAllFoundTemplates(Name);
1908 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001909 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001910
Douglas Gregor40808ce2009-03-09 23:48:35 +00001911 // Check that the template argument list is well-formed for this
1912 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001913 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001914 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001915 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001916 return QualType();
1917
Douglas Gregor910f8002010-11-07 23:05:16 +00001918 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001919 "Converted template argument list is too short!");
1920
1921 QualType CanonType;
1922
Douglas Gregor561f8122011-07-01 01:22:09 +00001923 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001924 if (TypeAliasTemplateDecl *AliasTemplate
1925 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1926 // Find the canonical type for this type alias template specialization.
1927 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1928 if (Pattern->isInvalidDecl())
1929 return QualType();
1930
1931 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1932 Converted.data(), Converted.size());
1933
1934 // Only substitute for the innermost template argument list.
1935 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001936 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001937 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1938 for (unsigned I = 0; I < Depth; ++I)
1939 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001940
1941 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1942 CanonType = SubstType(Pattern->getUnderlyingType(),
1943 TemplateArgLists, AliasTemplate->getLocation(),
1944 AliasTemplate->getDeclName());
1945 if (CanonType.isNull())
1946 return QualType();
1947 } else if (Name.isDependent() ||
1948 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001949 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001950 // This class template specialization is a dependent
1951 // type. Therefore, its canonical type is another class template
1952 // specialization type that contains all of the converted
1953 // arguments in canonical form. This ensures that, e.g., A<T> and
1954 // A<T, T> have identical types when A is declared as:
1955 //
1956 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001957 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001958 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001959 Converted.data(),
1960 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Douglas Gregor1275ae02009-07-28 23:00:59 +00001962 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001963 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001964 // In the future, we need to teach getTemplateSpecializationType to only
1965 // build the canonical type and return that to us.
1966 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001967
1968 // This might work out to be a current instantiation, in which
1969 // case the canonical type needs to be the InjectedClassNameType.
1970 //
1971 // TODO: in theory this could be a simple hashtable lookup; most
1972 // changes to CurContext don't change the set of current
1973 // instantiations.
1974 if (isa<ClassTemplateDecl>(Template)) {
1975 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1976 // If we get out to a namespace, we're done.
1977 if (Ctx->isFileContext()) break;
1978
1979 // If this isn't a record, keep looking.
1980 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1981 if (!Record) continue;
1982
1983 // Look for one of the two cases with InjectedClassNameTypes
1984 // and check whether it's the same template.
1985 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1986 !Record->getDescribedClassTemplate())
1987 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001988
John McCall31f17ec2010-04-27 00:57:59 +00001989 // Fetch the injected class name type and check whether its
1990 // injected type is equal to the type we just built.
1991 QualType ICNT = Context.getTypeDeclType(Record);
1992 QualType Injected = cast<InjectedClassNameType>(ICNT)
1993 ->getInjectedSpecializationType();
1994
1995 if (CanonType != Injected->getCanonicalTypeInternal())
1996 continue;
1997
1998 // If so, the canonical type of this TST is the injected
1999 // class name type of the record we just found.
2000 assert(ICNT.isCanonical());
2001 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002002 break;
2003 }
2004 }
Mike Stump1eb44332009-09-09 15:08:12 +00002005 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002006 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002007 // Find the class template specialization declaration that
2008 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002009 void *InsertPos = 0;
2010 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002011 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002012 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002013 if (!Decl) {
2014 // This is the first time we have referenced this class template
2015 // specialization. Create the canonical declaration and add it to
2016 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002017 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002018 ClassTemplate->getTemplatedDecl()->getTagKind(),
2019 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002020 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002021 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002022 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002023 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002024 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002025 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002026 Decl->setLexicalDeclContext(CurContext);
2027 }
2028
2029 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002030 assert(isa<RecordType>(CanonType) &&
2031 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002032 }
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Douglas Gregor40808ce2009-03-09 23:48:35 +00002034 // Build the fully-sugared type for this class template
2035 // specialization, which refers back to the class template
2036 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002037 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002038}
2039
John McCallf312b1e2010-08-26 23:41:50 +00002040TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002041Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2042 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002043 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002044 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002045 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002046 if (SS.isInvalid())
2047 return true;
2048
Douglas Gregor7532dc62009-03-30 22:58:21 +00002049 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002050
Douglas Gregor40808ce2009-03-09 23:48:35 +00002051 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002052 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002053 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002054
Douglas Gregora88f09f2011-02-28 17:23:35 +00002055 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2056 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2057 DTN->getQualifier(),
2058 DTN->getIdentifier(),
2059 TemplateArgs);
2060
2061 // Build type-source information.
2062 TypeLocBuilder TLB;
2063 DependentTemplateSpecializationTypeLoc SpecTL
2064 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002065 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002066 SpecTL.setNameLoc(TemplateLoc);
2067 SpecTL.setLAngleLoc(LAngleLoc);
2068 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002069 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002070 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2071 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2072 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2073 }
2074
John McCalld5532b62009-11-23 01:53:49 +00002075 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002076 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002077
2078 if (Result.isNull())
2079 return true;
2080
Douglas Gregor059101f2011-03-02 00:47:37 +00002081 // Build type-source information.
2082 TypeLocBuilder TLB;
2083 TemplateSpecializationTypeLoc SpecTL
2084 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2085 SpecTL.setTemplateNameLoc(TemplateLoc);
2086 SpecTL.setLAngleLoc(LAngleLoc);
2087 SpecTL.setRAngleLoc(RAngleLoc);
2088 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2089 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002090
Douglas Gregor059101f2011-03-02 00:47:37 +00002091 if (SS.isNotEmpty()) {
2092 // Create an elaborated-type-specifier containing the nested-name-specifier.
2093 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2094 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2095 ElabTL.setKeywordLoc(SourceLocation());
2096 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2097 }
2098
2099 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002100}
John McCallf1bbbb42009-09-04 01:14:41 +00002101
Douglas Gregor059101f2011-03-02 00:47:37 +00002102TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002103 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002104 SourceLocation TagLoc,
2105 CXXScopeSpec &SS,
2106 TemplateTy TemplateD,
2107 SourceLocation TemplateLoc,
2108 SourceLocation LAngleLoc,
2109 ASTTemplateArgsPtr TemplateArgsIn,
2110 SourceLocation RAngleLoc) {
2111 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2112
2113 // Translate the parser's template argument list in our AST format.
2114 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2115 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2116
2117 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002118 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002119 ElaboratedTypeKeyword Keyword
2120 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002121
Douglas Gregor059101f2011-03-02 00:47:37 +00002122 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2123 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2124 DTN->getQualifier(),
2125 DTN->getIdentifier(),
2126 TemplateArgs);
2127
2128 // Build type-source information.
2129 TypeLocBuilder TLB;
2130 DependentTemplateSpecializationTypeLoc SpecTL
2131 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2132 SpecTL.setKeywordLoc(TagLoc);
2133 SpecTL.setNameLoc(TemplateLoc);
2134 SpecTL.setLAngleLoc(LAngleLoc);
2135 SpecTL.setRAngleLoc(RAngleLoc);
2136 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2137 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2138 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2139 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2140 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002141
2142 if (TypeAliasTemplateDecl *TAT =
2143 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2144 // C++0x [dcl.type.elab]p2:
2145 // If the identifier resolves to a typedef-name or the simple-template-id
2146 // resolves to an alias template specialization, the
2147 // elaborated-type-specifier is ill-formed.
2148 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2149 Diag(TAT->getLocation(), diag::note_declared_at);
2150 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002151
2152 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2153 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002154 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002155
2156 // Check the tag kind
2157 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002158 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002159
John McCall6b2becf2009-09-08 17:47:29 +00002160 IdentifierInfo *Id = D->getIdentifier();
2161 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002162
Richard Trieubbf34c02011-06-10 03:11:26 +00002163 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2164 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002165 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002166 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002167 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002168 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002169 }
2170 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002171
2172 // Provide source-location information for the template specialization.
2173 TypeLocBuilder TLB;
2174 TemplateSpecializationTypeLoc SpecTL
2175 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2176 SpecTL.setTemplateNameLoc(TemplateLoc);
2177 SpecTL.setLAngleLoc(LAngleLoc);
2178 SpecTL.setRAngleLoc(RAngleLoc);
2179 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2180 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002181
Douglas Gregor059101f2011-03-02 00:47:37 +00002182 // Construct an elaborated type containing the nested-name-specifier (if any)
2183 // and keyword.
2184 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2185 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2186 ElabTL.setKeywordLoc(TagLoc);
2187 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2188 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002189}
2190
John McCall60d7b3a2010-08-24 06:29:42 +00002191ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002192 LookupResult &R,
2193 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002194 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002195 // FIXME: Can we do any checking at this point? I guess we could check the
2196 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002197 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002198 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002199 // foo<int> could identify a single function unambiguously
2200 // This approach does NOT work, since f<int>(1);
2201 // gets resolved prior to resorting to overload resolution
2202 // i.e., template<class T> void f(double);
2203 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002204
2205 // These should be filtered out by our callers.
2206 assert(!R.empty() && "empty lookup results when building templateid");
2207 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2208
John McCallc373d482010-01-27 01:50:18 +00002209 // We don't want lookup warnings at this point.
2210 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002211
John McCallf7a1a742009-11-24 19:00:30 +00002212 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002213 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002214 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002215 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002216 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002217 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002218
2219 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002220}
2221
John McCallf7a1a742009-11-24 19:00:30 +00002222// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002223ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002224Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002225 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002226 const TemplateArgumentListInfo &TemplateArgs) {
2227 DeclContext *DC;
2228 if (!(DC = computeDeclContext(SS, false)) ||
2229 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002230 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002231 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002232
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002233 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002234 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002235 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2236 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002237
John McCallf7a1a742009-11-24 19:00:30 +00002238 if (R.isAmbiguous())
2239 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002240
John McCallf7a1a742009-11-24 19:00:30 +00002241 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002242 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2243 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002244 return ExprError();
2245 }
2246
2247 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002248 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2249 << (NestedNameSpecifier*) SS.getScopeRep()
2250 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002251 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2252 return ExprError();
2253 }
2254
2255 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002256}
2257
Douglas Gregorc45c2322009-03-31 00:43:58 +00002258/// \brief Form a dependent template name.
2259///
2260/// This action forms a dependent template name given the template
2261/// name and its (presumably dependent) scope specifier. For
2262/// example, given "MetaFun::template apply", the scope specifier \p
2263/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2264/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002265TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002266 SourceLocation TemplateKWLoc,
2267 CXXScopeSpec &SS,
2268 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002269 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002270 bool EnteringContext,
2271 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002272 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2273 Diag(TemplateKWLoc,
2274 getLangOptions().CPlusPlus0x ?
2275 diag::warn_cxx98_compat_template_outside_of_template :
2276 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002277 << FixItHint::CreateRemoval(TemplateKWLoc);
2278
Douglas Gregor0707bc52010-01-19 16:01:07 +00002279 DeclContext *LookupCtx = 0;
2280 if (SS.isSet())
2281 LookupCtx = computeDeclContext(SS, EnteringContext);
2282 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002283 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002284 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002285 // C++0x [temp.names]p5:
2286 // If a name prefixed by the keyword template is not the name of
2287 // a template, the program is ill-formed. [Note: the keyword
2288 // template may not be applied to non-template members of class
2289 // templates. -end note ] [ Note: as is the case with the
2290 // typename prefix, the template prefix is allowed in cases
2291 // where it is not strictly necessary; i.e., when the
2292 // nested-name-specifier or the expression on the left of the ->
2293 // or . is not dependent on a template-parameter, or the use
2294 // does not appear in the scope of a template. -end note]
2295 //
2296 // Note: C++03 was more strict here, because it banned the use of
2297 // the "template" keyword prior to a template-name that was not a
2298 // dependent name. C++ DR468 relaxed this requirement (the
2299 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002300 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002301 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002302 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2303 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002304 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002305 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2306 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002307 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2308 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002309 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002310 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002311 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002312 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002313 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002314 << Name.getSourceRange()
2315 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002316 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002317 } else {
2318 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002319 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002320 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002321 }
2322
Mike Stump1eb44332009-09-09 15:08:12 +00002323 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002324 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002325
Douglas Gregor014e88d2009-11-03 23:16:33 +00002326 switch (Name.getKind()) {
2327 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002328 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002329 Name.Identifier));
2330 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002331
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002332 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002333 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002334 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002335 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002336
2337 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002338 llvm_unreachable(
2339 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002340
Douglas Gregor014e88d2009-11-03 23:16:33 +00002341 default:
2342 break;
2343 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002344
2345 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002346 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002347 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002348 << Name.getSourceRange()
2349 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002350 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002351}
2352
Mike Stump1eb44332009-09-09 15:08:12 +00002353bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002354 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002355 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002356 const TemplateArgument &Arg = AL.getArgument();
2357
Anders Carlsson436b1562009-06-13 00:33:33 +00002358 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002359 switch(Arg.getKind()) {
2360 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002361 // C++ [temp.arg.type]p1:
2362 // A template-argument for a template-parameter which is a
2363 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002364 break;
2365 case TemplateArgument::Template: {
2366 // We have a template type parameter but the template argument
2367 // is a template without any arguments.
2368 SourceRange SR = AL.getSourceRange();
2369 TemplateName Name = Arg.getAsTemplate();
2370 Diag(SR.getBegin(), diag::err_template_missing_args)
2371 << Name << SR;
2372 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2373 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002374
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002375 return true;
2376 }
2377 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002378 // We have a template type parameter but the template argument
2379 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002380 SourceRange SR = AL.getSourceRange();
2381 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002382 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Anders Carlsson436b1562009-06-13 00:33:33 +00002384 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002385 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002386 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002387
John McCalla93c9342009-12-07 02:54:59 +00002388 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002389 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Anders Carlsson436b1562009-06-13 00:33:33 +00002391 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002392 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2393
2394 // Objective-C ARC:
2395 // If an explicitly-specified template argument type is a lifetime type
2396 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2397 if (getLangOptions().ObjCAutoRefCount &&
2398 ArgType->isObjCLifetimeType() &&
2399 !ArgType.getObjCLifetime()) {
2400 Qualifiers Qs;
2401 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2402 ArgType = Context.getQualifiedType(ArgType, Qs);
2403 }
2404
2405 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002406 return false;
2407}
2408
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002409/// \brief Substitute template arguments into the default template argument for
2410/// the given template type parameter.
2411///
2412/// \param SemaRef the semantic analysis object for which we are performing
2413/// the substitution.
2414///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002415/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002416/// for.
2417///
2418/// \param TemplateLoc the location of the template name that started the
2419/// template-id we are checking.
2420///
2421/// \param RAngleLoc the location of the right angle bracket ('>') that
2422/// terminates the template-id.
2423///
2424/// \param Param the template template parameter whose default we are
2425/// substituting into.
2426///
2427/// \param Converted the list of template arguments provided for template
2428/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002429/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002430static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002431SubstDefaultTemplateArgument(Sema &SemaRef,
2432 TemplateDecl *Template,
2433 SourceLocation TemplateLoc,
2434 SourceLocation RAngleLoc,
2435 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002436 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002437 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002438
2439 // If the argument type is dependent, instantiate it now based
2440 // on the previously-computed template arguments.
2441 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002442 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002443 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002444
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002445 MultiLevelTemplateArgumentList AllTemplateArgs
2446 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2447
2448 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002449 Template, Converted.data(),
2450 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002451 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002452
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002453 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2454 Param->getDefaultArgumentLoc(),
2455 Param->getDeclName());
2456 }
2457
2458 return ArgType;
2459}
2460
2461/// \brief Substitute template arguments into the default template argument for
2462/// the given non-type template parameter.
2463///
2464/// \param SemaRef the semantic analysis object for which we are performing
2465/// the substitution.
2466///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002467/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002468/// for.
2469///
2470/// \param TemplateLoc the location of the template name that started the
2471/// template-id we are checking.
2472///
2473/// \param RAngleLoc the location of the right angle bracket ('>') that
2474/// terminates the template-id.
2475///
Douglas Gregor788cd062009-11-11 01:00:40 +00002476/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002477/// substituting into.
2478///
2479/// \param Converted the list of template arguments provided for template
2480/// parameters that precede \p Param in the template parameter list.
2481///
2482/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002483static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002484SubstDefaultTemplateArgument(Sema &SemaRef,
2485 TemplateDecl *Template,
2486 SourceLocation TemplateLoc,
2487 SourceLocation RAngleLoc,
2488 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002489 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002490 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002491 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002492
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002493 MultiLevelTemplateArgumentList AllTemplateArgs
2494 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002495
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002496 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002497 Template, Converted.data(),
2498 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002499 SourceRange(TemplateLoc, RAngleLoc));
2500
2501 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2502}
2503
Douglas Gregor788cd062009-11-11 01:00:40 +00002504/// \brief Substitute template arguments into the default template argument for
2505/// the given template template parameter.
2506///
2507/// \param SemaRef the semantic analysis object for which we are performing
2508/// the substitution.
2509///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002510/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002511/// for.
2512///
2513/// \param TemplateLoc the location of the template name that started the
2514/// template-id we are checking.
2515///
2516/// \param RAngleLoc the location of the right angle bracket ('>') that
2517/// terminates the template-id.
2518///
2519/// \param Param the template template parameter whose default we are
2520/// substituting into.
2521///
2522/// \param Converted the list of template arguments provided for template
2523/// parameters that precede \p Param in the template parameter list.
2524///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002525/// \param QualifierLoc Will be set to the nested-name-specifier (with
2526/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002527///
Douglas Gregor788cd062009-11-11 01:00:40 +00002528/// \returns the substituted template argument, or NULL if an error occurred.
2529static TemplateName
2530SubstDefaultTemplateArgument(Sema &SemaRef,
2531 TemplateDecl *Template,
2532 SourceLocation TemplateLoc,
2533 SourceLocation RAngleLoc,
2534 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002535 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002536 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002537 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002538 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002539
Douglas Gregor788cd062009-11-11 01:00:40 +00002540 MultiLevelTemplateArgumentList AllTemplateArgs
2541 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002542
Douglas Gregor788cd062009-11-11 01:00:40 +00002543 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002544 Template, Converted.data(),
2545 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002546 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002547
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002548 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002549 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002550 if (QualifierLoc) {
2551 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2552 AllTemplateArgs);
2553 if (!QualifierLoc)
2554 return TemplateName();
2555 }
2556
Douglas Gregor1d752d72011-03-02 18:46:51 +00002557 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002558 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002559 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002560 AllTemplateArgs);
2561}
2562
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002563/// \brief If the given template parameter has a default template
2564/// argument, substitute into that default template argument and
2565/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002566TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002567Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2568 SourceLocation TemplateLoc,
2569 SourceLocation RAngleLoc,
2570 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002571 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002572 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002573 if (!TypeParm->hasDefaultArgument())
2574 return TemplateArgumentLoc();
2575
John McCalla93c9342009-12-07 02:54:59 +00002576 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002577 TemplateLoc,
2578 RAngleLoc,
2579 TypeParm,
2580 Converted);
2581 if (DI)
2582 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2583
2584 return TemplateArgumentLoc();
2585 }
2586
2587 if (NonTypeTemplateParmDecl *NonTypeParm
2588 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2589 if (!NonTypeParm->hasDefaultArgument())
2590 return TemplateArgumentLoc();
2591
John McCall60d7b3a2010-08-24 06:29:42 +00002592 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002593 TemplateLoc,
2594 RAngleLoc,
2595 NonTypeParm,
2596 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002597 if (Arg.isInvalid())
2598 return TemplateArgumentLoc();
2599
2600 Expr *ArgE = Arg.takeAs<Expr>();
2601 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2602 }
2603
2604 TemplateTemplateParmDecl *TempTempParm
2605 = cast<TemplateTemplateParmDecl>(Param);
2606 if (!TempTempParm->hasDefaultArgument())
2607 return TemplateArgumentLoc();
2608
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002609
Douglas Gregor1d752d72011-03-02 18:46:51 +00002610 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002611 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002612 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002613 RAngleLoc,
2614 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002615 Converted,
2616 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002617 if (TName.isNull())
2618 return TemplateArgumentLoc();
2619
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002620 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002621 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002622 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2623}
2624
Douglas Gregore7526412009-11-11 19:31:23 +00002625/// \brief Check that the given template argument corresponds to the given
2626/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002627///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002628/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002629/// checked.
2630///
2631/// \param Arg The template argument.
2632///
2633/// \param Template The template in which the template argument resides.
2634///
2635/// \param TemplateLoc The location of the template name for the template
2636/// whose argument list we're matching.
2637///
2638/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2639/// the template argument list.
2640///
2641/// \param ArgumentPackIndex The index into the argument pack where this
2642/// argument will be placed. Only valid if the parameter is a parameter pack.
2643///
2644/// \param Converted The checked, converted argument will be added to the
2645/// end of this small vector.
2646///
2647/// \param CTAK Describes how we arrived at this particular template argument:
2648/// explicitly written, deduced, etc.
2649///
2650/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002651bool Sema::CheckTemplateArgument(NamedDecl *Param,
2652 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002653 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002654 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002655 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002656 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002657 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002658 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002659 // Check template type parameters.
2660 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002661 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002662
Douglas Gregord9e15302009-11-11 19:41:09 +00002663 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002664 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002665 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002666 // with the template arguments we've seen thus far. But if the
2667 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002668 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002669 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2670 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002671
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002672 if (NTTPType->isDependentType() &&
2673 !isa<TemplateTemplateParmDecl>(Template) &&
2674 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002675 // Do substitution on the type of the non-type template parameter.
2676 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002677 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002678 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002679
2680 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002681 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002682 NTTPType = SubstType(NTTPType,
2683 MultiLevelTemplateArgumentList(TemplateArgs),
2684 NTTP->getLocation(),
2685 NTTP->getDeclName());
2686 // If that worked, check the non-type template parameter type
2687 // for validity.
2688 if (!NTTPType.isNull())
2689 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2690 NTTP->getLocation());
2691 if (NTTPType.isNull())
2692 return true;
2693 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002694
Douglas Gregore7526412009-11-11 19:31:23 +00002695 switch (Arg.getArgument().getKind()) {
2696 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002697 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002698
Douglas Gregore7526412009-11-11 19:31:23 +00002699 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002700 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002701 ExprResult Res =
2702 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2703 Result, CTAK);
2704 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002705 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002706
Douglas Gregor910f8002010-11-07 23:05:16 +00002707 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002708 break;
2709 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002710
Douglas Gregore7526412009-11-11 19:31:23 +00002711 case TemplateArgument::Declaration:
2712 case TemplateArgument::Integral:
2713 // We've already checked this template argument, so just copy
2714 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002715 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002716 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002717
Douglas Gregore7526412009-11-11 19:31:23 +00002718 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002719 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002720 // We were given a template template argument. It may not be ill-formed;
2721 // see below.
2722 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002723 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2724 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002725 // We have a template argument such as \c T::template X, which we
2726 // parsed as a template template argument. However, since we now
2727 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002728 // template name into an expression.
2729
2730 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2731 Arg.getTemplateNameLoc());
2732
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002733 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002734 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002735 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002736 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002737 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002738
Douglas Gregora7fc9012011-01-05 18:58:31 +00002739 // If we parsed the template argument as a pack expansion, create a
2740 // pack expansion expression.
2741 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002742 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2743 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002744 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002745 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002746
Douglas Gregore7526412009-11-11 19:31:23 +00002747 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002748 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2749 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002750 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002751
Douglas Gregor910f8002010-11-07 23:05:16 +00002752 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002753 break;
2754 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002755
Douglas Gregore7526412009-11-11 19:31:23 +00002756 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002757 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002758 // therefore cannot be a non-type template argument.
2759 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2760 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002761
Douglas Gregore7526412009-11-11 19:31:23 +00002762 Diag(Param->getLocation(), diag::note_template_param_here);
2763 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002764
Douglas Gregore7526412009-11-11 19:31:23 +00002765 case TemplateArgument::Type: {
2766 // We have a non-type template parameter but the template
2767 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002768
Douglas Gregore7526412009-11-11 19:31:23 +00002769 // C++ [temp.arg]p2:
2770 // In a template-argument, an ambiguity between a type-id and
2771 // an expression is resolved to a type-id, regardless of the
2772 // form of the corresponding template-parameter.
2773 //
2774 // We warn specifically about this case, since it can be rather
2775 // confusing for users.
2776 QualType T = Arg.getArgument().getAsType();
2777 SourceRange SR = Arg.getSourceRange();
2778 if (T->isFunctionType())
2779 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2780 else
2781 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2782 Diag(Param->getLocation(), diag::note_template_param_here);
2783 return true;
2784 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002785
Douglas Gregore7526412009-11-11 19:31:23 +00002786 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002787 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002788 break;
2789 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002790
Douglas Gregore7526412009-11-11 19:31:23 +00002791 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002792 }
2793
2794
Douglas Gregore7526412009-11-11 19:31:23 +00002795 // Check template template parameters.
2796 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002797
Douglas Gregore7526412009-11-11 19:31:23 +00002798 // Substitute into the template parameter list of the template
2799 // template parameter, since previously-supplied template arguments
2800 // may appear within the template template parameter.
2801 {
2802 // Set up a template instantiation context.
2803 LocalInstantiationScope Scope(*this);
2804 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002805 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002806 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002807
2808 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002809 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002810 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002811 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002812 MultiLevelTemplateArgumentList(TemplateArgs)));
2813 if (!TempParm)
2814 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002815 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002816
Douglas Gregore7526412009-11-11 19:31:23 +00002817 switch (Arg.getArgument().getKind()) {
2818 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002819 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002820
Douglas Gregore7526412009-11-11 19:31:23 +00002821 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002822 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002823 if (CheckTemplateArgument(TempParm, Arg))
2824 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002825
Douglas Gregor910f8002010-11-07 23:05:16 +00002826 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002827 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002828
Douglas Gregore7526412009-11-11 19:31:23 +00002829 case TemplateArgument::Expression:
2830 case TemplateArgument::Type:
2831 // We have a template template parameter but the template
2832 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002833 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2834 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002835 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002836
Douglas Gregore7526412009-11-11 19:31:23 +00002837 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002838 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002839 "Declaration argument with template template parameter");
2840 break;
2841 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002842 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002843 "Integral argument with template template parameter");
2844 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002845
Douglas Gregore7526412009-11-11 19:31:23 +00002846 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002847 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002848 break;
2849 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002850
Douglas Gregore7526412009-11-11 19:31:23 +00002851 return false;
2852}
2853
Douglas Gregorc15cb382009-02-09 23:23:08 +00002854/// \brief Check that the given template argument list is well-formed
2855/// for specializing the given template.
2856bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2857 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002858 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002859 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002860 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002861 TemplateParameterList *Params = Template->getTemplateParameters();
2862 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002863 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002864 bool Invalid = false;
2865
John McCalld5532b62009-11-23 01:53:49 +00002866 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2867
Mike Stump1eb44332009-09-09 15:08:12 +00002868 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002869 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002870
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002871 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002872 (NumArgs < Params->getMinRequiredArguments() &&
2873 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002874 // FIXME: point at either the first arg beyond what we can handle,
2875 // or the '>', depending on whether we have too many or too few
2876 // arguments.
2877 SourceRange Range;
2878 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002879 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002880 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2881 << (NumArgs > NumParams)
2882 << (isa<ClassTemplateDecl>(Template)? 0 :
2883 isa<FunctionTemplateDecl>(Template)? 1 :
2884 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2885 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002886 Diag(Template->getLocation(), diag::note_template_decl_here)
2887 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002888 Invalid = true;
2889 }
Mike Stump1eb44332009-09-09 15:08:12 +00002890
2891 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002892 // [...] The type and form of each template-argument specified in
2893 // a template-id shall match the type and form specified for the
2894 // corresponding parameter declared by the template in its
2895 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002896 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002897 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002898 TemplateParameterList::iterator Param = Params->begin(),
2899 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002900 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002901 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002902 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002903 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002904 // If we have an expanded parameter pack, make sure we don't have too
2905 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002906 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002907 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002908 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002909 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2910 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2911 << true
2912 << (isa<ClassTemplateDecl>(Template)? 0 :
2913 isa<FunctionTemplateDecl>(Template)? 1 :
2914 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2915 << Template;
2916 Diag(Template->getLocation(), diag::note_template_decl_here)
2917 << Params->getSourceRange();
2918 return true;
2919 }
2920 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002921
Douglas Gregorf35f8282009-11-11 21:54:23 +00002922 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002923 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2924 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002925 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002926 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002927
Douglas Gregor14be16b2010-12-20 16:57:52 +00002928 if ((*Param)->isTemplateParameterPack()) {
2929 // The template parameter was a template parameter pack, so take the
2930 // deduced argument and place it on the argument pack. Note that we
2931 // stay on the same template parameter so that we can deduce more
2932 // arguments.
2933 ArgumentPack.push_back(Converted.back());
2934 Converted.pop_back();
2935 } else {
2936 // Move to the next template parameter.
2937 ++Param;
2938 }
2939 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002940 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002941 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002942
Douglas Gregor8735b292011-06-03 02:59:40 +00002943 // If we're checking a partial template argument list, we're done.
2944 if (PartialTemplateArgs) {
2945 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2946 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2947 ArgumentPack.data(),
2948 ArgumentPack.size()));
2949
2950 return Invalid;
2951 }
2952
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002953 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002954 // arguments, just break out now and we'll fill in the argument pack below.
2955 if ((*Param)->isTemplateParameterPack())
2956 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002957
Douglas Gregorf35f8282009-11-11 21:54:23 +00002958 // We have a default template argument that we will use.
2959 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002960
Douglas Gregorf35f8282009-11-11 21:54:23 +00002961 // Retrieve the default template argument from the template
2962 // parameter. For each kind of template parameter, we substitute the
2963 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002964 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002965 // the default argument.
2966 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2967 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002968 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002969 break;
2970 }
2971
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002972 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002973 Template,
2974 TemplateLoc,
2975 RAngleLoc,
2976 TTP,
2977 Converted);
2978 if (!ArgType)
2979 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002980
Douglas Gregorf35f8282009-11-11 21:54:23 +00002981 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2982 ArgType);
2983 } else if (NonTypeTemplateParmDecl *NTTP
2984 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2985 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002986 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002987 break;
2988 }
2989
John McCall60d7b3a2010-08-24 06:29:42 +00002990 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002991 TemplateLoc,
2992 RAngleLoc,
2993 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002994 Converted);
2995 if (E.isInvalid())
2996 return true;
2997
2998 Expr *Ex = E.takeAs<Expr>();
2999 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3000 } else {
3001 TemplateTemplateParmDecl *TempParm
3002 = cast<TemplateTemplateParmDecl>(*Param);
3003
3004 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003005 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00003006 break;
3007 }
3008
Douglas Gregor1d752d72011-03-02 18:46:51 +00003009 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003010 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003011 TemplateLoc,
3012 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003013 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003014 Converted,
3015 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003016 if (Name.isNull())
3017 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003018
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003019 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3020 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003021 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003022
Douglas Gregorf35f8282009-11-11 21:54:23 +00003023 // Introduce an instantiation record that describes where we are using
3024 // the default template argument.
3025 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003026 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003027 SourceRange(TemplateLoc, RAngleLoc));
3028
Douglas Gregorf35f8282009-11-11 21:54:23 +00003029 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003030 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003031 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003032 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003033
Douglas Gregor67714232011-03-03 02:41:12 +00003034 // Core issue 150 (assumed resolution): if this is a template template
3035 // parameter, keep track of the default template arguments from the
3036 // template definition.
3037 if (isTemplateTemplateParameter)
3038 TemplateArgs.addArgument(Arg);
3039
Douglas Gregor14be16b2010-12-20 16:57:52 +00003040 // Move to the next template parameter and argument.
3041 ++Param;
3042 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003043 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003044
Douglas Gregor14be16b2010-12-20 16:57:52 +00003045 // Form argument packs for each of the parameter packs remaining.
3046 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003047 // If we're checking a partial list of template arguments, don't fill
3048 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003049
3050 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003051 if (!HasParameterPack)
3052 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003053 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003054 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003055 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003056 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3057 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003058 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003059 ArgumentPack.clear();
3060 }
3061 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003062
Douglas Gregor14be16b2010-12-20 16:57:52 +00003063 ++Param;
3064 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003065
Douglas Gregorc15cb382009-02-09 23:23:08 +00003066 return Invalid;
3067}
3068
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003069namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003070 class UnnamedLocalNoLinkageFinder
3071 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003072 {
3073 Sema &S;
3074 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003075
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003076 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003077
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003078 public:
3079 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3080
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003081 bool Visit(QualType T) {
3082 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003083 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003084
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003085#define TYPE(Class, Parent) \
3086 bool Visit##Class##Type(const Class##Type *);
3087#define ABSTRACT_TYPE(Class, Parent) \
3088 bool Visit##Class##Type(const Class##Type *) { return false; }
3089#define NON_CANONICAL_TYPE(Class, Parent) \
3090 bool Visit##Class##Type(const Class##Type *) { return false; }
3091#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003092
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003093 bool VisitTagDecl(const TagDecl *Tag);
3094 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3095 };
3096}
3097
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003098bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003099 return false;
3100}
3101
3102bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3103 return Visit(T->getElementType());
3104}
3105
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003106bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003107 return Visit(T->getPointeeType());
3108}
3109
3110bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003111 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003112 return Visit(T->getPointeeType());
3113}
3114
3115bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003116 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003117 return Visit(T->getPointeeType());
3118}
3119
3120bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003121 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003122 return Visit(T->getPointeeType());
3123}
3124
3125bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003126 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003127 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3128}
3129
3130bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003131 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003132 return Visit(T->getElementType());
3133}
3134
3135bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003136 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003137 return Visit(T->getElementType());
3138}
3139
3140bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003141 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003142 return Visit(T->getElementType());
3143}
3144
3145bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003146 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003147 return Visit(T->getElementType());
3148}
3149
3150bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003151 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003152 return Visit(T->getElementType());
3153}
3154
3155bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3156 return Visit(T->getElementType());
3157}
3158
3159bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3160 return Visit(T->getElementType());
3161}
3162
3163bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3164 const FunctionProtoType* T) {
3165 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003166 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003167 A != AEnd; ++A) {
3168 if (Visit(*A))
3169 return true;
3170 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003171
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003172 return Visit(T->getResultType());
3173}
3174
3175bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3176 const FunctionNoProtoType* T) {
3177 return Visit(T->getResultType());
3178}
3179
3180bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3181 const UnresolvedUsingType*) {
3182 return false;
3183}
3184
3185bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3186 return false;
3187}
3188
3189bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3190 return Visit(T->getUnderlyingType());
3191}
3192
3193bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3194 return false;
3195}
3196
Sean Huntca63c202011-05-24 22:41:36 +00003197bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3198 const UnaryTransformType*) {
3199 return false;
3200}
3201
Richard Smith34b41d92011-02-20 03:19:35 +00003202bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3203 return Visit(T->getDeducedType());
3204}
3205
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003206bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3207 return VisitTagDecl(T->getDecl());
3208}
3209
3210bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3211 return VisitTagDecl(T->getDecl());
3212}
3213
3214bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3215 const TemplateTypeParmType*) {
3216 return false;
3217}
3218
Douglas Gregorc3069d62011-01-14 02:55:32 +00003219bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3220 const SubstTemplateTypeParmPackType *) {
3221 return false;
3222}
3223
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003224bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3225 const TemplateSpecializationType*) {
3226 return false;
3227}
3228
3229bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3230 const InjectedClassNameType* T) {
3231 return VisitTagDecl(T->getDecl());
3232}
3233
3234bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3235 const DependentNameType* T) {
3236 return VisitNestedNameSpecifier(T->getQualifier());
3237}
3238
3239bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3240 const DependentTemplateSpecializationType* T) {
3241 return VisitNestedNameSpecifier(T->getQualifier());
3242}
3243
Douglas Gregor7536dd52010-12-20 02:24:11 +00003244bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3245 const PackExpansionType* T) {
3246 return Visit(T->getPattern());
3247}
3248
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003249bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3250 return false;
3251}
3252
3253bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3254 const ObjCInterfaceType *) {
3255 return false;
3256}
3257
3258bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3259 const ObjCObjectPointerType *) {
3260 return false;
3261}
3262
Eli Friedmanb001de72011-10-06 23:00:33 +00003263bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3264 return Visit(T->getValueType());
3265}
3266
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003267bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3268 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003269 S.Diag(SR.getBegin(),
3270 S.getLangOptions().CPlusPlus0x ?
3271 diag::warn_cxx98_compat_template_arg_local_type :
3272 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003273 << S.Context.getTypeDeclType(Tag) << SR;
3274 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003275 }
3276
Richard Smith162e1c12011-04-15 14:24:37 +00003277 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003278 S.Diag(SR.getBegin(),
3279 S.getLangOptions().CPlusPlus0x ?
3280 diag::warn_cxx98_compat_template_arg_unnamed_type :
3281 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003282 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3283 return true;
3284 }
3285
3286 return false;
3287}
3288
3289bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3290 NestedNameSpecifier *NNS) {
3291 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3292 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003293
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003294 switch (NNS->getKind()) {
3295 case NestedNameSpecifier::Identifier:
3296 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003297 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003298 case NestedNameSpecifier::Global:
3299 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003300
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003301 case NestedNameSpecifier::TypeSpec:
3302 case NestedNameSpecifier::TypeSpecWithTemplate:
3303 return Visit(QualType(NNS->getAsType(), 0));
3304 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003305 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003306}
3307
3308
Douglas Gregorc15cb382009-02-09 23:23:08 +00003309/// \brief Check a template argument against its corresponding
3310/// template type parameter.
3311///
3312/// This routine implements the semantics of C++ [temp.arg.type]. It
3313/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003314bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003315 TypeSourceInfo *ArgInfo) {
3316 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003317 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003318 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003319
3320 if (Arg->isVariablyModifiedType()) {
3321 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003322 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003323 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003324 }
3325
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003326 // C++03 [temp.arg.type]p2:
3327 // A local type, a type with no linkage, an unnamed type or a type
3328 // compounded from any of these types shall not be used as a
3329 // template-argument for a template type-parameter.
3330 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003331 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003332 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003333 if (LangOpts.CPlusPlus0x ?
3334 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3335 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3336 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3337 SR.getBegin()) != DiagnosticsEngine::Ignored :
3338 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003339 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3340 (void)Finder.Visit(Context.getCanonicalType(Arg));
3341 }
3342
Douglas Gregorc15cb382009-02-09 23:23:08 +00003343 return false;
3344}
3345
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003346/// \brief Checks whether the given template argument is the address
3347/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003348static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003349CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3350 NonTypeTemplateParmDecl *Param,
3351 QualType ParamType,
3352 Expr *ArgIn,
3353 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003354 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003355 Expr *Arg = ArgIn;
3356 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003357
3358 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003359 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003360
3361 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003362 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003363 // A template-argument for a non-type, non-template
3364 // template-parameter shall be one of: [...]
3365 //
3366 // -- the address of an object or function with external
3367 // linkage, including function templates and function
3368 // template-ids but excluding non-static class members,
3369 // expressed as & id-expression where the & is optional if
3370 // the name refers to a function or array, or if the
3371 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003372
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003373 // In C++98/03 mode, give an extension warning on any extra parentheses.
3374 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3375 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003376 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003377 if (!Invalid && !ExtraParens) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003378 S.Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003379 S.getLangOptions().CPlusPlus0x ?
3380 diag::warn_cxx98_compat_template_arg_extra_parens :
3381 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003382 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003383 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003384 }
3385
3386 Arg = Parens->getSubExpr();
3387 }
3388
John McCall91a57552011-07-15 05:09:51 +00003389 while (SubstNonTypeTemplateParmExpr *subst =
3390 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3391 Arg = subst->getReplacement()->IgnoreImpCasts();
3392
Douglas Gregorb7a09262010-04-01 18:32:35 +00003393 bool AddressTaken = false;
3394 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003395 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003396 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003397 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003398 AddressTaken = true;
3399 AddrOpLoc = UnOp->getOperatorLoc();
3400 }
Francois Picheta343a412011-04-29 09:08:14 +00003401 }
John McCall91a57552011-07-15 05:09:51 +00003402
Francois Pichet62ec1f22011-09-17 17:15:52 +00003403 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003404 Converted = TemplateArgument(ArgIn);
3405 return false;
3406 }
3407
3408 while (SubstNonTypeTemplateParmExpr *subst =
3409 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3410 Arg = subst->getReplacement()->IgnoreImpCasts();
3411
3412 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003413 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003414 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3415 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003416 S.Diag(Param->getLocation(), diag::note_template_param_here);
3417 return true;
3418 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003419
3420 // Stop checking the precise nature of the argument if it is value dependent,
3421 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003422 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003423 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003424 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003425 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003426
Douglas Gregorb7a09262010-04-01 18:32:35 +00003427 if (!isa<ValueDecl>(DRE->getDecl())) {
3428 S.Diag(Arg->getSourceRange().getBegin(),
3429 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003430 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003431 S.Diag(Param->getLocation(), diag::note_template_param_here);
3432 return true;
3433 }
3434
3435 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003436
3437 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003438 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3439 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003440 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003441 S.Diag(Param->getLocation(), diag::note_template_param_here);
3442 return true;
3443 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003444
3445 // Cannot refer to non-static member functions
3446 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003447 if (!Method->isStatic()) {
3448 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003449 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003450 S.Diag(Param->getLocation(), diag::note_template_param_here);
3451 return true;
3452 }
Mike Stump1eb44332009-09-09 15:08:12 +00003453
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003454 // Functions must have external linkage.
3455 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003456 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003457 S.Diag(Arg->getSourceRange().getBegin(),
3458 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003459 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003460 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003461 << true;
3462 return true;
3463 }
3464
3465 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003466 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003467
Douglas Gregorb7a09262010-04-01 18:32:35 +00003468 // If the template parameter has pointer type, the function decays.
3469 if (ParamType->isPointerType() && !AddressTaken)
3470 ArgType = S.Context.getPointerType(Func->getType());
3471 else if (AddressTaken && ParamType->isReferenceType()) {
3472 // If we originally had an address-of operator, but the
3473 // parameter has reference type, complain and (if things look
3474 // like they will work) drop the address-of operator.
3475 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3476 ParamType.getNonReferenceType())) {
3477 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3478 << ParamType;
3479 S.Diag(Param->getLocation(), diag::note_template_param_here);
3480 return true;
3481 }
3482
3483 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3484 << ParamType
3485 << FixItHint::CreateRemoval(AddrOpLoc);
3486 S.Diag(Param->getLocation(), diag::note_template_param_here);
3487
3488 ArgType = Func->getType();
3489 }
3490 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003491 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003492 S.Diag(Arg->getSourceRange().getBegin(),
3493 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003494 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003495 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003496 << true;
3497 return true;
3498 }
3499
Douglas Gregorb7a09262010-04-01 18:32:35 +00003500 // A value of reference type is not an object.
3501 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003502 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003503 diag::err_template_arg_reference_var)
3504 << Var->getType() << Arg->getSourceRange();
3505 S.Diag(Param->getLocation(), diag::note_template_param_here);
3506 return true;
3507 }
3508
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003509 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003510 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003511
3512 // If the template parameter has pointer type, we must have taken
3513 // the address of this object.
3514 if (ParamType->isReferenceType()) {
3515 if (AddressTaken) {
3516 // If we originally had an address-of operator, but the
3517 // parameter has reference type, complain and (if things look
3518 // like they will work) drop the address-of operator.
3519 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3520 ParamType.getNonReferenceType())) {
3521 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3522 << ParamType;
3523 S.Diag(Param->getLocation(), diag::note_template_param_here);
3524 return true;
3525 }
3526
3527 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3528 << ParamType
3529 << FixItHint::CreateRemoval(AddrOpLoc);
3530 S.Diag(Param->getLocation(), diag::note_template_param_here);
3531
3532 ArgType = Var->getType();
3533 }
3534 } else if (!AddressTaken && ParamType->isPointerType()) {
3535 if (Var->getType()->isArrayType()) {
3536 // Array-to-pointer decay.
3537 ArgType = S.Context.getArrayDecayedType(Var->getType());
3538 } else {
3539 // If the template parameter has pointer type but the address of
3540 // this object was not taken, complain and (possibly) recover by
3541 // taking the address of the entity.
3542 ArgType = S.Context.getPointerType(Var->getType());
3543 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3544 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3545 << ParamType;
3546 S.Diag(Param->getLocation(), diag::note_template_param_here);
3547 return true;
3548 }
3549
3550 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3551 << ParamType
3552 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3553
3554 S.Diag(Param->getLocation(), diag::note_template_param_here);
3555 }
3556 }
3557 } else {
3558 // We found something else, but we don't know specifically what it is.
3559 S.Diag(Arg->getSourceRange().getBegin(),
3560 diag::err_template_arg_not_object_or_func)
3561 << Arg->getSourceRange();
3562 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3563 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003564 }
Mike Stump1eb44332009-09-09 15:08:12 +00003565
John McCallf85e1932011-06-15 23:02:42 +00003566 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003567 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003568 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003569 S.IsQualificationConversion(ArgType, ParamType, false,
3570 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003571 // For pointer-to-object types, qualification conversions are
3572 // permitted.
3573 } else {
3574 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3575 if (!ParamRef->getPointeeType()->isFunctionType()) {
3576 // C++ [temp.arg.nontype]p5b3:
3577 // For a non-type template-parameter of type reference to
3578 // object, no conversions apply. The type referred to by the
3579 // reference may be more cv-qualified than the (otherwise
3580 // identical) type of the template- argument. The
3581 // template-parameter is bound directly to the
3582 // template-argument, which shall be an lvalue.
3583
3584 // FIXME: Other qualifiers?
3585 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3586 unsigned ArgQuals = ArgType.getCVRQualifiers();
3587
3588 if ((ParamQuals | ArgQuals) != ParamQuals) {
3589 S.Diag(Arg->getSourceRange().getBegin(),
3590 diag::err_template_arg_ref_bind_ignores_quals)
3591 << ParamType << Arg->getType()
3592 << Arg->getSourceRange();
3593 S.Diag(Param->getLocation(), diag::note_template_param_here);
3594 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003595 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003596 }
3597 }
3598
3599 // At this point, the template argument refers to an object or
3600 // function with external linkage. We now need to check whether the
3601 // argument and parameter types are compatible.
3602 if (!S.Context.hasSameUnqualifiedType(ArgType,
3603 ParamType.getNonReferenceType())) {
3604 // We can't perform this conversion or binding.
3605 if (ParamType->isReferenceType())
3606 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003607 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003608 else
3609 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003610 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003611 S.Diag(Param->getLocation(), diag::note_template_param_here);
3612 return true;
3613 }
3614 }
3615
3616 // Create the template argument.
3617 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003618 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003619 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003620}
3621
3622/// \brief Checks whether the given template argument is a pointer to
3623/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003624bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003625 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003626 bool Invalid = false;
3627
3628 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003629 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003630 Arg = Cast->getSubExpr();
3631
3632 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003633 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003634 // A template-argument for a non-type, non-template
3635 // template-parameter shall be one of: [...]
3636 //
3637 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003638 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003639
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003640 // In C++98/03 mode, give an extension warning on any extra parentheses.
3641 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3642 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003643 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003644 if (!Invalid && !ExtraParens) {
Mike Stump1eb44332009-09-09 15:08:12 +00003645 Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003646 getLangOptions().CPlusPlus0x ?
3647 diag::warn_cxx98_compat_template_arg_extra_parens :
3648 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003649 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003650 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003651 }
3652
3653 Arg = Parens->getSubExpr();
3654 }
3655
John McCall91a57552011-07-15 05:09:51 +00003656 while (SubstNonTypeTemplateParmExpr *subst =
3657 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3658 Arg = subst->getReplacement()->IgnoreImpCasts();
3659
Douglas Gregorcaddba02009-11-12 18:38:13 +00003660 // A pointer-to-member constant written &Class::member.
3661 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003662 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003663 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3664 if (DRE && !DRE->getQualifier())
3665 DRE = 0;
3666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003667 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003668 // A constant of pointer-to-member type.
3669 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3670 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3671 if (VD->getType()->isMemberPointerType()) {
3672 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003673 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003674 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3675 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003676 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003677 else
3678 Converted = TemplateArgument(VD->getCanonicalDecl());
3679 return Invalid;
3680 }
3681 }
3682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003683
Douglas Gregorcaddba02009-11-12 18:38:13 +00003684 DRE = 0;
3685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003686
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003687 if (!DRE)
3688 return Diag(Arg->getSourceRange().getBegin(),
3689 diag::err_template_arg_not_pointer_to_member_form)
3690 << Arg->getSourceRange();
3691
3692 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3693 assert((isa<FieldDecl>(DRE->getDecl()) ||
3694 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3695 "Only non-static member pointers can make it here");
3696
3697 // Okay: this is the address of a non-static member, and therefore
3698 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003699 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003700 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003701 else
3702 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003703 return Invalid;
3704 }
3705
3706 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003707 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003708 diag::err_template_arg_not_pointer_to_member_form)
3709 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003710 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003711 diag::note_template_arg_refers_here);
3712 return true;
3713}
3714
Douglas Gregorc15cb382009-02-09 23:23:08 +00003715/// \brief Check a template argument against its corresponding
3716/// non-type template parameter.
3717///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003718/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003719/// If an error occurred, it returns ExprError(); otherwise, it
3720/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003721/// InstantiatedParamType is the type of the non-type template
3722/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003723ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3724 QualType InstantiatedParamType, Expr *Arg,
3725 TemplateArgument &Converted,
3726 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003727 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3728
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003729 // If either the parameter has a dependent type or the argument is
3730 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003731 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3732 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003733 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003734 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003735 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003736
3737 // C++ [temp.arg.nontype]p5:
3738 // The following conversions are performed on each expression used
3739 // as a non-type template-argument. If a non-type
3740 // template-argument cannot be converted to the type of the
3741 // corresponding template-parameter then the program is
3742 // ill-formed.
3743 //
3744 // -- for a non-type template-parameter of integral or
3745 // enumeration type, integral promotions (4.5) and integral
3746 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003747 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003748 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith4f870622011-10-27 22:11:44 +00003749 // FIXME: In C++11, the argument is a converted constant expression of the
3750 // type of the template parameter.
3751 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3752 if (ArgResult.isInvalid())
3753 return ExprError();
3754 Arg = ArgResult.take();
3755
3756 QualType ArgType = Arg->getType();
3757
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003758 // C++ [temp.arg.nontype]p1:
3759 // A template-argument for a non-type, non-template
3760 // template-parameter shall be one of:
3761 //
3762 // -- an integral constant-expression of integral or enumeration
3763 // type; or
3764 // -- the name of a non-type template-parameter; or
3765 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003766 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003767 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003768 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003769 diag::err_template_arg_not_integral_or_enumeral)
3770 << ArgType << Arg->getSourceRange();
3771 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003772 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003773 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003774 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003775 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3776 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003777 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003778 }
3779
Douglas Gregor02024a92010-03-28 02:42:43 +00003780 // From here on out, all we care about are the unqualified forms
3781 // of the parameter and argument types.
3782 ParamType = ParamType.getUnqualifiedType();
3783 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003784
3785 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003786 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003787 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003788 } else if (CTAK == CTAK_Deduced) {
3789 // C++ [temp.deduct.type]p17:
3790 // If, in the declaration of a function template with a non-type
3791 // template-parameter, the non-type template- parameter is used
3792 // in an expression in the function parameter-list and, if the
3793 // corresponding template-argument is deduced, the
3794 // template-argument type shall match the type of the
3795 // template-parameter exactly, except that a template-argument
3796 // deduced from an array bound may be of any integral type.
3797 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3798 << ArgType << ParamType;
3799 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003800 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003801 } else if (ParamType->isBooleanType()) {
3802 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003803 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003804 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3805 !ParamType->isEnumeralType()) {
3806 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003807 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003808 } else {
3809 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003810 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003811 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003812 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003813 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003814 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003815 }
3816
Douglas Gregorc7469372011-05-04 21:55:00 +00003817 // Add the value of this argument to the list of converted
3818 // arguments. We use the bitwidth and signedness of the template
3819 // parameter.
3820 if (Arg->isValueDependent()) {
3821 // The argument is value-dependent. Create a new
3822 // TemplateArgument with the converted expression.
3823 Converted = TemplateArgument(Arg);
3824 return Owned(Arg);
3825 }
3826
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003827 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003828 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003829 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003830
Douglas Gregorc7469372011-05-04 21:55:00 +00003831 if (ParamType->isBooleanType()) {
3832 // Value must be zero or one.
3833 Value = Value != 0;
3834 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3835 if (Value.getBitWidth() != AllowedBits)
3836 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003837 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003838 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003839 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003840
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003841 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003842 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003843 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003844 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003845 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003846 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003847
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003848 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003849 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003850 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003851 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3852 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3853 << Arg->getSourceRange();
3854 Diag(Param->getLocation(), diag::note_template_param_here);
3855 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003856
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003857 // Complain if we overflowed the template parameter's type.
3858 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003859 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003860 RequiredBits = OldValue.getActiveBits();
3861 else if (OldValue.isUnsigned())
3862 RequiredBits = OldValue.getActiveBits() + 1;
3863 else
3864 RequiredBits = OldValue.getMinSignedBits();
3865 if (RequiredBits > AllowedBits) {
3866 Diag(Arg->getSourceRange().getBegin(),
3867 diag::warn_template_arg_too_large)
3868 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3869 << Arg->getSourceRange();
3870 Diag(Param->getLocation(), diag::note_template_param_here);
3871 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003872 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003873
John McCall833ca992009-10-29 08:12:44 +00003874 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003875 ParamType->isEnumeralType()
3876 ? Context.getCanonicalType(ParamType)
3877 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003878 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003879 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003880
Richard Smith4f870622011-10-27 22:11:44 +00003881 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00003882 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3883
Douglas Gregorb7a09262010-04-01 18:32:35 +00003884 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3885 // from a template argument of type std::nullptr_t to a non-type
3886 // template parameter of type pointer to object, pointer to
3887 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003888 if (ArgType->isNullPtrType()) {
3889 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3890 Converted = TemplateArgument((NamedDecl *)0);
3891 return Owned(Arg);
3892 }
3893
3894 if (ParamType->isNullPtrType()) {
3895 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3896 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3897 return Owned(Arg);
3898 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003899 }
3900
Douglas Gregorb86b0572009-02-11 01:18:59 +00003901 // Handle pointer-to-function, reference-to-function, and
3902 // pointer-to-member-function all in (roughly) the same way.
3903 if (// -- For a non-type template-parameter of type pointer to
3904 // function, only the function-to-pointer conversion (4.3) is
3905 // applied. If the template-argument represents a set of
3906 // overloaded functions (or a pointer to such), the matching
3907 // function is selected from the set (13.4).
3908 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003909 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003910 // -- For a non-type template-parameter of type reference to
3911 // function, no conversions apply. If the template-argument
3912 // represents a set of overloaded functions, the matching
3913 // function is selected from the set (13.4).
3914 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003915 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003916 // -- For a non-type template-parameter of type pointer to
3917 // member function, no conversions apply. If the
3918 // template-argument represents a set of overloaded member
3919 // functions, the matching member function is selected from
3920 // the set (13.4).
3921 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003922 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003923 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003924
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003925 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003926 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003927 true,
3928 FoundResult)) {
3929 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003930 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003931
3932 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3933 ArgType = Arg->getType();
3934 } else
John Wiegley429bb272011-04-08 18:41:53 +00003935 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003936 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003937
John Wiegley429bb272011-04-08 18:41:53 +00003938 if (!ParamType->isMemberPointerType()) {
3939 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3940 ParamType,
3941 Arg, Converted))
3942 return ExprError();
3943 return Owned(Arg);
3944 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003945
John McCallf85e1932011-06-15 23:02:42 +00003946 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003947 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003948 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00003949 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
3950 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003951 } else if (!Context.hasSameUnqualifiedType(ArgType,
3952 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003953 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003954 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003955 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003956 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003957 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003958 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003959 }
Mike Stump1eb44332009-09-09 15:08:12 +00003960
John Wiegley429bb272011-04-08 18:41:53 +00003961 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3962 return ExprError();
3963 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003964 }
3965
Chris Lattnerfe90de72009-02-20 21:37:53 +00003966 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003967 // -- for a non-type template-parameter of type pointer to
3968 // object, qualification conversions (4.4) and the
3969 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003970 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003971 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003972 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003973
John Wiegley429bb272011-04-08 18:41:53 +00003974 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3975 ParamType,
3976 Arg, Converted))
3977 return ExprError();
3978 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003979 }
Mike Stump1eb44332009-09-09 15:08:12 +00003980
Ted Kremenek6217b802009-07-29 21:53:49 +00003981 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003982 // -- For a non-type template-parameter of type reference to
3983 // object, no conversions apply. The type referred to by the
3984 // reference may be more cv-qualified than the (otherwise
3985 // identical) type of the template-argument. The
3986 // template-parameter is bound directly to the
3987 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003988 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003989 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003990
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003991 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003992 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3993 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003994 true,
3995 FoundResult)) {
3996 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003997 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003998
3999 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4000 ArgType = Arg->getType();
4001 } else
John Wiegley429bb272011-04-08 18:41:53 +00004002 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004003 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004004
John Wiegley429bb272011-04-08 18:41:53 +00004005 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4006 ParamType,
4007 Arg, Converted))
4008 return ExprError();
4009 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004010 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004011
4012 // -- For a non-type template-parameter of type pointer to data
4013 // member, qualification conversions (4.4) are applied.
4014 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4015
John McCallf85e1932011-06-15 23:02:42 +00004016 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004017 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004018 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004019 } else if (IsQualificationConversion(ArgType, ParamType, false,
4020 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004021 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4022 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004023 } else {
4024 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004025 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004026 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004027 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004028 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004029 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004030 }
4031
John Wiegley429bb272011-04-08 18:41:53 +00004032 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4033 return ExprError();
4034 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004035}
4036
4037/// \brief Check a template argument against its corresponding
4038/// template template parameter.
4039///
4040/// This routine implements the semantics of C++ [temp.arg.template].
4041/// It returns true if an error occurred, and false otherwise.
4042bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004043 const TemplateArgumentLoc &Arg) {
4044 TemplateName Name = Arg.getArgument().getAsTemplate();
4045 TemplateDecl *Template = Name.getAsTemplateDecl();
4046 if (!Template) {
4047 // Any dependent template name is fine.
4048 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4049 return false;
4050 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004051
Richard Smith3e4c6c42011-05-05 21:57:07 +00004052 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004053 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004054 // the name of a class template or an alias template, expressed as an
4055 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004056 // primary class templates are considered when matching the
4057 // template template argument with the corresponding parameter;
4058 // partial specializations are not considered even if their
4059 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004060 //
4061 // Note that we also allow template template parameters here, which
4062 // will happen when we are dealing with, e.g., class template
4063 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004064 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004065 !isa<TemplateTemplateParmDecl>(Template) &&
4066 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004067 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004068 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004069 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004070 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004071 << Template;
4072 }
4073
4074 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4075 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004076 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004077 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004078 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004079}
4080
Douglas Gregor02024a92010-03-28 02:42:43 +00004081/// \brief Given a non-type template argument that refers to a
4082/// declaration and the type of its corresponding non-type template
4083/// parameter, produce an expression that properly refers to that
4084/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004085ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004086Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4087 QualType ParamType,
4088 SourceLocation Loc) {
4089 assert(Arg.getKind() == TemplateArgument::Declaration &&
4090 "Only declaration template arguments permitted here");
4091 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4092
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004093 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004094 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4095 // If the value is a class member, we might have a pointer-to-member.
4096 // Determine whether the non-type template template parameter is of
4097 // pointer-to-member type. If so, we need to build an appropriate
4098 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4099 // would refer to the member itself.
4100 if (ParamType->isMemberPointerType()) {
4101 QualType ClassType
4102 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4103 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004104 = NestedNameSpecifier::Create(Context, 0, false,
4105 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004106 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004107 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004108
4109 // The actual value-ness of this is unimportant, but for
4110 // internal consistency's sake, references to instance methods
4111 // are r-values.
4112 ExprValueKind VK = VK_LValue;
4113 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4114 VK = VK_RValue;
4115
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004116 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004117 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004118 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004119 Loc,
4120 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004121 if (RefExpr.isInvalid())
4122 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004123
John McCall2de56d12010-08-25 11:45:40 +00004124 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004125
Douglas Gregorc0c83002010-04-30 21:46:38 +00004126 // We might need to perform a trailing qualification conversion, since
4127 // the element type on the parameter could be more qualified than the
4128 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004129 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004130 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004131 ParamType.getUnqualifiedType(), false,
4132 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004133 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004134
Douglas Gregor02024a92010-03-28 02:42:43 +00004135 assert(!RefExpr.isInvalid() &&
4136 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004137 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004138 return move(RefExpr);
4139 }
4140 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004141
Douglas Gregor02024a92010-03-28 02:42:43 +00004142 QualType T = VD->getType().getNonReferenceType();
4143 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004144 // When the non-type template parameter is a pointer, take the
4145 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004146 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004147 if (RefExpr.isInvalid())
4148 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004149
4150 if (T->isFunctionType() || T->isArrayType()) {
4151 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004152 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4153 if (RefExpr.isInvalid())
4154 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004155
4156 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004157 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004158
Douglas Gregorb7a09262010-04-01 18:32:35 +00004159 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004160 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004161 }
4162
John McCallf89e55a2010-11-18 06:31:45 +00004163 ExprValueKind VK = VK_RValue;
4164
Douglas Gregor02024a92010-03-28 02:42:43 +00004165 // If the non-type template parameter has reference type, qualify the
4166 // resulting declaration reference with the extra qualifiers on the
4167 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004168 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4169 VK = VK_LValue;
4170 T = Context.getQualifiedType(T,
4171 TargetRef->getPointeeType().getQualifiers());
4172 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004173
John McCallf89e55a2010-11-18 06:31:45 +00004174 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004175}
4176
4177/// \brief Construct a new expression that refers to the given
4178/// integral template argument with the given source-location
4179/// information.
4180///
4181/// This routine takes care of the mapping from an integral template
4182/// argument (which may have any integral type) to the appropriate
4183/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004184ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004185Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4186 SourceLocation Loc) {
4187 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004188 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004189 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004190 if (T->isAnyCharacterType()) {
4191 CharacterLiteral::CharacterKind Kind;
4192 if (T->isWideCharType())
4193 Kind = CharacterLiteral::Wide;
4194 else if (T->isChar16Type())
4195 Kind = CharacterLiteral::UTF16;
4196 else if (T->isChar32Type())
4197 Kind = CharacterLiteral::UTF32;
4198 else
4199 Kind = CharacterLiteral::Ascii;
4200
Douglas Gregor02024a92010-03-28 02:42:43 +00004201 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004202 Arg.getAsIntegral()->getZExtValue(),
4203 Kind, T, Loc));
4204 }
4205
Douglas Gregor02024a92010-03-28 02:42:43 +00004206 if (T->isBooleanType())
4207 return Owned(new (Context) CXXBoolLiteralExpr(
4208 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004209 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004210
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004211 if (T->isNullPtrType())
4212 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4213
Chris Lattner223de242011-04-25 20:37:58 +00004214 // If this is an enum type that we're instantiating, we need to use an integer
4215 // type the same size as the enumerator. We don't want to build an
4216 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004217 QualType BT;
4218 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004219 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004220 else
4221 BT = T;
4222
John McCall4e9272d2011-07-15 07:47:58 +00004223 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4224 if (T->isEnumeralType()) {
4225 // FIXME: This is a hack. We need a better way to handle substituted
4226 // non-type template parameters.
4227 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4228 Context.getTrivialTypeSourceInfo(T, Loc),
4229 Loc, Loc);
4230 }
4231
4232 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004233}
4234
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004235/// \brief Match two template parameters within template parameter lists.
4236static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4237 bool Complain,
4238 Sema::TemplateParameterListEqualKind Kind,
4239 SourceLocation TemplateArgLoc) {
4240 // Check the actual kind (type, non-type, template).
4241 if (Old->getKind() != New->getKind()) {
4242 if (Complain) {
4243 unsigned NextDiag = diag::err_template_param_different_kind;
4244 if (TemplateArgLoc.isValid()) {
4245 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4246 NextDiag = diag::note_template_param_different_kind;
4247 }
4248 S.Diag(New->getLocation(), NextDiag)
4249 << (Kind != Sema::TPL_TemplateMatch);
4250 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4251 << (Kind != Sema::TPL_TemplateMatch);
4252 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004253
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004254 return false;
4255 }
4256
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004257 // Check that both are parameter packs are neither are parameter packs.
4258 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004259 // template template parameter, the template template parameter can have
4260 // a parameter pack where the template template argument does not.
4261 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4262 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4263 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004264 if (Complain) {
4265 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4266 if (TemplateArgLoc.isValid()) {
4267 S.Diag(TemplateArgLoc,
4268 diag::err_template_arg_template_params_mismatch);
4269 NextDiag = diag::note_template_parameter_pack_non_pack;
4270 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004271
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004272 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4273 : isa<NonTypeTemplateParmDecl>(New)? 1
4274 : 2;
4275 S.Diag(New->getLocation(), NextDiag)
4276 << ParamKind << New->isParameterPack();
4277 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4278 << ParamKind << Old->isParameterPack();
4279 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004280
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004281 return false;
4282 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004283
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004284 // For non-type template parameters, check the type of the parameter.
4285 if (NonTypeTemplateParmDecl *OldNTTP
4286 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4287 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004288
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004289 // If we are matching a template template argument to a template
4290 // template parameter and one of the non-type template parameter types
4291 // is dependent, then we must wait until template instantiation time
4292 // to actually compare the arguments.
4293 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4294 (OldNTTP->getType()->isDependentType() ||
4295 NewNTTP->getType()->isDependentType()))
4296 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004297
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004298 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4299 if (Complain) {
4300 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4301 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004302 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004303 diag::err_template_arg_template_params_mismatch);
4304 NextDiag = diag::note_template_nontype_parm_different_type;
4305 }
4306 S.Diag(NewNTTP->getLocation(), NextDiag)
4307 << NewNTTP->getType()
4308 << (Kind != Sema::TPL_TemplateMatch);
4309 S.Diag(OldNTTP->getLocation(),
4310 diag::note_template_nontype_parm_prev_declaration)
4311 << OldNTTP->getType();
4312 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004313
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004314 return false;
4315 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004316
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004317 return true;
4318 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004319
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004320 // For template template parameters, check the template parameter types.
4321 // The template parameter lists of template template
4322 // parameters must agree.
4323 if (TemplateTemplateParmDecl *OldTTP
4324 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004325 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004326 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4327 OldTTP->getTemplateParameters(),
4328 Complain,
4329 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004330 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004331 : Kind),
4332 TemplateArgLoc);
4333 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004334
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004335 return true;
4336}
Douglas Gregor02024a92010-03-28 02:42:43 +00004337
Douglas Gregora0347822011-01-13 00:08:50 +00004338/// \brief Diagnose a known arity mismatch when comparing template argument
4339/// lists.
4340static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004341void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004342 TemplateParameterList *New,
4343 TemplateParameterList *Old,
4344 Sema::TemplateParameterListEqualKind Kind,
4345 SourceLocation TemplateArgLoc) {
4346 unsigned NextDiag = diag::err_template_param_list_different_arity;
4347 if (TemplateArgLoc.isValid()) {
4348 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4349 NextDiag = diag::note_template_param_list_different_arity;
4350 }
4351 S.Diag(New->getTemplateLoc(), NextDiag)
4352 << (New->size() > Old->size())
4353 << (Kind != Sema::TPL_TemplateMatch)
4354 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4355 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4356 << (Kind != Sema::TPL_TemplateMatch)
4357 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4358}
4359
Douglas Gregorddc29e12009-02-06 22:42:48 +00004360/// \brief Determine whether the given template parameter lists are
4361/// equivalent.
4362///
Mike Stump1eb44332009-09-09 15:08:12 +00004363/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004364/// source code as part of a new template declaration.
4365///
4366/// \param Old The old template parameter list, typically found via
4367/// name lookup of the template declared with this template parameter
4368/// list.
4369///
4370/// \param Complain If true, this routine will produce a diagnostic if
4371/// the template parameter lists are not equivalent.
4372///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004373/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004374///
4375/// \param TemplateArgLoc If this source location is valid, then we
4376/// are actually checking the template parameter list of a template
4377/// argument (New) against the template parameter list of its
4378/// corresponding template template parameter (Old). We produce
4379/// slightly different diagnostics in this scenario.
4380///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004381/// \returns True if the template parameter lists are equal, false
4382/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004383bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004384Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4385 TemplateParameterList *Old,
4386 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004387 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004388 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004389 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4390 if (Complain)
4391 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4392 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004393
4394 return false;
4395 }
4396
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004397 // C++0x [temp.arg.template]p3:
4398 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004399 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004400 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004401 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004402 // template-parameter-list of P. [...]
4403 TemplateParameterList::iterator NewParm = New->begin();
4404 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004405 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004406 OldParmEnd = Old->end();
4407 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004408 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4409 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004410 if (NewParm == NewParmEnd) {
4411 if (Complain)
4412 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4413 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004414
Douglas Gregora0347822011-01-13 00:08:50 +00004415 return false;
4416 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004417
Douglas Gregora0347822011-01-13 00:08:50 +00004418 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4419 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004420 return false;
4421
Douglas Gregora0347822011-01-13 00:08:50 +00004422 ++NewParm;
4423 continue;
4424 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004425
Douglas Gregora0347822011-01-13 00:08:50 +00004426 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004427 // [...] When P's template- parameter-list contains a template parameter
4428 // pack (14.5.3), the template parameter pack will match zero or more
4429 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004430 // template-parameter-list of A with the same type and form as the
4431 // template parameter pack in P (ignoring whether those template
4432 // parameters are template parameter packs).
4433 for (; NewParm != NewParmEnd; ++NewParm) {
4434 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4435 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004436 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004437 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004438 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004439
Douglas Gregora0347822011-01-13 00:08:50 +00004440 // Make sure we exhausted all of the arguments.
4441 if (NewParm != NewParmEnd) {
4442 if (Complain)
4443 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4444 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004445
Douglas Gregora0347822011-01-13 00:08:50 +00004446 return false;
4447 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004448
Douglas Gregorddc29e12009-02-06 22:42:48 +00004449 return true;
4450}
4451
4452/// \brief Check whether a template can be declared within this scope.
4453///
4454/// If the template declaration is valid in this scope, returns
4455/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004456bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004457Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004458 if (!S)
4459 return false;
4460
Douglas Gregorddc29e12009-02-06 22:42:48 +00004461 // Find the nearest enclosing declaration scope.
4462 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4463 (S->getFlags() & Scope::TemplateParamScope) != 0)
4464 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004465
Douglas Gregorddc29e12009-02-06 22:42:48 +00004466 // C++ [temp]p2:
4467 // A template-declaration can appear only as a namespace scope or
4468 // class scope declaration.
4469 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004470 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4471 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004472 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004473 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004474
Eli Friedman1503f772009-07-31 01:43:05 +00004475 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004476 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004477
4478 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4479 return false;
4480
Mike Stump1eb44332009-09-09 15:08:12 +00004481 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004482 diag::err_template_outside_namespace_or_class_scope)
4483 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004484}
Douglas Gregorcc636682009-02-17 23:15:12 +00004485
Douglas Gregord5cb8762009-10-07 00:13:32 +00004486/// \brief Determine what kind of template specialization the given declaration
4487/// is.
4488static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4489 if (!D)
4490 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004491
Douglas Gregorf6b11852009-10-08 15:14:33 +00004492 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4493 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004494 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4495 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004496 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4497 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004498
Douglas Gregord5cb8762009-10-07 00:13:32 +00004499 return TSK_Undeclared;
4500}
4501
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004502/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004503/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004504///
Douglas Gregor9302da62009-10-14 23:50:59 +00004505/// This routine determines whether a template specialization can be declared
4506/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004507///
4508/// \param S the semantic analysis object for which this check is being
4509/// performed.
4510///
4511/// \param Specialized the entity being specialized or instantiated, which
4512/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004513/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004514/// member class).
4515///
4516/// \param PrevDecl the previous declaration of this entity, if any.
4517///
4518/// \param Loc the location of the explicit specialization or instantiation of
4519/// this entity.
4520///
4521/// \param IsPartialSpecialization whether this is a partial specialization of
4522/// a class template.
4523///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004524/// \returns true if there was an error that we cannot recover from, false
4525/// otherwise.
4526static bool CheckTemplateSpecializationScope(Sema &S,
4527 NamedDecl *Specialized,
4528 NamedDecl *PrevDecl,
4529 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004530 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004531 // Keep these "kind" numbers in sync with the %select statements in the
4532 // various diagnostics emitted by this routine.
4533 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004534 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004535 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004536 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004537 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004538 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004539 EntityKind = 3;
4540 else if (isa<VarDecl>(Specialized))
4541 EntityKind = 4;
4542 else if (isa<RecordDecl>(Specialized))
4543 EntityKind = 5;
4544 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004545 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4546 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004547 return true;
4548 }
4549
Douglas Gregor88b70942009-02-25 22:02:03 +00004550 // C++ [temp.expl.spec]p2:
4551 // An explicit specialization shall be declared in the namespace
4552 // of which the template is a member, or, for member templates, in
4553 // the namespace of which the enclosing class or enclosing class
4554 // template is a member. An explicit specialization of a member
4555 // function, member class or static data member of a class
4556 // template shall be declared in the namespace of which the class
4557 // template is a member. Such a declaration may also be a
4558 // definition. If the declaration is not a definition, the
4559 // specialization may be defined later in the name- space in which
4560 // the explicit specialization was declared, or in a namespace
4561 // that encloses the one in which the explicit specialization was
4562 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004563 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004564 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004565 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004566 return true;
4567 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004568
Douglas Gregor0a407472009-10-07 17:30:37 +00004569 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004570 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004571 // Do not warn for class scope explicit specialization during
4572 // instantiation, warning was already emitted during pattern
4573 // semantic analysis.
4574 if (!S.ActiveTemplateInstantiations.size())
4575 S.Diag(Loc, diag::ext_function_specialization_in_class)
4576 << Specialized;
4577 } else {
4578 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4579 << Specialized;
4580 return true;
4581 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004582 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004583
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004584 if (S.CurContext->isRecord() &&
4585 !S.CurContext->Equals(Specialized->getDeclContext())) {
4586 // Make sure that we're specializing in the right record context.
4587 // Otherwise, things can go horribly wrong.
4588 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4589 << Specialized;
4590 return true;
4591 }
4592
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004593 // C++ [temp.class.spec]p6:
4594 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004595 // in any namespace scope in which its definition may be defined (14.5.1
4596 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004597 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004598 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004599 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004600 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004601 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004602 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4603 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004604 // C++ [temp.exp.spec]p2:
4605 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004606 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004607 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004608 // An explicit specialization of a member function, member class or
4609 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004610 // namespace of which the class template is a member.
4611 //
4612 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004613 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004614 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004615 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4616 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4617 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4618 assert(!IsCPlusPlus0xExtension &&
4619 "DC encloses TU but isn't in enclosing namespace set");
4620 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004621 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004622 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4623 int Diag;
4624 if (!IsCPlusPlus0xExtension)
4625 Diag = diag::err_template_spec_decl_out_of_scope;
4626 else if (!S.getLangOptions().CPlusPlus0x)
4627 Diag = diag::ext_template_spec_decl_out_of_scope;
4628 else
4629 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4630 S.Diag(Loc, Diag)
4631 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4632 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004633
Douglas Gregor9302da62009-10-14 23:50:59 +00004634 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004635 ComplainedAboutScope =
4636 !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004637 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004638 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004639
4640 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004641 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004642 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004643 // specializations of function templates, static data members, and member
4644 // functions, so we skip the check here for those kinds of entities.
4645 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004646 // Should we refactor that check, so that it occurs later?
4647 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004648 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4649 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004650 if (isa<TranslationUnitDecl>(SpecializedContext))
4651 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4652 << EntityKind << Specialized;
4653 else if (isa<NamespaceDecl>(SpecializedContext))
4654 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4655 << EntityKind << Specialized
4656 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004657
Douglas Gregor9302da62009-10-14 23:50:59 +00004658 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004659 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004660
Douglas Gregord5cb8762009-10-07 00:13:32 +00004661 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004662
Douglas Gregor88b70942009-02-25 22:02:03 +00004663 return false;
4664}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004665
Douglas Gregorbacb9492011-01-03 21:13:47 +00004666/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4667/// that checks non-type template partial specialization arguments.
4668static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4669 NonTypeTemplateParmDecl *Param,
4670 const TemplateArgument *Args,
4671 unsigned NumArgs) {
4672 for (unsigned I = 0; I != NumArgs; ++I) {
4673 if (Args[I].getKind() == TemplateArgument::Pack) {
4674 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004675 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004676 Args[I].pack_size()))
4677 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004678
Douglas Gregore94866f2009-06-12 21:21:02 +00004679 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004680 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004681
Douglas Gregorbacb9492011-01-03 21:13:47 +00004682 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004683 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004684 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004685 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004686
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004687 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004688 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4689 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004690
4691 // Strip off any implicit casts we added as part of type checking.
4692 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4693 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004694
Douglas Gregore94866f2009-06-12 21:21:02 +00004695 // C++ [temp.class.spec]p8:
4696 // A non-type argument is non-specialized if it is the name of a
4697 // non-type parameter. All other non-type arguments are
4698 // specialized.
4699 //
4700 // Below, we check the two conditions that only apply to
4701 // specialized non-type arguments, so skip any non-specialized
4702 // arguments.
4703 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004704 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004705 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004706
Douglas Gregore94866f2009-06-12 21:21:02 +00004707 // C++ [temp.class.spec]p9:
4708 // Within the argument list of a class template partial
4709 // specialization, the following restrictions apply:
4710 // -- A partially specialized non-type argument expression
4711 // shall not involve a template parameter of the partial
4712 // specialization except when the argument expression is a
4713 // simple identifier.
4714 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004715 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004716 diag::err_dependent_non_type_arg_in_partial_spec)
4717 << ArgExpr->getSourceRange();
4718 return true;
4719 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004720
Douglas Gregore94866f2009-06-12 21:21:02 +00004721 // -- The type of a template parameter corresponding to a
4722 // specialized non-type argument shall not be dependent on a
4723 // parameter of the specialization.
4724 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004725 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004726 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4727 << Param->getType()
4728 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004729 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004730 return true;
4731 }
4732 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004733
Douglas Gregorbacb9492011-01-03 21:13:47 +00004734 return false;
4735}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004736
Douglas Gregorbacb9492011-01-03 21:13:47 +00004737/// \brief Check the non-type template arguments of a class template
4738/// partial specialization according to C++ [temp.class.spec]p9.
4739///
4740/// \param TemplateParams the template parameters of the primary class
4741/// template.
4742///
4743/// \param TemplateArg the template arguments of the class template
4744/// partial specialization.
4745///
4746/// \returns true if there was an error, false otherwise.
4747static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4748 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004749 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004750 const TemplateArgument *ArgList = TemplateArgs.data();
4751
4752 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4753 NonTypeTemplateParmDecl *Param
4754 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4755 if (!Param)
4756 continue;
4757
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004758 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004759 &ArgList[I], 1))
4760 return true;
4761 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004762
4763 return false;
4764}
4765
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004766/// \brief Retrieve the previous declaration of the given declaration.
4767static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4768 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4769 return VD->getPreviousDeclaration();
4770 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4771 return FD->getPreviousDeclaration();
4772 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4773 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004774 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004775 return TD->getPreviousDeclaration();
4776 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4777 return FTD->getPreviousDeclaration();
4778 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4779 return CTD->getPreviousDeclaration();
4780 return 0;
4781}
4782
John McCalld226f652010-08-21 09:40:31 +00004783DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004784Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4785 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004786 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004787 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004788 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004789 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004790 SourceLocation TemplateNameLoc,
4791 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004792 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004793 SourceLocation RAngleLoc,
4794 AttributeList *Attr,
4795 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004796 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004797
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004798 // NOTE: KWLoc is the location of the tag keyword. This will instead
4799 // store the location of the outermost template keyword in the declaration.
4800 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4801 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4802
Douglas Gregorcc636682009-02-17 23:15:12 +00004803 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004804 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004805 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004806 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4807
4808 if (!ClassTemplate) {
4809 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004810 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004811 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4812 return true;
4813 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004814
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004815 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004816 bool isPartialSpecialization = false;
4817
Douglas Gregor88b70942009-02-25 22:02:03 +00004818 // Check the validity of the template headers that introduce this
4819 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004820 // FIXME: We probably shouldn't complain about these headers for
4821 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004822 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004823 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004824 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4825 TemplateNameLoc,
4826 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004827 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004828 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004829 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004830 isExplicitSpecialization,
4831 Invalid);
4832 if (Invalid)
4833 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004834
Douglas Gregor05396e22009-08-25 17:23:04 +00004835 if (TemplateParams && TemplateParams->size() > 0) {
4836 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004837
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004838 if (TUK == TUK_Friend) {
4839 Diag(KWLoc, diag::err_partial_specialization_friend)
4840 << SourceRange(LAngleLoc, RAngleLoc);
4841 return true;
4842 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004843
Douglas Gregor05396e22009-08-25 17:23:04 +00004844 // C++ [temp.class.spec]p10:
4845 // The template parameter list of a specialization shall not
4846 // contain default template argument values.
4847 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4848 Decl *Param = TemplateParams->getParam(I);
4849 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4850 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004851 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004852 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004853 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004854 }
4855 } else if (NonTypeTemplateParmDecl *NTTP
4856 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4857 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004858 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004859 diag::err_default_arg_in_partial_spec)
4860 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004861 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004862 }
4863 } else {
4864 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004865 if (TTP->hasDefaultArgument()) {
4866 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004867 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004868 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004869 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004870 }
4871 }
4872 }
Douglas Gregora735b202009-10-13 14:39:41 +00004873 } else if (TemplateParams) {
4874 if (TUK == TUK_Friend)
4875 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004876 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004877 SourceRange(TemplateParams->getTemplateLoc(),
4878 TemplateParams->getRAngleLoc()))
4879 << SourceRange(LAngleLoc, RAngleLoc);
4880 else
4881 isExplicitSpecialization = true;
4882 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004883 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004884 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004885 isExplicitSpecialization = true;
4886 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004887
Douglas Gregorcc636682009-02-17 23:15:12 +00004888 // Check that the specialization uses the same tag kind as the
4889 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004890 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4891 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004892 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004893 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004894 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004895 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004896 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004897 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004898 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004899 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004900 diag::note_previous_use);
4901 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4902 }
4903
Douglas Gregor40808ce2009-03-09 23:48:35 +00004904 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004905 TemplateArgumentListInfo TemplateArgs;
4906 TemplateArgs.setLAngleLoc(LAngleLoc);
4907 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004908 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004909
Douglas Gregor925910d2011-01-03 20:35:03 +00004910 // Check for unexpanded parameter packs in any of the template arguments.
4911 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004912 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004913 UPPC_PartialSpecialization))
4914 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004915
Douglas Gregorcc636682009-02-17 23:15:12 +00004916 // Check that the template argument list is well-formed for this
4917 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004918 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004919 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4920 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004921 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004922
Douglas Gregor910f8002010-11-07 23:05:16 +00004923 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004924 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004925
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004926 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004927 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004928 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004929 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004930 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004931 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004932 return true;
4933
Douglas Gregor561f8122011-07-01 01:22:09 +00004934 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004935 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004936 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004937 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004938 TemplateArgs.size(),
4939 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004940 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4941 << ClassTemplate->getDeclName();
4942 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004943 }
4944 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004945
Douglas Gregorcc636682009-02-17 23:15:12 +00004946 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004947 ClassTemplateSpecializationDecl *PrevDecl = 0;
4948
4949 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004950 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004951 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004952 = ClassTemplate->findPartialSpecialization(Converted.data(),
4953 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004954 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004955 else
4956 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004957 = ClassTemplate->findSpecialization(Converted.data(),
4958 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004959
4960 ClassTemplateSpecializationDecl *Specialization = 0;
4961
Douglas Gregor88b70942009-02-25 22:02:03 +00004962 // Check whether we can declare a class template specialization in
4963 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004964 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004965 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4966 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004967 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004968 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004969
Douglas Gregorb88e8882009-07-30 17:40:51 +00004970 // The canonical type
4971 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004972 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004973 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004974 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004975 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004976 // arguments was referenced but not declared, or we're only
4977 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004978 // declaration node as our own, updating its source location and
4979 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004980 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004981 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004982 if (TemplateParameterLists.size() > 0) {
4983 Specialization->setTemplateParameterListsInfo(Context,
4984 TemplateParameterLists.size(),
4985 (TemplateParameterList**) TemplateParameterLists.release());
4986 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004987 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004988 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004989 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004990 // Build the canonical type that describes the converted template
4991 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004992 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4993 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004994 Converted.data(),
4995 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004996
4997 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004998 ClassTemplate->getInjectedClassNameSpecialization())) {
4999 // C++ [temp.class.spec]p9b3:
5000 //
5001 // -- The argument list of the specialization shall not be identical
5002 // to the implicit argument list of the primary template.
5003 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00005004 << (TUK == TUK_Definition)
5005 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00005006 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5007 ClassTemplate->getIdentifier(),
5008 TemplateNameLoc,
5009 Attr,
5010 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005011 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005012 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005013 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005014 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005015
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005016 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005017 ClassTemplatePartialSpecializationDecl *PrevPartial
5018 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005019 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005020 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005021 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005022 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005023 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005024 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005025 TemplateParams,
5026 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005027 Converted.data(),
5028 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005029 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005030 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005031 PrevPartial,
5032 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005033 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005034 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005035 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005036 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005037 (TemplateParameterList**) TemplateParameterLists.release());
5038 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005039
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005040 if (!PrevPartial)
5041 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005042 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005043
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005044 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005045 // template specialization, make a note of that.
5046 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5047 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005048
Douglas Gregor031a5882009-06-13 00:26:55 +00005049 // Check that all of the template parameters of the class template
5050 // partial specialization are deducible from the template
5051 // arguments. If not, this class template partial specialization
5052 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005053 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00005054 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005055 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005056 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005057 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005058 unsigned NumNonDeducible = 0;
5059 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
5060 if (!DeducibleParams[I])
5061 ++NumNonDeducible;
5062
5063 if (NumNonDeducible) {
5064 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5065 << (NumNonDeducible > 1)
5066 << SourceRange(TemplateNameLoc, RAngleLoc);
5067 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5068 if (!DeducibleParams[I]) {
5069 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5070 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005071 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005072 diag::note_partial_spec_unused_parameter)
5073 << Param->getDeclName();
5074 else
Mike Stump1eb44332009-09-09 15:08:12 +00005075 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005076 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005077 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005078 }
5079 }
5080 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005081 } else {
5082 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005083 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005084 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005085 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005086 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005087 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005088 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005089 Converted.data(),
5090 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005091 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005092 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005093 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005094 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005095 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005096 (TemplateParameterList**) TemplateParameterLists.release());
5097 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005098
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005099 if (!PrevDecl)
5100 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005101
5102 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005103 }
5104
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005105 // C++ [temp.expl.spec]p6:
5106 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005107 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005108 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005109 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005110 // use occurs; no diagnostic is required.
5111 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005112 bool Okay = false;
5113 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5114 // Is there any previous explicit specialization declaration?
5115 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5116 Okay = true;
5117 break;
5118 }
5119 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005120
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005121 if (!Okay) {
5122 SourceRange Range(TemplateNameLoc, RAngleLoc);
5123 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5124 << Context.getTypeDeclType(Specialization) << Range;
5125
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005126 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005127 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005128 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005129 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005130 return true;
5131 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005132 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005133
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005134 // If this is not a friend, note that this is an explicit specialization.
5135 if (TUK != TUK_Friend)
5136 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005137
5138 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005139 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005140 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005141 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005142 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005143 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005144 Diag(Def->getLocation(), diag::note_previous_definition);
5145 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005146 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005147 }
5148 }
5149
John McCall7f1b9872010-12-18 03:30:47 +00005150 if (Attr)
5151 ProcessDeclAttributeList(S, Specialization, Attr);
5152
Douglas Gregord023aec2011-09-09 20:53:38 +00005153 if (ModulePrivateLoc.isValid())
5154 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5155 << (isPartialSpecialization? 1 : 0)
5156 << FixItHint::CreateRemoval(ModulePrivateLoc);
5157
Douglas Gregorfc705b82009-02-26 22:19:44 +00005158 // Build the fully-sugared type for this class template
5159 // specialization as the user wrote in the specialization
5160 // itself. This means that we'll pretty-print the type retrieved
5161 // from the specialization's declaration the way that the user
5162 // actually wrote the specialization, rather than formatting the
5163 // name based on the "canonical" representation used to store the
5164 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005165 TypeSourceInfo *WrittenTy
5166 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5167 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005168 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005169 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005170 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005171 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005172 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005173
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005174 // C++ [temp.expl.spec]p9:
5175 // A template explicit specialization is in the scope of the
5176 // namespace in which the template was defined.
5177 //
5178 // We actually implement this paragraph where we set the semantic
5179 // context (in the creation of the ClassTemplateSpecializationDecl),
5180 // but we also maintain the lexical context where the actual
5181 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005182 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005183
Douglas Gregorcc636682009-02-17 23:15:12 +00005184 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005185 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005186 Specialization->startDefinition();
5187
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005188 if (TUK == TUK_Friend) {
5189 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5190 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005191 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005192 /*FIXME:*/KWLoc);
5193 Friend->setAccess(AS_public);
5194 CurContext->addDecl(Friend);
5195 } else {
5196 // Add the specialization into its lexical context, so that it can
5197 // be seen when iterating through the list of declarations in that
5198 // context. However, specializations are not found by name lookup.
5199 CurContext->addDecl(Specialization);
5200 }
John McCalld226f652010-08-21 09:40:31 +00005201 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005202}
Douglas Gregord57959a2009-03-27 23:10:48 +00005203
John McCalld226f652010-08-21 09:40:31 +00005204Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005205 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005206 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005207 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005208}
5209
John McCalld226f652010-08-21 09:40:31 +00005210Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005211 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005212 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005213 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005214 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005215
Douglas Gregor52591bf2009-06-24 00:54:41 +00005216 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005217 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005218 }
Mike Stump1eb44332009-09-09 15:08:12 +00005219
Douglas Gregor52591bf2009-06-24 00:54:41 +00005220 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005221
Douglas Gregor45fa5602011-11-07 20:56:01 +00005222 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005223 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005224 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005225 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005226 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005227 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005228 FunctionTemplate->getTemplatedDecl());
5229 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5230 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5231 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005232}
5233
John McCall75042392010-02-11 01:33:53 +00005234/// \brief Strips various properties off an implicit instantiation
5235/// that has just been explicitly specialized.
5236static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005237 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005238
5239 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5240 FD->setInlineSpecified(false);
5241 }
5242}
5243
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005244/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005245/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005246/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005247/// new specialization/instantiation will have any effect.
5248///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005249/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005250/// instantiation.
5251///
5252/// \param NewTSK the kind of the new explicit specialization or instantiation.
5253///
5254/// \param PrevDecl the previous declaration of the entity.
5255///
5256/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5257///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005258/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005259/// declaration was instantiated (either implicitly or explicitly).
5260///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005261/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005262/// specialization or instantiation has no effect and should be ignored.
5263///
5264/// \returns true if there was an error that should prevent the introduction of
5265/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005266bool
5267Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5268 TemplateSpecializationKind NewTSK,
5269 NamedDecl *PrevDecl,
5270 TemplateSpecializationKind PrevTSK,
5271 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005272 bool &HasNoEffect) {
5273 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005274
Douglas Gregor454885e2009-10-15 15:54:05 +00005275 switch (NewTSK) {
5276 case TSK_Undeclared:
5277 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005278 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005279
Douglas Gregor454885e2009-10-15 15:54:05 +00005280 case TSK_ExplicitSpecialization:
5281 switch (PrevTSK) {
5282 case TSK_Undeclared:
5283 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005284 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005285 // explicitly specialized or has merely been mentioned without any
5286 // instantiation.
5287 return false;
5288
5289 case TSK_ImplicitInstantiation:
5290 if (PrevPointOfInstantiation.isInvalid()) {
5291 // The declaration itself has not actually been instantiated, so it is
5292 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005293 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005294 return false;
5295 }
5296 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005297
Douglas Gregor454885e2009-10-15 15:54:05 +00005298 case TSK_ExplicitInstantiationDeclaration:
5299 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005300 assert((PrevTSK == TSK_ImplicitInstantiation ||
5301 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005302 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005303
Douglas Gregor454885e2009-10-15 15:54:05 +00005304 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005305 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005306 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005307 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005308 // implicit instantiation to take place, in every translation unit in
5309 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005310 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5311 // Is there any previous explicit specialization declaration?
5312 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5313 return false;
5314 }
5315
Douglas Gregor0d035142009-10-27 18:42:08 +00005316 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005317 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005318 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005319 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005320
Douglas Gregor454885e2009-10-15 15:54:05 +00005321 return true;
5322 }
5323 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005324
Douglas Gregor454885e2009-10-15 15:54:05 +00005325 case TSK_ExplicitInstantiationDeclaration:
5326 switch (PrevTSK) {
5327 case TSK_ExplicitInstantiationDeclaration:
5328 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005329 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005330 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005331
Douglas Gregor454885e2009-10-15 15:54:05 +00005332 case TSK_Undeclared:
5333 case TSK_ImplicitInstantiation:
5334 // We're explicitly instantiating something that may have already been
5335 // implicitly instantiated; that's fine.
5336 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005337
Douglas Gregor454885e2009-10-15 15:54:05 +00005338 case TSK_ExplicitSpecialization:
5339 // C++0x [temp.explicit]p4:
5340 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005341 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005342 // specialization for that template, the explicit instantiation has no
5343 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005344 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005345 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005346
Douglas Gregor454885e2009-10-15 15:54:05 +00005347 case TSK_ExplicitInstantiationDefinition:
5348 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005349 // If an entity is the subject of both an explicit instantiation
5350 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005351 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005352 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005353 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005354 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005355 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005356 assert(PrevPointOfInstantiation.isValid() &&
5357 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005358 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005359 return false;
5360 }
5361 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005362
Douglas Gregor454885e2009-10-15 15:54:05 +00005363 case TSK_ExplicitInstantiationDefinition:
5364 switch (PrevTSK) {
5365 case TSK_Undeclared:
5366 case TSK_ImplicitInstantiation:
5367 // We're explicitly instantiating something that may have already been
5368 // implicitly instantiated; that's fine.
5369 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005370
Douglas Gregor454885e2009-10-15 15:54:05 +00005371 case TSK_ExplicitSpecialization:
5372 // C++ DR 259, C++0x [temp.explicit]p4:
5373 // For a given set of template parameters, if an explicit
5374 // instantiation of a template appears after a declaration of
5375 // an explicit specialization for that template, the explicit
5376 // instantiation has no effect.
5377 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005378 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005379 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005380 // has been explicitly specialized.
Richard Smithebaf0e62011-10-18 20:49:44 +00005381 Diag(NewLoc, getLangOptions().CPlusPlus0x ?
5382 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5383 diag::ext_explicit_instantiation_after_specialization)
5384 << PrevDecl;
5385 Diag(PrevDecl->getLocation(),
5386 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005387 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005388 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005389
Douglas Gregor454885e2009-10-15 15:54:05 +00005390 case TSK_ExplicitInstantiationDeclaration:
5391 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005392 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005393 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005394
Douglas Gregor454885e2009-10-15 15:54:05 +00005395 case TSK_ExplicitInstantiationDefinition:
5396 // C++0x [temp.spec]p5:
5397 // For a given template and a given set of template-arguments,
5398 // - an explicit instantiation definition shall appear at most once
5399 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005400 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005401 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005402 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005403 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005404 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005405 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005406 }
5407 break;
5408 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005409
David Blaikieb219cfc2011-09-23 05:06:16 +00005410 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005411}
5412
John McCallaf2094e2010-04-08 09:05:18 +00005413/// \brief Perform semantic analysis for the given dependent function
5414/// template specialization. The only possible way to get a dependent
5415/// function template specialization is with a friend declaration,
5416/// like so:
5417///
5418/// template <class T> void foo(T);
5419/// template <class T> class A {
5420/// friend void foo<>(T);
5421/// };
5422///
5423/// There really isn't any useful analysis we can do here, so we
5424/// just store the information.
5425bool
5426Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5427 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5428 LookupResult &Previous) {
5429 // Remove anything from Previous that isn't a function template in
5430 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005431 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005432 LookupResult::Filter F = Previous.makeFilter();
5433 while (F.hasNext()) {
5434 NamedDecl *D = F.next()->getUnderlyingDecl();
5435 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005436 !FDLookupContext->InEnclosingNamespaceSetOf(
5437 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005438 F.erase();
5439 }
5440 F.done();
5441
5442 // Should this be diagnosed here?
5443 if (Previous.empty()) return true;
5444
5445 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5446 ExplicitTemplateArgs);
5447 return false;
5448}
5449
Abramo Bagnarae03db982010-05-20 15:32:11 +00005450/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005451/// specialization.
5452///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005453/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005454/// explicit function template specialization. On successful completion,
5455/// the function declaration \p FD will become a function template
5456/// specialization.
5457///
5458/// \param FD the function declaration, which will be updated to become a
5459/// function template specialization.
5460///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005461/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5462/// if any. Note that this may be valid info even when 0 arguments are
5463/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5464/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005465///
Francois Pichet59e7c562011-07-08 06:21:47 +00005466/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005467/// this function specialization.
5468bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005469Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005470 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005471 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005472 // The set of function template specializations that could match this
5473 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005474 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005475
Sebastian Redl7a126a42010-08-31 00:36:30 +00005476 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005477 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5478 I != E; ++I) {
5479 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5480 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005481 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005482 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005483 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5484 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005485 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005486
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005487 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005488 // A trailing template-argument can be left unspecified in the
5489 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005490 // provided it can be deduced from the function argument type.
5491 // Perform template argument deduction to determine whether we may be
5492 // specializing this template.
5493 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005494 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005495 FunctionDecl *Specialization = 0;
5496 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005497 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005498 FD->getType(),
5499 Specialization,
5500 Info)) {
5501 // FIXME: Template argument deduction failed; record why it failed, so
5502 // that we can provide nifty diagnostics.
5503 (void)TDK;
5504 continue;
5505 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005506
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005507 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005508 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005509 }
5510 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005511
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005512 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005513 UnresolvedSetIterator Result
5514 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005515 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005516 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005517 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005518 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005519 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005520 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005521 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005522 return true;
John McCallc373d482010-01-27 01:50:18 +00005523
5524 // Ignore access information; it doesn't figure into redeclaration checking.
5525 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005526
5527 FunctionTemplateSpecializationInfo *SpecInfo
5528 = Specialization->getTemplateSpecializationInfo();
5529 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005530
5531 // Note: do not overwrite location info if previous template
5532 // specialization kind was explicit.
5533 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5534 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5535 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005536
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005537 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005538 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005539
5540 // If this is a friend declaration, then we're not really declaring
5541 // an explicit specialization.
5542 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005543
Douglas Gregord5cb8762009-10-07 00:13:32 +00005544 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005545 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005546 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005547 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005548 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005549 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005550 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005551
5552 // C++ [temp.expl.spec]p6:
5553 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005554 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005555 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005556 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005557 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005558 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005559 if (!isFriend &&
5560 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005561 TSK_ExplicitSpecialization,
5562 Specialization,
5563 SpecInfo->getTemplateSpecializationKind(),
5564 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005565 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005566 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005567
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005568 // Mark the prior declaration as an explicit specialization, so that later
5569 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005570 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005571 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005572 MarkUnusedFileScopedDecl(Specialization);
5573 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005574
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005575 // Turn the given function declaration into a function template
5576 // specialization, with the template arguments from the previous
5577 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005578 // Take copies of (semantic and syntactic) template argument lists.
5579 const TemplateArgumentList* TemplArgs = new (Context)
5580 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005581 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005582 TemplArgs, /*InsertPos=*/0,
5583 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005584 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005585 FD->setStorageClass(Specialization->getStorageClass());
5586
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005587 // The "previous declaration" for this function template specialization is
5588 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005589 Previous.clear();
5590 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005591 return false;
5592}
5593
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005594/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005595/// specialization.
5596///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005597/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005598/// explicit member function specialization. On successful completion,
5599/// the function declaration \p FD will become a member function
5600/// specialization.
5601///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005602/// \param Member the member declaration, which will be updated to become a
5603/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005604///
John McCall68263142009-11-18 22:49:29 +00005605/// \param Previous the set of declarations, one of which may be specialized
5606/// by this function specialization; the set will be modified to contain the
5607/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005608bool
John McCall68263142009-11-18 22:49:29 +00005609Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005610 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005611
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005612 // Try to find the member we are instantiating.
5613 NamedDecl *Instantiation = 0;
5614 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005615 MemberSpecializationInfo *MSInfo = 0;
5616
John McCall68263142009-11-18 22:49:29 +00005617 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005618 // Nowhere to look anyway.
5619 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005620 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5621 I != E; ++I) {
5622 NamedDecl *D = (*I)->getUnderlyingDecl();
5623 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005624 if (Context.hasSameType(Function->getType(), Method->getType())) {
5625 Instantiation = Method;
5626 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005627 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005628 break;
5629 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005630 }
5631 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005632 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005633 VarDecl *PrevVar;
5634 if (Previous.isSingleResult() &&
5635 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005636 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005637 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005638 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005639 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005640 }
5641 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005642 CXXRecordDecl *PrevRecord;
5643 if (Previous.isSingleResult() &&
5644 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5645 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005646 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005647 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005648 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005649 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005650
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005651 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005652 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005653 // specializations are always out-of-line, the caller will complain about
5654 // this mismatch later.
5655 return false;
5656 }
John McCall77e8b112010-04-13 20:37:33 +00005657
5658 // If this is a friend, just bail out here before we start turning
5659 // things into explicit specializations.
5660 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5661 // Preserve instantiation information.
5662 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5663 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5664 cast<CXXMethodDecl>(InstantiatedFrom),
5665 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5666 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5667 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5668 cast<CXXRecordDecl>(InstantiatedFrom),
5669 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5670 }
5671
5672 Previous.clear();
5673 Previous.addDecl(Instantiation);
5674 return false;
5675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005676
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005677 // Make sure that this is a specialization of a member.
5678 if (!InstantiatedFrom) {
5679 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5680 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005681 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5682 return true;
5683 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005684
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005685 // C++ [temp.expl.spec]p6:
5686 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005687 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005688 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005689 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005690 // use occurs; no diagnostic is required.
5691 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005692
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005693 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005694 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5695 TSK_ExplicitSpecialization,
5696 Instantiation,
5697 MSInfo->getTemplateSpecializationKind(),
5698 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005699 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005700 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005701
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005702 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005703 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005704 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005705 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005706 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005707 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005708
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005709 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005710 // the original declaration to note that it is an explicit specialization
5711 // (if it was previously an implicit instantiation). This latter step
5712 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005713 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005714 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5715 if (InstantiationFunction->getTemplateSpecializationKind() ==
5716 TSK_ImplicitInstantiation) {
5717 InstantiationFunction->setTemplateSpecializationKind(
5718 TSK_ExplicitSpecialization);
5719 InstantiationFunction->setLocation(Member->getLocation());
5720 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005721
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005722 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5723 cast<CXXMethodDecl>(InstantiatedFrom),
5724 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005725 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005726 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005727 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5728 if (InstantiationVar->getTemplateSpecializationKind() ==
5729 TSK_ImplicitInstantiation) {
5730 InstantiationVar->setTemplateSpecializationKind(
5731 TSK_ExplicitSpecialization);
5732 InstantiationVar->setLocation(Member->getLocation());
5733 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005734
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005735 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5736 cast<VarDecl>(InstantiatedFrom),
5737 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005738 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005739 } else {
5740 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005741 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5742 if (InstantiationClass->getTemplateSpecializationKind() ==
5743 TSK_ImplicitInstantiation) {
5744 InstantiationClass->setTemplateSpecializationKind(
5745 TSK_ExplicitSpecialization);
5746 InstantiationClass->setLocation(Member->getLocation());
5747 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005748
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005749 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005750 cast<CXXRecordDecl>(InstantiatedFrom),
5751 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005752 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005753
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005754 // Save the caller the trouble of having to figure out which declaration
5755 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005756 Previous.clear();
5757 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005758 return false;
5759}
5760
Douglas Gregor558c0322009-10-14 23:41:34 +00005761/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005762///
5763/// \returns true if a serious error occurs, false otherwise.
5764static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005765 SourceLocation InstLoc,
5766 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005767 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5768 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005769
Douglas Gregor669eed82010-07-13 00:10:04 +00005770 if (CurContext->isRecord()) {
5771 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5772 << D;
5773 return true;
5774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005775
Richard Smith3e2e91e2011-10-18 02:28:33 +00005776 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005777 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005778 // template. If the name declared in the explicit instantiation is an
5779 // unqualified name, the explicit instantiation shall appear in the
5780 // namespace where its template is declared or, if that namespace is inline
5781 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005782 //
5783 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005784 if (WasQualifiedName) {
5785 if (CurContext->Encloses(OrigContext))
5786 return false;
5787 } else {
5788 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5789 return false;
5790 }
5791
5792 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5793 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005794 S.Diag(InstLoc,
5795 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005796 diag::err_explicit_instantiation_out_of_scope :
5797 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005798 << D << NS;
5799 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005800 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005801 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005802 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5803 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5804 << D << NS;
5805 } else
5806 S.Diag(InstLoc,
5807 S.getLangOptions().CPlusPlus0x?
5808 diag::err_explicit_instantiation_must_be_global :
5809 diag::warn_explicit_instantiation_must_be_global_0x)
5810 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005811 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005812 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005813}
5814
5815/// \brief Determine whether the given scope specifier has a template-id in it.
5816static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5817 if (!SS.isSet())
5818 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005819
Richard Smith3e2e91e2011-10-18 02:28:33 +00005820 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005821 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005822 // or a static data member of a class template specialization, the name of
5823 // the class template specialization in the qualified-id for the member
5824 // name shall be a simple-template-id.
5825 //
5826 // C++98 has the same restriction, just worded differently.
5827 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5828 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005829 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005830 if (isa<TemplateSpecializationType>(T))
5831 return true;
5832
5833 return false;
5834}
5835
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005836// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005837DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005838Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005839 SourceLocation ExternLoc,
5840 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005841 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005842 SourceLocation KWLoc,
5843 const CXXScopeSpec &SS,
5844 TemplateTy TemplateD,
5845 SourceLocation TemplateNameLoc,
5846 SourceLocation LAngleLoc,
5847 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005848 SourceLocation RAngleLoc,
5849 AttributeList *Attr) {
5850 // Find the class template we're specializing
5851 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005852 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005853 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5854
5855 // Check that the specialization uses the same tag kind as the
5856 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005857 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5858 assert(Kind != TTK_Enum &&
5859 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005860 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005861 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005862 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005863 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005864 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005865 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005866 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005867 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005868 diag::note_previous_use);
5869 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5870 }
5871
Douglas Gregor558c0322009-10-14 23:41:34 +00005872 // C++0x [temp.explicit]p2:
5873 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005874 // definition and an explicit instantiation declaration. An explicit
5875 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005876 TemplateSpecializationKind TSK
5877 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5878 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005879
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005880 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005881 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005882 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005883
5884 // Check that the template argument list is well-formed for this
5885 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005886 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005887 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5888 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005889 return true;
5890
Douglas Gregor910f8002010-11-07 23:05:16 +00005891 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005892 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005893
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005894 // Find the class template specialization declaration that
5895 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005896 void *InsertPos = 0;
5897 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005898 = ClassTemplate->findSpecialization(Converted.data(),
5899 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005900
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005901 TemplateSpecializationKind PrevDecl_TSK
5902 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5903
Douglas Gregord5cb8762009-10-07 00:13:32 +00005904 // C++0x [temp.explicit]p2:
5905 // [...] An explicit instantiation shall appear in an enclosing
5906 // namespace of its template. [...]
5907 //
5908 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005909 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5910 SS.isSet()))
5911 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005912
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005913 ClassTemplateSpecializationDecl *Specialization = 0;
5914
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005915 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005916 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005917 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005918 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005919 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005920 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005921 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005922
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005923 // Even though HasNoEffect == true means that this explicit instantiation
5924 // has no effect on semantics, we go on to put its syntax in the AST.
5925
5926 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5927 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005928 // Since the only prior class template specialization with these
5929 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005930 // declaration node as our own, updating the source location
5931 // for the template name to reflect our new declaration.
5932 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005933 Specialization = PrevDecl;
5934 Specialization->setLocation(TemplateNameLoc);
5935 PrevDecl = 0;
5936 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005937 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005938
Douglas Gregor52604ab2009-09-11 21:19:12 +00005939 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005940 // Create a new class template specialization declaration node for
5941 // this explicit specialization.
5942 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005943 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005944 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005945 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005946 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005947 Converted.data(),
5948 Converted.size(),
5949 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005950 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005951
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005952 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005953 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005954 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005955 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005956 }
5957
5958 // Build the fully-sugared type for this explicit instantiation as
5959 // the user wrote in the explicit instantiation itself. This means
5960 // that we'll pretty-print the type retrieved from the
5961 // specialization's declaration the way that the user actually wrote
5962 // the explicit instantiation, rather than formatting the name based
5963 // on the "canonical" representation used to store the template
5964 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005965 TypeSourceInfo *WrittenTy
5966 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5967 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005968 Context.getTypeDeclType(Specialization));
5969 Specialization->setTypeAsWritten(WrittenTy);
5970 TemplateArgsIn.release();
5971
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005972 // Set source locations for keywords.
5973 Specialization->setExternLoc(ExternLoc);
5974 Specialization->setTemplateKeywordLoc(TemplateLoc);
5975
5976 // Add the explicit instantiation into its lexical context. However,
5977 // since explicit instantiations are never found by name lookup, we
5978 // just put it into the declaration context directly.
5979 Specialization->setLexicalDeclContext(CurContext);
5980 CurContext->addDecl(Specialization);
5981
5982 // Syntax is now OK, so return if it has no other effect on semantics.
5983 if (HasNoEffect) {
5984 // Set the template specialization kind.
5985 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005986 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005987 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005988
5989 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005990 // A definition of a class template or class member template
5991 // shall be in scope at the point of the explicit instantiation of
5992 // the class template or class member template.
5993 //
5994 // This check comes when we actually try to perform the
5995 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005996 ClassTemplateSpecializationDecl *Def
5997 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005998 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005999 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006000 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006001 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006002 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006003 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6004 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006005
Douglas Gregor0d035142009-10-27 18:42:08 +00006006 // Instantiate the members of this class template specialization.
6007 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006008 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006009 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006010 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6011
6012 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6013 // TSK_ExplicitInstantiationDefinition
6014 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6015 TSK == TSK_ExplicitInstantiationDefinition)
6016 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006017
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006018 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006019 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006020
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006021 // Set the template specialization kind.
6022 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006023 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006024}
6025
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006026// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006027DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006028Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006029 SourceLocation ExternLoc,
6030 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006031 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006032 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006033 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006034 IdentifierInfo *Name,
6035 SourceLocation NameLoc,
6036 AttributeList *Attr) {
6037
Douglas Gregor402abb52009-05-28 23:31:59 +00006038 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006039 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006040 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006041 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006042 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006043 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00006044 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006045 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006046 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6047
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006048 if (!TagD)
6049 return true;
6050
John McCalld226f652010-08-21 09:40:31 +00006051 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006052 if (Tag->isEnum()) {
6053 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6054 << Context.getTypeDeclType(Tag);
6055 return true;
6056 }
6057
Douglas Gregord0c87372009-05-27 17:30:49 +00006058 if (Tag->isInvalidDecl())
6059 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006060
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006061 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6062 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6063 if (!Pattern) {
6064 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6065 << Context.getTypeDeclType(Record);
6066 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6067 return true;
6068 }
6069
Douglas Gregor558c0322009-10-14 23:41:34 +00006070 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006071 // If the explicit instantiation is for a class or member class, the
6072 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006073 // simple-template-id.
6074 //
6075 // C++98 has the same restriction, just worded differently.
6076 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006077 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006078 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006079
Douglas Gregor558c0322009-10-14 23:41:34 +00006080 // C++0x [temp.explicit]p2:
6081 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006082 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006083 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006084 TemplateSpecializationKind TSK
6085 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6086 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006087
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006088 // C++0x [temp.explicit]p2:
6089 // [...] An explicit instantiation shall appear in an enclosing
6090 // namespace of its template. [...]
6091 //
6092 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006093 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006094
Douglas Gregor454885e2009-10-15 15:54:05 +00006095 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006096 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00006097 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00006098 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006099 PrevDecl = Record;
6100 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006101 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006102 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006103 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006104 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006105 PrevDecl,
6106 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006107 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006108 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006109 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006110 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006111 return TagD;
6112 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006113
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006114 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006115 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006116 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006117 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006118 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006119 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006120 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006121 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006122 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006123 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6124 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006125 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6126 << Pattern;
6127 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006128 } else {
6129 if (InstantiateClass(NameLoc, Record, Def,
6130 getTemplateInstantiationArgs(Record),
6131 TSK))
6132 return true;
6133
Douglas Gregor952b0172010-02-11 01:04:33 +00006134 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006135 if (!RecordDef)
6136 return true;
6137 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006138 }
6139
Douglas Gregor0d035142009-10-27 18:42:08 +00006140 // Instantiate all of the members of the class.
6141 InstantiateClassMembers(NameLoc, RecordDef,
6142 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006143
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006144 if (TSK == TSK_ExplicitInstantiationDefinition)
6145 MarkVTableUsed(NameLoc, RecordDef, true);
6146
Mike Stump390b4cc2009-05-16 07:39:55 +00006147 // FIXME: We don't have any representation for explicit instantiations of
6148 // member classes. Such a representation is not needed for compilation, but it
6149 // should be available for clients that want to see all of the declarations in
6150 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006151 return TagD;
6152}
6153
John McCallf312b1e2010-08-26 23:41:50 +00006154DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6155 SourceLocation ExternLoc,
6156 SourceLocation TemplateLoc,
6157 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006158 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006159 // TODO: check if/when DNInfo should replace Name.
6160 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6161 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006162 if (!Name) {
6163 if (!D.isInvalidType())
6164 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6165 diag::err_explicit_instantiation_requires_name)
6166 << D.getDeclSpec().getSourceRange()
6167 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006168
Douglas Gregord5a423b2009-09-25 18:43:00 +00006169 return true;
6170 }
6171
6172 // The scope passed in may not be a decl scope. Zip up the scope tree until
6173 // we find one that is.
6174 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6175 (S->getFlags() & Scope::TemplateParamScope) != 0)
6176 S = S->getParent();
6177
6178 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006179 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6180 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006181 if (R.isNull())
6182 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006183
Douglas Gregore885e182011-05-21 18:53:30 +00006184 // C++ [dcl.stc]p1:
6185 // A storage-class-specifier shall not be specified in [...] an explicit
6186 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006187 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006188 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6189 << Name;
6190 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006191 } else if (D.getDeclSpec().getStorageClassSpec()
6192 != DeclSpec::SCS_unspecified) {
6193 // Complain about then remove the storage class specifier.
6194 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6195 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6196
6197 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006198 }
6199
Douglas Gregor663b5a02009-10-14 20:14:33 +00006200 // C++0x [temp.explicit]p1:
6201 // [...] An explicit instantiation of a function template shall not use the
6202 // inline or constexpr specifiers.
6203 // Presumably, this also applies to member functions of class templates as
6204 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006205 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006206 Diag(D.getDeclSpec().getInlineSpecLoc(),
Richard Smith2dc7ece2011-10-18 03:44:03 +00006207 getLangOptions().CPlusPlus0x ?
6208 diag::err_explicit_instantiation_inline :
6209 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006210 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6211 if (D.getDeclSpec().isConstexprSpecified())
6212 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6213 // not already specified.
6214 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6215 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006216
Douglas Gregor558c0322009-10-14 23:41:34 +00006217 // C++0x [temp.explicit]p2:
6218 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006219 // definition and an explicit instantiation declaration. An explicit
6220 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006221 TemplateSpecializationKind TSK
6222 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6223 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006224
Abramo Bagnara25777432010-08-11 22:01:17 +00006225 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006226 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006227
6228 if (!R->isFunctionType()) {
6229 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006230 // A [...] static data member of a class template can be explicitly
6231 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006232 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006233 if (Previous.isAmbiguous())
6234 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006235
John McCall1bcee0a2009-12-02 08:25:40 +00006236 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006237 if (!Prev || !Prev->isStaticDataMember()) {
6238 // We expect to see a data data member here.
6239 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6240 << Name;
6241 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6242 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006243 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006244 return true;
6245 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006246
Douglas Gregord5a423b2009-09-25 18:43:00 +00006247 if (!Prev->getInstantiatedFromStaticDataMember()) {
6248 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006249 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006250 diag::err_explicit_instantiation_data_member_not_instantiated)
6251 << Prev;
6252 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6253 // FIXME: Can we provide a note showing where this was declared?
6254 return true;
6255 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006256
Douglas Gregor558c0322009-10-14 23:41:34 +00006257 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006258 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006259 // or a static data member of a class template specialization, the name of
6260 // the class template specialization in the qualified-id for the member
6261 // name shall be a simple-template-id.
6262 //
6263 // C++98 has the same restriction, just worded differently.
6264 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006265 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006266 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006267 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006268
Douglas Gregor558c0322009-10-14 23:41:34 +00006269 // Check the scope of this explicit instantiation.
6270 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006271
Douglas Gregor454885e2009-10-15 15:54:05 +00006272 // Verify that it is okay to explicitly instantiate here.
6273 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6274 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006275 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006276 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006277 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006278 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006279 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006280 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006281 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006282 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006283
Douglas Gregord5a423b2009-09-25 18:43:00 +00006284 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006285 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006286 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006287 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006288
Douglas Gregord5a423b2009-09-25 18:43:00 +00006289 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006290 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006291 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006292
6293 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006294 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006295 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006296 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006297 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6298 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006299 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6300 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006301 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6302 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006303 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006304 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006305 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006306 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006307 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006308
Douglas Gregord5a423b2009-09-25 18:43:00 +00006309 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006310 // A [...] function [...] can be explicitly instantiated from its template.
6311 // A member function [...] of a class template can be explicitly
6312 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006313 // template.
John McCallc373d482010-01-27 01:50:18 +00006314 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006315 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6316 P != PEnd; ++P) {
6317 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006318 if (!HasExplicitTemplateArgs) {
6319 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6320 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6321 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006322
John McCallc373d482010-01-27 01:50:18 +00006323 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006324 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6325 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006326 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006327 }
6328 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006329
Douglas Gregord5a423b2009-09-25 18:43:00 +00006330 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6331 if (!FunTmpl)
6332 continue;
6333
John McCall5769d612010-02-08 23:07:23 +00006334 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006335 FunctionDecl *Specialization = 0;
6336 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006337 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006338 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006339 R, Specialization, Info)) {
6340 // FIXME: Keep track of almost-matches?
6341 (void)TDK;
6342 continue;
6343 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006344
John McCallc373d482010-01-27 01:50:18 +00006345 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006346 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006347
Douglas Gregord5a423b2009-09-25 18:43:00 +00006348 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006349 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006350 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006351 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006352 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6353 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6354 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006355
John McCallc373d482010-01-27 01:50:18 +00006356 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006357 return true;
John McCallc373d482010-01-27 01:50:18 +00006358
6359 // Ignore access control bits, we don't need them for redeclaration checking.
6360 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006361
Douglas Gregor0a897e32009-10-15 17:21:20 +00006362 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006363 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006364 diag::err_explicit_instantiation_member_function_not_instantiated)
6365 << Specialization
6366 << (Specialization->getTemplateSpecializationKind() ==
6367 TSK_ExplicitSpecialization);
6368 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6369 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006370 }
6371
Douglas Gregor0a897e32009-10-15 17:21:20 +00006372 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006373 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6374 PrevDecl = Specialization;
6375
Douglas Gregor0a897e32009-10-15 17:21:20 +00006376 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006377 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006378 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006379 PrevDecl,
6380 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006381 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006382 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006383 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006384
Douglas Gregor0a897e32009-10-15 17:21:20 +00006385 // FIXME: We may still want to build some representation of this
6386 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006387 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006388 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006389 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006390
6391 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006392
Douglas Gregor0a897e32009-10-15 17:21:20 +00006393 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006394 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006395
Douglas Gregor558c0322009-10-14 23:41:34 +00006396 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006397 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006398 // or a static data member of a class template specialization, the name of
6399 // the class template specialization in the qualified-id for the member
6400 // name shall be a simple-template-id.
6401 //
6402 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006403 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006404 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006405 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006406 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006407 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006408 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006409 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006410
Douglas Gregor558c0322009-10-14 23:41:34 +00006411 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006412 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006413 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006414 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006415 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006416
Douglas Gregord5a423b2009-09-25 18:43:00 +00006417 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006418 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006419}
6420
John McCallf312b1e2010-08-26 23:41:50 +00006421TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006422Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6423 const CXXScopeSpec &SS, IdentifierInfo *Name,
6424 SourceLocation TagLoc, SourceLocation NameLoc) {
6425 // This has to hold, because SS is expected to be defined.
6426 assert(Name && "Expected a name in a dependent tag");
6427
6428 NestedNameSpecifier *NNS
6429 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6430 if (!NNS)
6431 return true;
6432
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006433 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006434
Douglas Gregor48c89f42010-04-24 16:38:41 +00006435 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6436 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006437 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006438 return true;
6439 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006440
Douglas Gregor059101f2011-03-02 00:47:37 +00006441 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006442 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006443 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6444
6445 // Create type-source location information for this type.
6446 TypeLocBuilder TLB;
6447 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6448 TL.setKeywordLoc(TagLoc);
6449 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6450 TL.setNameLoc(NameLoc);
6451 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006452}
6453
John McCallf312b1e2010-08-26 23:41:50 +00006454TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006455Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6456 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006457 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006458 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006459 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006460
Richard Smithebaf0e62011-10-18 20:49:44 +00006461 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6462 Diag(TypenameLoc,
6463 getLangOptions().CPlusPlus0x ?
6464 diag::warn_cxx98_compat_typename_outside_of_template :
6465 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006466 << FixItHint::CreateRemoval(TypenameLoc);
6467
Douglas Gregor2494dd02011-03-01 01:34:45 +00006468 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006469 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6470 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006471 if (T.isNull())
6472 return true;
John McCall63b43852010-04-29 23:50:39 +00006473
6474 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6475 if (isa<DependentNameType>(T)) {
6476 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006477 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006478 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006479 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006480 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006481 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006482 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006483 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006484 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006485 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006486
John McCallb3d87482010-08-24 05:47:05 +00006487 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006488}
6489
John McCallf312b1e2010-08-26 23:41:50 +00006490TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006491Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6492 const CXXScopeSpec &SS,
6493 SourceLocation TemplateLoc,
6494 TemplateTy TemplateIn,
6495 SourceLocation TemplateNameLoc,
6496 SourceLocation LAngleLoc,
6497 ASTTemplateArgsPtr TemplateArgsIn,
6498 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006499 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6500 Diag(TypenameLoc,
6501 getLangOptions().CPlusPlus0x ?
6502 diag::warn_cxx98_compat_typename_outside_of_template :
6503 diag::ext_typename_outside_of_template)
6504 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006505
6506 // Translate the parser's template argument list in our AST format.
6507 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6508 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6509
6510 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006511 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6512 // Construct a dependent template specialization type.
6513 assert(DTN && "dependent template has non-dependent name?");
6514 assert(DTN->getQualifier()
6515 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6516 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6517 DTN->getQualifier(),
6518 DTN->getIdentifier(),
6519 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006520
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006521 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006522 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006523 DependentTemplateSpecializationTypeLoc SpecTL
6524 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006525 SpecTL.setLAngleLoc(LAngleLoc);
6526 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006527 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006528 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006529 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006530 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6531 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006532 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006533 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006534
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006535 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6536 if (T.isNull())
6537 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006538
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006539 // Provide source-location information for the template specialization
6540 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006541 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006542 TemplateSpecializationTypeLoc SpecTL
6543 = Builder.push<TemplateSpecializationTypeLoc>(T);
6544
6545 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006546 SpecTL.setLAngleLoc(LAngleLoc);
6547 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006548 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006549 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6550 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6551
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006552 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6553 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6554 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006555 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6556
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006557 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6558 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006559}
6560
Douglas Gregora02411e2011-02-27 22:46:49 +00006561
Douglas Gregord57959a2009-03-27 23:10:48 +00006562/// \brief Build the type that describes a C++ typename specifier,
6563/// e.g., "typename T::type".
6564QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006565Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6566 SourceLocation KeywordLoc,
6567 NestedNameSpecifierLoc QualifierLoc,
6568 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006569 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006570 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006571 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006572
John McCall77bb1aa2010-05-01 00:40:08 +00006573 DeclContext *Ctx = computeDeclContext(SS);
6574 if (!Ctx) {
6575 // If the nested-name-specifier is dependent and couldn't be
6576 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006577 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6578 return Context.getDependentNameType(Keyword,
6579 QualifierLoc.getNestedNameSpecifier(),
6580 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006581 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006582
John McCall77bb1aa2010-05-01 00:40:08 +00006583 // If the nested-name-specifier refers to the current instantiation,
6584 // the "typename" keyword itself is superfluous. In C++03, the
6585 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6586 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006587 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006588
John McCall77bb1aa2010-05-01 00:40:08 +00006589 if (RequireCompleteDeclContext(SS, Ctx))
6590 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006591
6592 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006593 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006594 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006595 unsigned DiagID = 0;
6596 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006597 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006598 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006599 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006600 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006601
6602 case LookupResult::FoundUnresolvedValue: {
6603 // We found a using declaration that is a value. Most likely, the using
6604 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006605 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006606 IILoc);
6607 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6608 << Name << Ctx << FullRange;
6609 if (UnresolvedUsingValueDecl *Using
6610 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006611 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006612 Diag(Loc, diag::note_using_value_decl_missing_typename)
6613 << FixItHint::CreateInsertion(Loc, "typename ");
6614 }
6615 }
6616 // Fall through to create a dependent typename type, from which we can recover
6617 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006618
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006619 case LookupResult::NotFoundInCurrentInstantiation:
6620 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006621 return Context.getDependentNameType(Keyword,
6622 QualifierLoc.getNestedNameSpecifier(),
6623 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006624
6625 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006626 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006627 // We found a type. Build an ElaboratedType, since the
6628 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006629 return Context.getElaboratedType(ETK_Typename,
6630 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006631 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006632 }
6633
6634 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006635 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006636 break;
6637
6638 case LookupResult::FoundOverloaded:
6639 DiagID = diag::err_typename_nested_not_type;
6640 Referenced = *Result.begin();
6641 break;
6642
John McCall6e247262009-10-10 05:48:19 +00006643 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006644 return QualType();
6645 }
6646
6647 // If we get here, it's because name lookup did not find a
6648 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006649 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006650 IILoc);
6651 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006652 if (Referenced)
6653 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6654 << Name;
6655 return QualType();
6656}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006657
6658namespace {
6659 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006660 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006661 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006662 SourceLocation Loc;
6663 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006664
Douglas Gregor4a959d82009-08-06 16:20:37 +00006665 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006666 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006667
Mike Stump1eb44332009-09-09 15:08:12 +00006668 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006669 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006670 DeclarationName Entity)
6671 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006672 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006673
6674 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006675 /// transformed.
6676 ///
6677 /// For the purposes of type reconstruction, a type has already been
6678 /// transformed if it is NULL or if it is not dependent.
6679 bool AlreadyTransformed(QualType T) {
6680 return T.isNull() || !T->isDependentType();
6681 }
Mike Stump1eb44332009-09-09 15:08:12 +00006682
6683 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006684 /// rebuilt.
6685 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006686
Douglas Gregor4a959d82009-08-06 16:20:37 +00006687 /// \brief Returns the name of the entity whose type is being rebuilt.
6688 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006689
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006690 /// \brief Sets the "base" location and entity when that
6691 /// information is known based on another transformation.
6692 void setBase(SourceLocation Loc, DeclarationName Entity) {
6693 this->Loc = Loc;
6694 this->Entity = Entity;
6695 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006696 };
6697}
6698
Douglas Gregor4a959d82009-08-06 16:20:37 +00006699/// \brief Rebuilds a type within the context of the current instantiation.
6700///
Mike Stump1eb44332009-09-09 15:08:12 +00006701/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006702/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006703/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006704/// partial specialization thereof). This routine will rebuild that type now
6705/// that we have entered the declarator's scope, which may produce different
6706/// canonical types, e.g.,
6707///
6708/// \code
6709/// template<typename T>
6710/// struct X {
6711/// typedef T* pointer;
6712/// pointer data();
6713/// };
6714///
6715/// template<typename T>
6716/// typename X<T>::pointer X<T>::data() { ... }
6717/// \endcode
6718///
Douglas Gregor4714c122010-03-31 17:34:00 +00006719/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006720/// since we do not know that we can look into X<T> when we parsed the type.
6721/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006722/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006723/// as the canonical type of T*, allowing the return types of the out-of-line
6724/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006725TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6726 SourceLocation Loc,
6727 DeclarationName Name) {
6728 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006729 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006730
Douglas Gregor4a959d82009-08-06 16:20:37 +00006731 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6732 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006733}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006734
John McCall60d7b3a2010-08-24 06:29:42 +00006735ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006736 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6737 DeclarationName());
6738 return Rebuilder.TransformExpr(E);
6739}
6740
John McCall63b43852010-04-29 23:50:39 +00006741bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006742 if (SS.isInvalid())
6743 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006744
Douglas Gregor7e384942011-02-25 16:07:42 +00006745 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006746 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6747 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006748 NestedNameSpecifierLoc Rebuilt
6749 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6750 if (!Rebuilt)
6751 return true;
John McCall63b43852010-04-29 23:50:39 +00006752
Douglas Gregor7e384942011-02-25 16:07:42 +00006753 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006754 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006755}
6756
Douglas Gregor20606502011-10-14 15:31:12 +00006757/// \brief Rebuild the template parameters now that we know we're in a current
6758/// instantiation.
6759bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6760 TemplateParameterList *Params) {
6761 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6762 Decl *Param = Params->getParam(I);
6763
6764 // There is nothing to rebuild in a type parameter.
6765 if (isa<TemplateTypeParmDecl>(Param))
6766 continue;
6767
6768 // Rebuild the template parameter list of a template template parameter.
6769 if (TemplateTemplateParmDecl *TTP
6770 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6771 if (RebuildTemplateParamsInCurrentInstantiation(
6772 TTP->getTemplateParameters()))
6773 return true;
6774
6775 continue;
6776 }
6777
6778 // Rebuild the type of a non-type template parameter.
6779 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6780 TypeSourceInfo *NewTSI
6781 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6782 NTTP->getLocation(),
6783 NTTP->getDeclName());
6784 if (!NewTSI)
6785 return true;
6786
6787 if (NewTSI != NTTP->getTypeSourceInfo()) {
6788 NTTP->setTypeSourceInfo(NewTSI);
6789 NTTP->setType(NewTSI->getType());
6790 }
6791 }
6792
6793 return false;
6794}
6795
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006796/// \brief Produces a formatted string that describes the binding of
6797/// template parameters to template arguments.
6798std::string
6799Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6800 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006801 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006802}
6803
6804std::string
6805Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6806 const TemplateArgument *Args,
6807 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006808 llvm::SmallString<128> Str;
6809 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006810
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006811 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006812 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006813
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006814 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006815 if (I >= NumArgs)
6816 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006817
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006818 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006819 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006820 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006821 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006822
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006823 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006824 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006825 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006826 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006827 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006828
Douglas Gregor87dd6972010-12-20 16:52:59 +00006829 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006830 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006831 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006832
6833 Out << ']';
6834 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006835}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006836
6837void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6838 if (!FD)
6839 return;
6840 FD->setLateTemplateParsed(Flag);
6841}
6842
6843bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6844 DeclContext *DC = CurContext;
6845
6846 while (DC) {
6847 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6848 const FunctionDecl *FD = RD->isLocalClass();
6849 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6850 } else if (DC->isTranslationUnit() || DC->isNamespace())
6851 return false;
6852
6853 DC = DC->getParent();
6854 }
6855 return false;
6856}