blob: de0193c61d417b7f179909c6bc5d8b28a6e787f4 [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
Douglas Gregor312eadb2011-04-24 05:37:28 +000079void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
Douglas Gregor312eadb2011-04-24 05:37:28 +000085 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
Richard Smith3e4c6c42011-05-05 21:57:07 +000095 // is used as a template-name, the reference refers to the class
96 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +000098 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
99 if (!ClassTemplates.insert(ClassTmpl)) {
100 filter.erase();
101 continue;
102 }
John McCall8ba66912010-08-13 07:02:08 +0000103
104 // FIXME: we promote access to public here as a workaround to
105 // the fact that LookupResult doesn't let us remember that we
106 // found this template through a particular injected class name,
107 // which means we end up doing nasty things to the invariants.
108 // Pretending that access is public is *much* safer.
109 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000110 }
John McCallf7a1a742009-11-24 19:00:30 +0000111 }
112 filter.done();
113}
114
Douglas Gregor312eadb2011-04-24 05:37:28 +0000115bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
116 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
117 if (isAcceptableTemplateName(Context, *I))
118 return true;
119
Douglas Gregor3b887352011-04-27 04:48:22 +0000120 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000121}
122
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000124 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000125 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000126 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000127 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000128 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000129 TemplateTy &TemplateResult,
130 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000131 assert(getLangOptions().CPlusPlus && "No template names in C!");
132
Douglas Gregor014e88d2009-11-03 23:16:33 +0000133 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000134 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000135
Douglas Gregor014e88d2009-11-03 23:16:33 +0000136 switch (Name.getKind()) {
137 case UnqualifiedId::IK_Identifier:
138 TName = DeclarationName(Name.Identifier);
139 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000140
Douglas Gregor014e88d2009-11-03 23:16:33 +0000141 case UnqualifiedId::IK_OperatorFunctionId:
142 TName = Context.DeclarationNames.getCXXOperatorName(
143 Name.OperatorFunctionId.Operator);
144 break;
145
Sean Hunte6252d12009-11-28 08:58:14 +0000146 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000147 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
148 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000149
Douglas Gregor014e88d2009-11-03 23:16:33 +0000150 default:
151 return TNK_Non_template;
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
John McCallb3d87482010-08-24 05:47:05 +0000154 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000156 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000157 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000158 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
159 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000160 if (R.empty()) return TNK_Non_template;
161 if (R.isAmbiguous()) {
162 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000163 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000164
165 // FIXME: we might have ambiguous templates, in which case we
166 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000167 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000168 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000169
John McCall0bd6feb2009-12-02 08:04:21 +0000170 TemplateName Template;
171 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
John McCall0bd6feb2009-12-02 08:04:21 +0000173 unsigned ResultCount = R.end() - R.begin();
174 if (ResultCount > 1) {
175 // We assume that we'll preserve the qualifier from a function
176 // template name in other ways.
177 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
178 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000179
180 // We'll do this lookup again later.
181 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000182 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000183 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
184
185 if (SS.isSet() && !SS.isInvalid()) {
186 NestedNameSpecifier *Qualifier
187 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000188 Template = Context.getQualifiedTemplateName(Qualifier,
189 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000190 } else {
191 Template = TemplateName(TD);
192 }
193
John McCallb8592062010-08-13 02:23:42 +0000194 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000195 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000196
197 // We'll do this lookup again later.
198 R.suppressDiagnostics();
199 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000200 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
201 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000202 TemplateKind = TNK_Type_template;
203 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
John McCall0bd6feb2009-12-02 08:04:21 +0000206 TemplateResult = TemplateTy::make(Template);
207 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000208}
209
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000210bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000211 SourceLocation IILoc,
212 Scope *S,
213 const CXXScopeSpec *SS,
214 TemplateTy &SuggestedTemplate,
215 TemplateNameKind &SuggestedKind) {
216 // We can't recover unless there's a dependent scope specifier preceding the
217 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000218 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000219 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
220 computeDeclContext(*SS))
221 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 // The code is missing a 'template' keyword prior to the dependent template
224 // name.
225 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
226 Diag(IILoc, diag::err_template_kw_missing)
227 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000228 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000229 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000230 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
231 SuggestedKind = TNK_Dependent_template_name;
232 return true;
233}
234
John McCallf7a1a742009-11-24 19:00:30 +0000235void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000236 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000237 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000238 bool EnteringContext,
239 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000240 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000241 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000242 DeclContext *LookupCtx = 0;
243 bool isDependent = false;
244 if (!ObjectType.isNull()) {
245 // This nested-name-specifier occurs in a member access expression, e.g.,
246 // x->B::f, and we are looking into the type of the object.
247 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
248 LookupCtx = computeDeclContext(ObjectType);
249 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000251 "Caller should have completed object type");
252 } else if (SS.isSet()) {
253 // This nested-name-specifier occurs after another nested-name-specifier,
254 // so long into the context associated with the prior nested-name-specifier.
255 LookupCtx = computeDeclContext(SS, EnteringContext);
256 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000257
John McCallf7a1a742009-11-24 19:00:30 +0000258 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000259 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000260 return;
261 }
262
263 bool ObjectTypeSearchedInScope = false;
264 if (LookupCtx) {
265 // Perform "qualified" name lookup into the declaration context we
266 // computed, which is either the type of the base of a member access
267 // expression or the declaration context associated with a prior
268 // nested-name-specifier.
269 LookupQualifiedName(Found, LookupCtx);
270
271 if (!ObjectType.isNull() && Found.empty()) {
272 // C++ [basic.lookup.classref]p1:
273 // In a class member access expression (5.2.5), if the . or -> token is
274 // immediately followed by an identifier followed by a <, the
275 // identifier must be looked up to determine whether the < is the
276 // beginning of a template argument list (14.2) or a less-than operator.
277 // The identifier is first looked up in the class of the object
278 // expression. If the identifier is not found, it is then looked up in
279 // the context of the entire postfix-expression and shall name a class
280 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000281 if (S) LookupName(Found, S);
282 ObjectTypeSearchedInScope = true;
283 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000284 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000285 // We cannot look into a dependent object type or nested nme
286 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000287 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000288 return;
289 } else {
290 // Perform unqualified name lookup in the current scope.
291 LookupName(Found, S);
292 }
293
Douglas Gregor2e933882010-01-12 17:06:20 +0000294 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000295 // If we did not find any names, attempt to correct any typos.
296 DeclarationName Name = Found.getLookupName();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000297 Found.clear();
298 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
299 Found.getLookupKind(), S, &SS,
300 LookupCtx, false,
301 CTC_CXXCasts)) {
302 Found.setLookupName(Corrected.getCorrection());
303 if (Corrected.getCorrectionDecl())
304 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000305 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000306 if (!Found.empty()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000307 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
308 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000309 if (LookupCtx)
310 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000311 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
312 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000313 else
314 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000315 << Name << CorrectedQuotedStr
316 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000317 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
318 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000319 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000320 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000321 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000322 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000323 }
324 }
325
Douglas Gregor312eadb2011-04-24 05:37:28 +0000326 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000327 if (Found.empty()) {
328 if (isDependent)
329 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000330 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000331 }
John McCallf7a1a742009-11-24 19:00:30 +0000332
333 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
334 // C++ [basic.lookup.classref]p1:
335 // [...] If the lookup in the class of the object expression finds a
336 // template, the name is also looked up in the context of the entire
337 // postfix-expression and [...]
338 //
339 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
340 LookupOrdinaryName);
341 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000342 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000343
John McCallf7a1a742009-11-24 19:00:30 +0000344 if (FoundOuter.empty()) {
345 // - if the name is not found, the name found in the class of the
346 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000347 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
348 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000349 // - if the name is found in the context of the entire
350 // postfix-expression and does not name a class template, the name
351 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000352 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000353 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000354 // - if the name found is a class template, it must refer to the same
355 // entity as the one found in the class of the object expression,
356 // otherwise the program is ill-formed.
357 if (!Found.isSingleResult() ||
358 Found.getFoundDecl()->getCanonicalDecl()
359 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000360 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000361 diag::ext_nested_name_member_ref_lookup_ambiguous)
362 << Found.getLookupName()
363 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000364 Diag(Found.getRepresentativeDecl()->getLocation(),
365 diag::note_ambig_member_ref_object_type)
366 << ObjectType;
367 Diag(FoundOuter.getFoundDecl()->getLocation(),
368 diag::note_ambig_member_ref_scope);
369
370 // Recover by taking the template that we found in the object
371 // expression's type.
372 }
373 }
374 }
375}
376
John McCall2f841ba2009-12-02 03:53:29 +0000377/// ActOnDependentIdExpression - Handle a dependent id-expression that
378/// was just parsed. This is only possible with an explicit scope
379/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000380ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000381Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000382 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000383 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000384 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000385 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000386
John McCall2f841ba2009-12-02 03:53:29 +0000387 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000388 isa<CXXMethodDecl>(DC) &&
389 cast<CXXMethodDecl>(DC)->isInstance()) {
390 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000391
John McCallf7a1a742009-11-24 19:00:30 +0000392 // Since the 'this' expression is synthesized, we don't need to
393 // perform the double-lookup check.
394 NamedDecl *FirstQualifierInScope = 0;
395
John McCallaa81e162009-12-01 22:10:20 +0000396 return Owned(CXXDependentScopeMemberExpr::Create(Context,
397 /*This*/ 0, ThisType,
398 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000399 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000400 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000401 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000402 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000403 TemplateArgs));
404 }
405
Abramo Bagnara25777432010-08-11 22:01:17 +0000406 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000407}
408
John McCall60d7b3a2010-08-24 06:29:42 +0000409ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000410Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000411 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000412 const TemplateArgumentListInfo *TemplateArgs) {
413 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000414 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000415 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000416 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000417}
418
Douglas Gregor72c3f312008-12-05 18:15:24 +0000419/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
420/// that the template parameter 'PrevDecl' is being shadowed by a new
421/// declaration at location Loc. Returns true to indicate that this is
422/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000423void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000424 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000425
426 // Microsoft Visual C++ permits template parameters to be shadowed.
Francois Pichet62ec1f22011-09-17 17:15:52 +0000427 if (getLangOptions().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000428 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000429
430 // C++ [temp.local]p4:
431 // A template-parameter shall not be redeclared within its
432 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000433 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000434 << cast<NamedDecl>(PrevDecl)->getDeclName();
435 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000436 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000437}
438
Douglas Gregor2943aed2009-03-03 04:44:36 +0000439/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000440/// the parameter D to reference the templated declaration and return a pointer
441/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000442TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
443 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
444 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000445 return Temp;
446 }
447 return 0;
448}
449
Douglas Gregorba68eca2011-01-05 17:40:24 +0000450ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
451 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000453 "Only template template arguments can be pack expansions here");
454 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
455 "Template template argument pack expansion without packs");
456 ParsedTemplateArgument Result(*this);
457 Result.EllipsisLoc = EllipsisLoc;
458 return Result;
459}
460
Douglas Gregor788cd062009-11-11 01:00:40 +0000461static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
462 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 switch (Arg.getKind()) {
465 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000466 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000467 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000468 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000469 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000470 return TemplateArgumentLoc(TemplateArgument(T), DI);
471 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000472
Douglas Gregor788cd062009-11-11 01:00:40 +0000473 case ParsedTemplateArgument::NonType: {
474 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
475 return TemplateArgumentLoc(TemplateArgument(E), E);
476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000477
Douglas Gregor788cd062009-11-11 01:00:40 +0000478 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000479 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000480 TemplateArgument TArg;
481 if (Arg.getEllipsisLoc().isValid())
482 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
483 else
484 TArg = Template;
485 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000486 Arg.getScopeSpec().getWithLocInContext(
487 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000488 Arg.getLocation(),
489 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000490 }
491 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000492
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000493 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000494 return TemplateArgumentLoc();
495}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000496
Douglas Gregor788cd062009-11-11 01:00:40 +0000497/// \brief Translates template arguments as provided by the parser
498/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000499void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
500 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000501 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000502 TemplateArgs.addArgument(translateTemplateArgument(*this,
503 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000504}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000505
Douglas Gregor72c3f312008-12-05 18:15:24 +0000506/// ActOnTypeParameter - Called when a C++ template type parameter
507/// (e.g., "typename T") has been parsed. Typename specifies whether
508/// the keyword "typename" was used to declare the type parameter
509/// (otherwise, "class" was used), and KeyLoc is the location of the
510/// "class" or "typename" keyword. ParamName is the name of the
511/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000512/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000513/// If the type parameter has a default argument, it will be added
514/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000515Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
516 SourceLocation EllipsisLoc,
517 SourceLocation KeyLoc,
518 IdentifierInfo *ParamName,
519 SourceLocation ParamNameLoc,
520 unsigned Depth, unsigned Position,
521 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000522 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523 assert(S->isTemplateParamScope() &&
524 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000525 bool Invalid = false;
526
527 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000528 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000529 LookupOrdinaryName,
530 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000531 if (PrevDecl && PrevDecl->isTemplateParameter()) {
532 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
533 PrevDecl = 0;
534 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000535 }
536
Douglas Gregorddc29e12009-02-06 22:42:48 +0000537 SourceLocation Loc = ParamNameLoc;
538 if (!ParamName)
539 Loc = KeyLoc;
540
Douglas Gregor72c3f312008-12-05 18:15:24 +0000541 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000542 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000543 KeyLoc, Loc, Depth, Position, ParamName,
544 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000545 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000546 if (Invalid)
547 Param->setInvalidDecl();
548
549 if (ParamName) {
550 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000551 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000552 IdResolver.AddDecl(Param);
553 }
554
Douglas Gregor61c4d282011-01-05 15:48:55 +0000555 // C++0x [temp.param]p9:
556 // A default template-argument may be specified for any kind of
557 // template-parameter that is not a template parameter pack.
558 if (DefaultArg && Ellipsis) {
559 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
560 DefaultArg = ParsedType();
561 }
562
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000563 // Handle the default argument, if provided.
564 if (DefaultArg) {
565 TypeSourceInfo *DefaultTInfo;
566 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000568 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000569
Douglas Gregor6f526752010-12-16 08:48:57 +0000570 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000571 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000572 UPPC_DefaultArgument))
573 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000574
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000575 // Check the template argument itself.
576 if (CheckTemplateArgument(Param, DefaultTInfo)) {
577 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000578 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000579 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000580
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000581 Param->setDefaultArgument(DefaultTInfo, false);
582 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000583
John McCalld226f652010-08-21 09:40:31 +0000584 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000585}
586
Douglas Gregor2943aed2009-03-03 04:44:36 +0000587/// \brief Check that the type of a non-type template parameter is
588/// well-formed.
589///
590/// \returns the (possibly-promoted) parameter type if valid;
591/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000592QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000593Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000594 // We don't allow variably-modified types as the type of non-type template
595 // parameters.
596 if (T->isVariablyModifiedType()) {
597 Diag(Loc, diag::err_variably_modified_nontype_template_param)
598 << T;
599 return QualType();
600 }
601
Douglas Gregor2943aed2009-03-03 04:44:36 +0000602 // C++ [temp.param]p4:
603 //
604 // A non-type template-parameter shall have one of the following
605 // (optionally cv-qualified) types:
606 //
607 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000608 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000609 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000610 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000611 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000612 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000613 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000614 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000615 // -- std::nullptr_t.
616 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000617 // If T is a dependent type, we can't do the check now, so we
618 // assume that it is well-formed.
619 T->isDependentType())
620 return T;
621 // C++ [temp.param]p8:
622 //
623 // A non-type template-parameter of type "array of T" or
624 // "function returning T" is adjusted to be of type "pointer to
625 // T" or "pointer to function returning T", respectively.
626 else if (T->isArrayType())
627 // FIXME: Keep the type prior to promotion?
628 return Context.getArrayDecayedType(T);
629 else if (T->isFunctionType())
630 // FIXME: Keep the type prior to promotion?
631 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000632
Douglas Gregor2943aed2009-03-03 04:44:36 +0000633 Diag(Loc, diag::err_template_nontype_parm_bad_type)
634 << T;
635
636 return QualType();
637}
638
John McCalld226f652010-08-21 09:40:31 +0000639Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
640 unsigned Depth,
641 unsigned Position,
642 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000643 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000644 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
645 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000646
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000647 assert(S->isTemplateParamScope() &&
648 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000649 bool Invalid = false;
650
651 IdentifierInfo *ParamName = D.getIdentifier();
652 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000653 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000654 LookupOrdinaryName,
655 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000656 if (PrevDecl && PrevDecl->isTemplateParameter()) {
657 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
658 PrevDecl = 0;
659 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000660 }
661
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000662 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
663 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000664 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000665 Invalid = true;
666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000667
Douglas Gregor10738d32010-12-23 23:51:58 +0000668 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000669 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000670 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000671 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000672 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000673 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000674 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000675 Param->setAccess(AS_public);
676
Douglas Gregor72c3f312008-12-05 18:15:24 +0000677 if (Invalid)
678 Param->setInvalidDecl();
679
680 if (D.getIdentifier()) {
681 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000682 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000683 IdResolver.AddDecl(Param);
684 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000685
Douglas Gregor61c4d282011-01-05 15:48:55 +0000686 // C++0x [temp.param]p9:
687 // A default template-argument may be specified for any kind of
688 // template-parameter that is not a template parameter pack.
689 if (Default && IsParameterPack) {
690 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
691 Default = 0;
692 }
693
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000694 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000695 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000696 // Check for unexpanded parameter packs.
697 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
698 return Param;
699
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000700 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000701 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
702 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000703 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000704 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000705 }
John Wiegley429bb272011-04-08 18:41:53 +0000706 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000707
John McCall9ae2f072010-08-23 23:25:46 +0000708 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000709 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000710
John McCalld226f652010-08-21 09:40:31 +0000711 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000712}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000713
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000714/// ActOnTemplateTemplateParameter - Called when a C++ template template
715/// parameter (e.g. T in template <template <typename> class T> class array)
716/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000717Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
718 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000719 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000720 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000721 IdentifierInfo *Name,
722 SourceLocation NameLoc,
723 unsigned Depth,
724 unsigned Position,
725 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000726 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000727 assert(S->isTemplateParamScope() &&
728 "Template template parameter not in template parameter scope!");
729
730 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000731 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000732 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000733 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000734 NameLoc.isInvalid()? TmpLoc : NameLoc,
735 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000736 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000737 Param->setAccess(AS_public);
738
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000739 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000740 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000741 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000742 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000743 IdResolver.AddDecl(Param);
744 }
745
Douglas Gregor6f526752010-12-16 08:48:57 +0000746 if (Params->size() == 0) {
747 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
748 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
749 Param->setInvalidDecl();
750 }
751
Douglas Gregor61c4d282011-01-05 15:48:55 +0000752 // C++0x [temp.param]p9:
753 // A default template-argument may be specified for any kind of
754 // template-parameter that is not a template parameter pack.
755 if (IsParameterPack && !Default.isInvalid()) {
756 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
757 Default = ParsedTemplateArgument();
758 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000759
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000760 if (!Default.isInvalid()) {
761 // Check only that we have a template template argument. We don't want to
762 // try to check well-formedness now, because our template template parameter
763 // might have dependent types in its template parameters, which we wouldn't
764 // be able to match now.
765 //
766 // If none of the template template parameter's template arguments mention
767 // other template parameters, we could actually perform more checking here.
768 // However, it isn't worth doing.
769 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
770 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
771 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
772 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000773 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775
Douglas Gregor6f526752010-12-16 08:48:57 +0000776 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000777 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000778 DefaultArg.getArgument().getAsTemplate(),
779 UPPC_DefaultArgument))
780 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000781
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000782 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000783 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000784
John McCalld226f652010-08-21 09:40:31 +0000785 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000786}
787
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000788/// ActOnTemplateParameterList - Builds a TemplateParameterList that
789/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000790TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000791Sema::ActOnTemplateParameterList(unsigned Depth,
792 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000793 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000794 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000795 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000796 SourceLocation RAngleLoc) {
797 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000798 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000799
Douglas Gregorddc29e12009-02-06 22:42:48 +0000800 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000801 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000802 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000803}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000804
John McCallb6217662010-03-15 10:12:16 +0000805static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
806 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000807 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000808}
809
John McCallf312b1e2010-08-26 23:41:50 +0000810DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000811Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000812 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000813 IdentifierInfo *Name, SourceLocation NameLoc,
814 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000815 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000816 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000817 unsigned NumOuterTemplateParamLists,
818 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000819 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000820 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000821 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000822 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000823
824 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000825 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000826 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000827
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000828 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
829 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000830
831 // There is no such thing as an unnamed class template.
832 if (!Name) {
833 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000834 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000835 }
836
837 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000838 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000839 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000840 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000841 if (SS.isNotEmpty() && !SS.isInvalid()) {
842 SemanticContext = computeDeclContext(SS, true);
843 if (!SemanticContext) {
844 // FIXME: Produce a reasonable diagnostic here
845 return true;
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
John McCall77bb1aa2010-05-01 00:40:08 +0000848 if (RequireCompleteDeclContext(SS, SemanticContext))
849 return true;
850
Douglas Gregor20606502011-10-14 15:31:12 +0000851 // If we're adding a template to a dependent context, we may need to
852 // rebuilding some of the types used within the template parameter list,
853 // now that we know what the current instantiation is.
854 if (SemanticContext->isDependentContext()) {
855 ContextRAII SavedContext(*this, SemanticContext);
856 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
857 Invalid = true;
858 }
859
John McCalla24dc2e2009-11-17 02:14:36 +0000860 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000861 } else {
862 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000863 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregor57265e32010-04-12 16:00:01 +0000866 if (Previous.isAmbiguous())
867 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000868
Douglas Gregorddc29e12009-02-06 22:42:48 +0000869 NamedDecl *PrevDecl = 0;
870 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000871 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000872
Douglas Gregorddc29e12009-02-06 22:42:48 +0000873 // If there is a previous declaration with the same name, check
874 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000875 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000876 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000877
878 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000879 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000880 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000881 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000882 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
883 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000885 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
886 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
887 PrevClassTemplate
888 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
889 ->getSpecializedTemplate();
890 }
891 }
892
John McCall65c49462009-12-18 11:25:59 +0000893 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000894 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895 // [...] When looking for a prior declaration of a class or a function
896 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000897 // function is neither a qualified name nor a template-id, scopes outside
898 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000899 if (!SS.isSet()) {
900 DeclContext *OutermostContext = CurContext;
901 while (!OutermostContext->isFileContext())
902 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000903
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000904 if (PrevDecl &&
905 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
906 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
907 SemanticContext = PrevDecl->getDeclContext();
908 } else {
909 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000910 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000911 // declaration.
912 PrevDecl = PrevClassTemplate = 0;
913 SemanticContext = OutermostContext;
914 }
John McCalle129d442009-12-17 23:21:11 +0000915 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000916
John McCalle129d442009-12-17 23:21:11 +0000917 if (CurContext->isDependentContext()) {
918 // If this is a dependent context, we don't want to link the friend
919 // class template to the template in scope, because that would perform
920 // checking of the template parameter lists that can't be performed
921 // until the outer context is instantiated.
922 PrevDecl = PrevClassTemplate = 0;
923 }
924 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
925 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000926
Douglas Gregorddc29e12009-02-06 22:42:48 +0000927 if (PrevClassTemplate) {
928 // Ensure that the template parameter lists are compatible.
929 if (!TemplateParameterListsAreEqual(TemplateParams,
930 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000931 /*Complain=*/true,
932 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000933 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000934
935 // C++ [temp.class]p4:
936 // In a redeclaration, partial specialization, explicit
937 // specialization or explicit instantiation of a class template,
938 // the class-key shall agree in kind with the original class
939 // template declaration (7.1.5.3).
940 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000941 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
942 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000943 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000944 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000945 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000946 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000947 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000948 }
949
Douglas Gregorddc29e12009-02-06 22:42:48 +0000950 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000951 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000952 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000953 Diag(NameLoc, diag::err_redefinition) << Name;
954 Diag(Def->getLocation(), diag::note_previous_definition);
955 // FIXME: Would it make sense to try to "forget" the previous
956 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000957 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000958 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000959 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000960 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
961 // Maybe we will complain about the shadowed template parameter.
962 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
963 // Just pretend that we didn't see the previous declaration.
964 PrevDecl = 0;
965 } else if (PrevDecl) {
966 // C++ [temp]p5:
967 // A class template shall not have the same name as any other
968 // template, class, function, object, enumeration, enumerator,
969 // namespace, or type in the same scope (3.3), except as specified
970 // in (14.5.4).
971 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
972 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000973 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000974 }
975
Douglas Gregord684b002009-02-10 19:49:53 +0000976 // Check the template parameter list of this declaration, possibly
977 // merging in the template parameter list from the previous class
978 // template declaration.
979 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000980 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000981 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000982 SemanticContext->isRecord() &&
983 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000984 ? TPC_ClassTemplateMember
985 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000986 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Douglas Gregor57265e32010-04-12 16:00:01 +0000988 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000989 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000990 // template out-of-line.
991 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
992 !(TUK == TUK_Friend && CurContext->isDependentContext()))
993 Diag(NameLoc, diag::err_member_def_does_not_match)
994 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000995 }
996
Mike Stump1eb44332009-09-09 15:08:12 +0000997 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000998 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000999 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001000 PrevClassTemplate->getTemplatedDecl() : 0,
1001 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001002 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001003 if (NumOuterTemplateParamLists > 0)
1004 NewClass->setTemplateParameterListsInfo(Context,
1005 NumOuterTemplateParamLists,
1006 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001007
1008 ClassTemplateDecl *NewTemplate
1009 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1010 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001011 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001012 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001013
1014 if (PrevClassTemplate && PrevClassTemplate->isModulePrivate()) {
1015 NewTemplate->setModulePrivate();
Douglas Gregore7612302011-09-09 19:05:14 +00001016 } else if (ModulePrivateLoc.isValid()) {
1017 if (PrevClassTemplate && !PrevClassTemplate->isModulePrivate())
1018 diagnoseModulePrivateRedeclaration(NewTemplate, PrevClassTemplate,
1019 ModulePrivateLoc);
1020 else
1021 NewTemplate->setModulePrivate();
1022 }
Douglas Gregor8d267c52011-09-09 02:06:17 +00001023
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001024 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001025 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001026 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001027 assert(T->isDependentType() && "Class template type is not dependent?");
1028 (void)T;
1029
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001030 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001031 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001032 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001033 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1034 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001035
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001036 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001037 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001038 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Douglas Gregorddc29e12009-02-06 22:42:48 +00001040 // Set the lexical context of these templates
1041 NewClass->setLexicalDeclContext(CurContext);
1042 NewTemplate->setLexicalDeclContext(CurContext);
1043
John McCall0f434ec2009-07-31 02:45:11 +00001044 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001045 NewClass->startDefinition();
1046
1047 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001048 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001049
John McCall05b23ea2009-09-14 21:59:20 +00001050 if (TUK != TUK_Friend)
1051 PushOnScopeChains(NewTemplate, S);
1052 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001053 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001054 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001055 NewClass->setAccess(PrevClassTemplate->getAccess());
1056 }
John McCall05b23ea2009-09-14 21:59:20 +00001057
Douglas Gregord85bea22009-09-26 06:47:28 +00001058 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1059 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001060
John McCall05b23ea2009-09-14 21:59:20 +00001061 // Friend templates are visible in fairly strange ways.
1062 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001063 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001064 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1065 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1066 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001067 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001068 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001069
Douglas Gregord85bea22009-09-26 06:47:28 +00001070 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1071 NewClass->getLocation(),
1072 NewTemplate,
1073 /*FIXME:*/NewClass->getLocation());
1074 Friend->setAccess(AS_public);
1075 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001076 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001077
Douglas Gregord684b002009-02-10 19:49:53 +00001078 if (Invalid) {
1079 NewTemplate->setInvalidDecl();
1080 NewClass->setInvalidDecl();
1081 }
John McCalld226f652010-08-21 09:40:31 +00001082 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001083}
1084
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001085/// \brief Diagnose the presence of a default template argument on a
1086/// template parameter, which is ill-formed in certain contexts.
1087///
1088/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001089static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001090 Sema::TemplateParamListContext TPC,
1091 SourceLocation ParamLoc,
1092 SourceRange DefArgRange) {
1093 switch (TPC) {
1094 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001095 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001096 return false;
1097
1098 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001099 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001100 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001101 // A default template-argument shall not be specified in a
1102 // function template declaration or a function template
1103 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001104 // If a friend function template declaration specifies a default
1105 // template-argument, that declaration shall be a definition and shall be
1106 // the only declaration of the function template in the translation unit.
1107 // (C++98/03 doesn't have this wording; see DR226).
Richard Smithebaf0e62011-10-18 20:49:44 +00001108 S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
1109 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1110 : diag::ext_template_parameter_default_in_function_template)
1111 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001112 return false;
1113
1114 case Sema::TPC_ClassTemplateMember:
1115 // C++0x [temp.param]p9:
1116 // A default template-argument shall not be specified in the
1117 // template-parameter-lists of the definition of a member of a
1118 // class template that appears outside of the member's class.
1119 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1120 << DefArgRange;
1121 return true;
1122
1123 case Sema::TPC_FriendFunctionTemplate:
1124 // C++ [temp.param]p9:
1125 // A default template-argument shall not be specified in a
1126 // friend template declaration.
1127 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1128 << DefArgRange;
1129 return true;
1130
1131 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1132 // for friend function templates if there is only a single
1133 // declaration (and it is a definition). Strange!
1134 }
1135
1136 return false;
1137}
1138
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001139/// \brief Check for unexpanded parameter packs within the template parameters
1140/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001141static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1142 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001143 TemplateParameterList *Params = TTP->getTemplateParameters();
1144 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1145 NamedDecl *P = Params->getParam(I);
1146 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001147 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001148 NTTP->getTypeSourceInfo(),
1149 Sema::UPPC_NonTypeTemplateParameterType))
1150 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001151
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001152 continue;
1153 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001154
1155 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001156 = dyn_cast<TemplateTemplateParmDecl>(P))
1157 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1158 return true;
1159 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001160
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001161 return false;
1162}
1163
Douglas Gregord684b002009-02-10 19:49:53 +00001164/// \brief Checks the validity of a template parameter list, possibly
1165/// considering the template parameter list from a previous
1166/// declaration.
1167///
1168/// If an "old" template parameter list is provided, it must be
1169/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1170/// template parameter list.
1171///
1172/// \param NewParams Template parameter list for a new template
1173/// declaration. This template parameter list will be updated with any
1174/// default arguments that are carried through from the previous
1175/// template parameter list.
1176///
1177/// \param OldParams If provided, template parameter list from a
1178/// previous declaration of the same template. Default template
1179/// arguments will be merged from the old template parameter list to
1180/// the new template parameter list.
1181///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001182/// \param TPC Describes the context in which we are checking the given
1183/// template parameter list.
1184///
Douglas Gregord684b002009-02-10 19:49:53 +00001185/// \returns true if an error occurred, false otherwise.
1186bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001187 TemplateParameterList *OldParams,
1188 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001189 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Douglas Gregord684b002009-02-10 19:49:53 +00001191 // C++ [temp.param]p10:
1192 // The set of default template-arguments available for use with a
1193 // template declaration or definition is obtained by merging the
1194 // default arguments from the definition (if in scope) and all
1195 // declarations in scope in the same way default function
1196 // arguments are (8.3.6).
1197 bool SawDefaultArgument = false;
1198 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001199
Mike Stump1a35fde2009-02-11 23:03:27 +00001200 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001201 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001202 if (OldParams)
1203 OldParam = OldParams->begin();
1204
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001205 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001206 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1207 NewParamEnd = NewParams->end();
1208 NewParam != NewParamEnd; ++NewParam) {
1209 // Variables used to diagnose redundant default arguments
1210 bool RedundantDefaultArg = false;
1211 SourceLocation OldDefaultLoc;
1212 SourceLocation NewDefaultLoc;
1213
David Blaikie1368e582011-10-19 05:19:50 +00001214 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001215 bool MissingDefaultArg = false;
1216
David Blaikie1368e582011-10-19 05:19:50 +00001217 // Variable used to diagnose non-final parameter packs
1218 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001219
Douglas Gregord684b002009-02-10 19:49:53 +00001220 if (TemplateTypeParmDecl *NewTypeParm
1221 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001222 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001223 if (NewTypeParm->hasDefaultArgument() &&
1224 DiagnoseDefaultTemplateArgument(*this, TPC,
1225 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001226 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001227 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001228 NewTypeParm->removeDefaultArgument();
1229
1230 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001231 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001232 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Anders Carlsson49d25572009-06-12 23:20:15 +00001234 if (NewTypeParm->isParameterPack()) {
1235 assert(!NewTypeParm->hasDefaultArgument() &&
1236 "Parameter packs can't have a default argument!");
1237 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001238 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001239 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001240 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1241 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1242 SawDefaultArgument = true;
1243 RedundantDefaultArg = true;
1244 PreviousDefaultArgLoc = NewDefaultLoc;
1245 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1246 // Merge the default argument from the old declaration to the
1247 // new declaration.
1248 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001249 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001250 true);
1251 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1252 } else if (NewTypeParm->hasDefaultArgument()) {
1253 SawDefaultArgument = true;
1254 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1255 } else if (SawDefaultArgument)
1256 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001257 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001258 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001259 // Check for unexpanded parameter packs.
1260 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001261 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001262 UPPC_NonTypeTemplateParameterType)) {
1263 Invalid = true;
1264 continue;
1265 }
1266
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001267 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001268 if (NewNonTypeParm->hasDefaultArgument() &&
1269 DiagnoseDefaultTemplateArgument(*this, TPC,
1270 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001271 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001272 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001273 }
1274
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001275 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001276 NonTypeTemplateParmDecl *OldNonTypeParm
1277 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001278 if (NewNonTypeParm->isParameterPack()) {
1279 assert(!NewNonTypeParm->hasDefaultArgument() &&
1280 "Parameter packs can't have a default argument!");
1281 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001282 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001283 NewNonTypeParm->hasDefaultArgument()) {
1284 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1285 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1286 SawDefaultArgument = true;
1287 RedundantDefaultArg = true;
1288 PreviousDefaultArgLoc = NewDefaultLoc;
1289 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1290 // Merge the default argument from the old declaration to the
1291 // new declaration.
1292 SawDefaultArgument = true;
1293 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001294 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001295 // parameter.
1296 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001297 OldNonTypeParm->getDefaultArgument(),
1298 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001299 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1300 } else if (NewNonTypeParm->hasDefaultArgument()) {
1301 SawDefaultArgument = true;
1302 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1303 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001304 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001305 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001306 TemplateTemplateParmDecl *NewTemplateParm
1307 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001308
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001309 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001310 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001311 Invalid = true;
1312 continue;
1313 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001314
David Blaikie1368e582011-10-19 05:19:50 +00001315 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001316 if (NewTemplateParm->hasDefaultArgument() &&
1317 DiagnoseDefaultTemplateArgument(*this, TPC,
1318 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001319 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001320 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001321
1322 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001323 TemplateTemplateParmDecl *OldTemplateParm
1324 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001325 if (NewTemplateParm->isParameterPack()) {
1326 assert(!NewTemplateParm->hasDefaultArgument() &&
1327 "Parameter packs can't have a default argument!");
1328 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001329 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001330 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001331 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1332 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001333 SawDefaultArgument = true;
1334 RedundantDefaultArg = true;
1335 PreviousDefaultArgLoc = NewDefaultLoc;
1336 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1337 // Merge the default argument from the old declaration to the
1338 // new declaration.
1339 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001340 // FIXME: We need to create a new kind of "default argument" expression
1341 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001342 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001343 OldTemplateParm->getDefaultArgument(),
1344 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001345 PreviousDefaultArgLoc
1346 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001347 } else if (NewTemplateParm->hasDefaultArgument()) {
1348 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001349 PreviousDefaultArgLoc
1350 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001351 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001352 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001353 }
1354
David Blaikie1368e582011-10-19 05:19:50 +00001355 // C++0x [temp.param]p11:
1356 // If a template parameter of a primary class template or alias template
1357 // is a template parameter pack, it shall be the last template parameter.
1358 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1359 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1360 Diag((*NewParam)->getLocation(),
1361 diag::err_template_param_pack_must_be_last_template_parameter);
1362 Invalid = true;
1363 }
1364
Douglas Gregord684b002009-02-10 19:49:53 +00001365 if (RedundantDefaultArg) {
1366 // C++ [temp.param]p12:
1367 // A template-parameter shall not be given default arguments
1368 // by two different declarations in the same scope.
1369 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1370 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1371 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001372 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001373 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001374 // If a template-parameter of a class template has a default
1375 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001376 // have a default template-argument supplied or be a template parameter
1377 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001378 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001379 diag::err_template_param_default_arg_missing);
1380 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1381 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001382 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001383 }
1384
1385 // If we have an old template parameter list that we're merging
1386 // in, move on to the next parameter.
1387 if (OldParams)
1388 ++OldParam;
1389 }
1390
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001391 // We were missing some default arguments at the end of the list, so remove
1392 // all of the default arguments.
1393 if (RemoveDefaultArguments) {
1394 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1395 NewParamEnd = NewParams->end();
1396 NewParam != NewParamEnd; ++NewParam) {
1397 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1398 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001399 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001400 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1401 NTTP->removeDefaultArgument();
1402 else
1403 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1404 }
1405 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001406
Douglas Gregord684b002009-02-10 19:49:53 +00001407 return Invalid;
1408}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001409
John McCall4e2cbb22010-10-20 05:44:58 +00001410namespace {
1411
1412/// A class which looks for a use of a certain level of template
1413/// parameter.
1414struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1415 typedef RecursiveASTVisitor<DependencyChecker> super;
1416
1417 unsigned Depth;
1418 bool Match;
1419
1420 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1421 NamedDecl *ND = Params->getParam(0);
1422 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1423 Depth = PD->getDepth();
1424 } else if (NonTypeTemplateParmDecl *PD =
1425 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1426 Depth = PD->getDepth();
1427 } else {
1428 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1429 }
1430 }
1431
1432 bool Matches(unsigned ParmDepth) {
1433 if (ParmDepth >= Depth) {
1434 Match = true;
1435 return true;
1436 }
1437 return false;
1438 }
1439
1440 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1441 return !Matches(T->getDepth());
1442 }
1443
1444 bool TraverseTemplateName(TemplateName N) {
1445 if (TemplateTemplateParmDecl *PD =
1446 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1447 if (Matches(PD->getDepth())) return false;
1448 return super::TraverseTemplateName(N);
1449 }
1450
1451 bool VisitDeclRefExpr(DeclRefExpr *E) {
1452 if (NonTypeTemplateParmDecl *PD =
1453 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1454 if (PD->getDepth() == Depth) {
1455 Match = true;
1456 return false;
1457 }
1458 }
1459 return super::VisitDeclRefExpr(E);
1460 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001461
1462 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1463 return TraverseType(T->getInjectedSpecializationType());
1464 }
John McCall4e2cbb22010-10-20 05:44:58 +00001465};
1466}
1467
Douglas Gregorc8406492011-05-10 18:27:06 +00001468/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001469/// list.
1470static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001471DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001472 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001473 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001474 return Checker.Match;
1475}
1476
Douglas Gregorc8406492011-05-10 18:27:06 +00001477// Find the source range corresponding to the named type in the given
1478// nested-name-specifier, if any.
1479static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1480 QualType T,
1481 const CXXScopeSpec &SS) {
1482 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1483 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1484 if (const Type *CurType = NNS->getAsType()) {
1485 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1486 return NNSLoc.getTypeLoc().getSourceRange();
1487 } else
1488 break;
1489
1490 NNSLoc = NNSLoc.getPrefix();
1491 }
1492
1493 return SourceRange();
1494}
1495
Mike Stump1eb44332009-09-09 15:08:12 +00001496/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001497/// specifier, returning the template parameter list that applies to the
1498/// name.
1499///
1500/// \param DeclStartLoc the start of the declaration that has a scope
1501/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001502///
Douglas Gregorc8406492011-05-10 18:27:06 +00001503/// \param DeclLoc The location of the declaration itself.
1504///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001505/// \param SS the scope specifier that will be matched to the given template
1506/// parameter lists. This scope specifier precedes a qualified name that is
1507/// being declared.
1508///
1509/// \param ParamLists the template parameter lists, from the outermost to the
1510/// innermost template parameter lists.
1511///
1512/// \param NumParamLists the number of template parameter lists in ParamLists.
1513///
John McCall77e8b112010-04-13 20:37:33 +00001514/// \param IsFriend Whether to apply the slightly different rules for
1515/// matching template parameters to scope specifiers in friend
1516/// declarations.
1517///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001518/// \param IsExplicitSpecialization will be set true if the entity being
1519/// declared is an explicit specialization, false otherwise.
1520///
Mike Stump1eb44332009-09-09 15:08:12 +00001521/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001522/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001523/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001524/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001525/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001526/// itself a template).
1527TemplateParameterList *
1528Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001529 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001530 const CXXScopeSpec &SS,
1531 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001532 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001533 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001534 bool &IsExplicitSpecialization,
1535 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001536 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001537 Invalid = false;
1538
1539 // The sequence of nested types to which we will match up the template
1540 // parameter lists. We first build this list by starting with the type named
1541 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001542 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001543 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001544 if (SS.getScopeRep()) {
1545 if (CXXRecordDecl *Record
1546 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1547 T = Context.getTypeDeclType(Record);
1548 else
1549 T = QualType(SS.getScopeRep()->getAsType(), 0);
1550 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001551
1552 // If we found an explicit specialization that prevents us from needing
1553 // 'template<>' headers, this will be set to the location of that
1554 // explicit specialization.
1555 SourceLocation ExplicitSpecLoc;
1556
1557 while (!T.isNull()) {
1558 NestedTypes.push_back(T);
1559
1560 // Retrieve the parent of a record type.
1561 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1562 // If this type is an explicit specialization, we're done.
1563 if (ClassTemplateSpecializationDecl *Spec
1564 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1565 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1566 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1567 ExplicitSpecLoc = Spec->getLocation();
1568 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001569 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001570 } else if (Record->getTemplateSpecializationKind()
1571 == TSK_ExplicitSpecialization) {
1572 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001573 break;
1574 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001575
1576 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1577 T = Context.getTypeDeclType(Parent);
1578 else
1579 T = QualType();
1580 continue;
1581 }
1582
1583 if (const TemplateSpecializationType *TST
1584 = T->getAs<TemplateSpecializationType>()) {
1585 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1586 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1587 T = Context.getTypeDeclType(Parent);
1588 else
1589 T = QualType();
1590 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001591 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001592 }
1593
1594 // Look one step prior in a dependent template specialization type.
1595 if (const DependentTemplateSpecializationType *DependentTST
1596 = T->getAs<DependentTemplateSpecializationType>()) {
1597 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1598 T = QualType(NNS->getAsType(), 0);
1599 else
1600 T = QualType();
1601 continue;
1602 }
1603
1604 // Look one step prior in a dependent name type.
1605 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1606 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1607 T = QualType(NNS->getAsType(), 0);
1608 else
1609 T = QualType();
1610 continue;
1611 }
1612
1613 // Retrieve the parent of an enumeration type.
1614 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1615 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1616 // check here.
1617 EnumDecl *Enum = EnumT->getDecl();
1618
1619 // Get to the parent type.
1620 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1621 T = Context.getTypeDeclType(Parent);
1622 else
1623 T = QualType();
1624 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Douglas Gregorc8406492011-05-10 18:27:06 +00001627 T = QualType();
1628 }
1629 // Reverse the nested types list, since we want to traverse from the outermost
1630 // to the innermost while checking template-parameter-lists.
1631 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001632
Douglas Gregorc8406492011-05-10 18:27:06 +00001633 // C++0x [temp.expl.spec]p17:
1634 // A member or a member template may be nested within many
1635 // enclosing class templates. In an explicit specialization for
1636 // such a member, the member declaration shall be preceded by a
1637 // template<> for each enclosing class template that is
1638 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001639 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001640 unsigned ParamIdx = 0;
1641 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1642 ++TypeIdx) {
1643 T = NestedTypes[TypeIdx];
1644
1645 // Whether we expect a 'template<>' header.
1646 bool NeedEmptyTemplateHeader = false;
1647
1648 // Whether we expect a template header with parameters.
1649 bool NeedNonemptyTemplateHeader = false;
1650
1651 // For a dependent type, the set of template parameters that we
1652 // expect to see.
1653 TemplateParameterList *ExpectedTemplateParams = 0;
1654
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001655 // C++0x [temp.expl.spec]p15:
1656 // A member or a member template may be nested within many enclosing
1657 // class templates. In an explicit specialization for such a member, the
1658 // member declaration shall be preceded by a template<> for each
1659 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001660 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1661 if (ClassTemplatePartialSpecializationDecl *Partial
1662 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1663 ExpectedTemplateParams = Partial->getTemplateParameters();
1664 NeedNonemptyTemplateHeader = true;
1665 } else if (Record->isDependentType()) {
1666 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001667 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001668 ->getTemplateParameters();
1669 NeedNonemptyTemplateHeader = true;
1670 }
1671 } else if (ClassTemplateSpecializationDecl *Spec
1672 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1673 // C++0x [temp.expl.spec]p4:
1674 // Members of an explicitly specialized class template are defined
1675 // in the same manner as members of normal classes, and not using
1676 // the template<> syntax.
1677 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1678 NeedEmptyTemplateHeader = true;
1679 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001680 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001681 } else if (Record->getTemplateSpecializationKind()) {
1682 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001683 != TSK_ExplicitSpecialization &&
1684 TypeIdx == NumTypes - 1)
1685 IsExplicitSpecialization = true;
1686
1687 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001688 }
1689 } else if (const TemplateSpecializationType *TST
1690 = T->getAs<TemplateSpecializationType>()) {
1691 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1692 ExpectedTemplateParams = Template->getTemplateParameters();
1693 NeedNonemptyTemplateHeader = true;
1694 }
1695 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1696 // FIXME: We actually could/should check the template arguments here
1697 // against the corresponding template parameter list.
1698 NeedNonemptyTemplateHeader = false;
1699 }
1700
Douglas Gregor89b9f102011-06-06 15:22:55 +00001701 // C++ [temp.expl.spec]p16:
1702 // In an explicit specialization declaration for a member of a class
1703 // template or a member template that ap- pears in namespace scope, the
1704 // member template and some of its enclosing class templates may remain
1705 // unspecialized, except that the declaration shall not explicitly
1706 // specialize a class member template if its en- closing class templates
1707 // are not explicitly specialized as well.
1708 if (ParamIdx < NumParamLists) {
1709 if (ParamLists[ParamIdx]->size() == 0) {
1710 if (SawNonEmptyTemplateParameterList) {
1711 Diag(DeclLoc, diag::err_specialize_member_of_template)
1712 << ParamLists[ParamIdx]->getSourceRange();
1713 Invalid = true;
1714 IsExplicitSpecialization = false;
1715 return 0;
1716 }
1717 } else
1718 SawNonEmptyTemplateParameterList = true;
1719 }
1720
Douglas Gregorc8406492011-05-10 18:27:06 +00001721 if (NeedEmptyTemplateHeader) {
1722 // If we're on the last of the types, and we need a 'template<>' header
1723 // here, then it's an explicit specialization.
1724 if (TypeIdx == NumTypes - 1)
1725 IsExplicitSpecialization = true;
1726
1727 if (ParamIdx < NumParamLists) {
1728 if (ParamLists[ParamIdx]->size() > 0) {
1729 // The header has template parameters when it shouldn't. Complain.
1730 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1731 diag::err_template_param_list_matches_nontemplate)
1732 << T
1733 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1734 ParamLists[ParamIdx]->getRAngleLoc())
1735 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1736 Invalid = true;
1737 return 0;
1738 }
1739
1740 // Consume this template header.
1741 ++ParamIdx;
1742 continue;
1743 }
1744
1745 if (!IsFriend) {
1746 // We don't have a template header, but we should.
1747 SourceLocation ExpectedTemplateLoc;
1748 if (NumParamLists > 0)
1749 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1750 else
1751 ExpectedTemplateLoc = DeclStartLoc;
1752
1753 Diag(DeclLoc, diag::err_template_spec_needs_header)
1754 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1755 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1756 }
1757
1758 continue;
1759 }
1760
1761 if (NeedNonemptyTemplateHeader) {
1762 // In friend declarations we can have template-ids which don't
1763 // depend on the corresponding template parameter lists. But
1764 // assume that empty parameter lists are supposed to match this
1765 // template-id.
1766 if (IsFriend && T->isDependentType()) {
1767 if (ParamIdx < NumParamLists &&
1768 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1769 ExpectedTemplateParams = 0;
1770 else
1771 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001772 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001773
Douglas Gregorc8406492011-05-10 18:27:06 +00001774 if (ParamIdx < NumParamLists) {
1775 // Check the template parameter list, if we can.
1776 if (ExpectedTemplateParams &&
1777 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1778 ExpectedTemplateParams,
1779 true, TPL_TemplateMatch))
1780 Invalid = true;
1781
1782 if (!Invalid &&
1783 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1784 TPC_ClassTemplateMember))
1785 Invalid = true;
1786
1787 ++ParamIdx;
1788 continue;
1789 }
1790
1791 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1792 << T
1793 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1794 Invalid = true;
1795 continue;
1796 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001797 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001798
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001799 // If there were at least as many template-ids as there were template
1800 // parameter lists, then there are no template parameter lists remaining for
1801 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001802 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001803 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001805 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001806 if (ParamIdx < NumParamLists - 1) {
1807 bool HasAnyExplicitSpecHeader = false;
1808 bool AllExplicitSpecHeaders = true;
1809 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1810 if (ParamLists[I]->size() == 0)
1811 HasAnyExplicitSpecHeader = true;
1812 else
1813 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001814 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001815
1816 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1817 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1818 : diag::err_template_spec_extra_headers)
1819 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1820 ParamLists[NumParamLists - 2]->getRAngleLoc());
1821
1822 // If there was a specialization somewhere, such that 'template<>' is
1823 // not required, and there were any 'template<>' headers, note where the
1824 // specialization occurred.
1825 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1826 Diag(ExplicitSpecLoc,
1827 diag::note_explicit_template_spec_does_not_need_header)
1828 << NestedTypes.back();
1829
1830 // We have a template parameter list with no corresponding scope, which
1831 // means that the resulting template declaration can't be instantiated
1832 // properly (we'll end up with dependent nodes when we shouldn't).
1833 if (!AllExplicitSpecHeaders)
1834 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001835 }
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Douglas Gregor89b9f102011-06-06 15:22:55 +00001837 // C++ [temp.expl.spec]p16:
1838 // In an explicit specialization declaration for a member of a class
1839 // template or a member template that ap- pears in namespace scope, the
1840 // member template and some of its enclosing class templates may remain
1841 // unspecialized, except that the declaration shall not explicitly
1842 // specialize a class member template if its en- closing class templates
1843 // are not explicitly specialized as well.
1844 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1845 SawNonEmptyTemplateParameterList) {
1846 Diag(DeclLoc, diag::err_specialize_member_of_template)
1847 << ParamLists[ParamIdx]->getSourceRange();
1848 Invalid = true;
1849 IsExplicitSpecialization = false;
1850 return 0;
1851 }
1852
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001853 // Return the last template parameter list, which corresponds to the
1854 // entity being declared.
1855 return ParamLists[NumParamLists - 1];
1856}
1857
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001858void Sema::NoteAllFoundTemplates(TemplateName Name) {
1859 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1860 Diag(Template->getLocation(), diag::note_template_declared_here)
1861 << (isa<FunctionTemplateDecl>(Template)? 0
1862 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001863 : isa<TypeAliasTemplateDecl>(Template)? 2
1864 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001865 << Template->getDeclName();
1866 return;
1867 }
1868
1869 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1870 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1871 IEnd = OST->end();
1872 I != IEnd; ++I)
1873 Diag((*I)->getLocation(), diag::note_template_declared_here)
1874 << 0 << (*I)->getDeclName();
1875
1876 return;
1877 }
1878}
1879
1880
Douglas Gregor7532dc62009-03-30 22:58:21 +00001881QualType Sema::CheckTemplateIdType(TemplateName Name,
1882 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001883 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001884 DependentTemplateName *DTN
1885 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001886 if (DTN && DTN->isIdentifier())
1887 // When building a template-id where the template-name is dependent,
1888 // assume the template is a type template. Either our assumption is
1889 // correct, or the code is ill-formed and will be diagnosed when the
1890 // dependent name is substituted.
1891 return Context.getDependentTemplateSpecializationType(ETK_None,
1892 DTN->getQualifier(),
1893 DTN->getIdentifier(),
1894 TemplateArgs);
1895
Douglas Gregor7532dc62009-03-30 22:58:21 +00001896 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001897 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1898 // We might have a substituted template template parameter pack. If so,
1899 // build a template specialization type for it.
1900 if (Name.getAsSubstTemplateTemplateParmPack())
1901 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001902
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001903 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1904 << Name;
1905 NoteAllFoundTemplates(Name);
1906 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001907 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001908
Douglas Gregor40808ce2009-03-09 23:48:35 +00001909 // Check that the template argument list is well-formed for this
1910 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001911 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001912 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001913 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001914 return QualType();
1915
Douglas Gregor910f8002010-11-07 23:05:16 +00001916 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001917 "Converted template argument list is too short!");
1918
1919 QualType CanonType;
1920
Douglas Gregor561f8122011-07-01 01:22:09 +00001921 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001922 if (TypeAliasTemplateDecl *AliasTemplate
1923 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1924 // Find the canonical type for this type alias template specialization.
1925 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1926 if (Pattern->isInvalidDecl())
1927 return QualType();
1928
1929 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1930 Converted.data(), Converted.size());
1931
1932 // Only substitute for the innermost template argument list.
1933 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001934 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001935 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1936 for (unsigned I = 0; I < Depth; ++I)
1937 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001938
1939 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1940 CanonType = SubstType(Pattern->getUnderlyingType(),
1941 TemplateArgLists, AliasTemplate->getLocation(),
1942 AliasTemplate->getDeclName());
1943 if (CanonType.isNull())
1944 return QualType();
1945 } else if (Name.isDependent() ||
1946 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001947 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001948 // This class template specialization is a dependent
1949 // type. Therefore, its canonical type is another class template
1950 // specialization type that contains all of the converted
1951 // arguments in canonical form. This ensures that, e.g., A<T> and
1952 // A<T, T> have identical types when A is declared as:
1953 //
1954 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001955 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001956 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001957 Converted.data(),
1958 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Douglas Gregor1275ae02009-07-28 23:00:59 +00001960 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001961 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001962 // In the future, we need to teach getTemplateSpecializationType to only
1963 // build the canonical type and return that to us.
1964 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001965
1966 // This might work out to be a current instantiation, in which
1967 // case the canonical type needs to be the InjectedClassNameType.
1968 //
1969 // TODO: in theory this could be a simple hashtable lookup; most
1970 // changes to CurContext don't change the set of current
1971 // instantiations.
1972 if (isa<ClassTemplateDecl>(Template)) {
1973 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1974 // If we get out to a namespace, we're done.
1975 if (Ctx->isFileContext()) break;
1976
1977 // If this isn't a record, keep looking.
1978 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1979 if (!Record) continue;
1980
1981 // Look for one of the two cases with InjectedClassNameTypes
1982 // and check whether it's the same template.
1983 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1984 !Record->getDescribedClassTemplate())
1985 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001986
John McCall31f17ec2010-04-27 00:57:59 +00001987 // Fetch the injected class name type and check whether its
1988 // injected type is equal to the type we just built.
1989 QualType ICNT = Context.getTypeDeclType(Record);
1990 QualType Injected = cast<InjectedClassNameType>(ICNT)
1991 ->getInjectedSpecializationType();
1992
1993 if (CanonType != Injected->getCanonicalTypeInternal())
1994 continue;
1995
1996 // If so, the canonical type of this TST is the injected
1997 // class name type of the record we just found.
1998 assert(ICNT.isCanonical());
1999 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002000 break;
2001 }
2002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002004 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002005 // Find the class template specialization declaration that
2006 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002007 void *InsertPos = 0;
2008 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002009 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002010 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002011 if (!Decl) {
2012 // This is the first time we have referenced this class template
2013 // specialization. Create the canonical declaration and add it to
2014 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002015 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002016 ClassTemplate->getTemplatedDecl()->getTagKind(),
2017 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002018 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002019 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002020 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002021 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002022 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002023 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002024 Decl->setLexicalDeclContext(CurContext);
2025 }
2026
2027 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002028 assert(isa<RecordType>(CanonType) &&
2029 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002030 }
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Douglas Gregor40808ce2009-03-09 23:48:35 +00002032 // Build the fully-sugared type for this class template
2033 // specialization, which refers back to the class template
2034 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002035 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002036}
2037
John McCallf312b1e2010-08-26 23:41:50 +00002038TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002039Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2040 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002041 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002042 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002043 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002044 if (SS.isInvalid())
2045 return true;
2046
Douglas Gregor7532dc62009-03-30 22:58:21 +00002047 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002048
Douglas Gregor40808ce2009-03-09 23:48:35 +00002049 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002050 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002051 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002052
Douglas Gregora88f09f2011-02-28 17:23:35 +00002053 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2054 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2055 DTN->getQualifier(),
2056 DTN->getIdentifier(),
2057 TemplateArgs);
2058
2059 // Build type-source information.
2060 TypeLocBuilder TLB;
2061 DependentTemplateSpecializationTypeLoc SpecTL
2062 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002063 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002064 SpecTL.setNameLoc(TemplateLoc);
2065 SpecTL.setLAngleLoc(LAngleLoc);
2066 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002067 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002068 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2069 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2070 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2071 }
2072
John McCalld5532b62009-11-23 01:53:49 +00002073 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002074 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002075
2076 if (Result.isNull())
2077 return true;
2078
Douglas Gregor059101f2011-03-02 00:47:37 +00002079 // Build type-source information.
2080 TypeLocBuilder TLB;
2081 TemplateSpecializationTypeLoc SpecTL
2082 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2083 SpecTL.setTemplateNameLoc(TemplateLoc);
2084 SpecTL.setLAngleLoc(LAngleLoc);
2085 SpecTL.setRAngleLoc(RAngleLoc);
2086 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2087 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002088
Douglas Gregor059101f2011-03-02 00:47:37 +00002089 if (SS.isNotEmpty()) {
2090 // Create an elaborated-type-specifier containing the nested-name-specifier.
2091 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2092 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2093 ElabTL.setKeywordLoc(SourceLocation());
2094 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2095 }
2096
2097 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002098}
John McCallf1bbbb42009-09-04 01:14:41 +00002099
Douglas Gregor059101f2011-03-02 00:47:37 +00002100TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002101 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002102 SourceLocation TagLoc,
2103 CXXScopeSpec &SS,
2104 TemplateTy TemplateD,
2105 SourceLocation TemplateLoc,
2106 SourceLocation LAngleLoc,
2107 ASTTemplateArgsPtr TemplateArgsIn,
2108 SourceLocation RAngleLoc) {
2109 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2110
2111 // Translate the parser's template argument list in our AST format.
2112 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2113 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2114
2115 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002116 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002117 ElaboratedTypeKeyword Keyword
2118 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Douglas Gregor059101f2011-03-02 00:47:37 +00002120 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2121 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2122 DTN->getQualifier(),
2123 DTN->getIdentifier(),
2124 TemplateArgs);
2125
2126 // Build type-source information.
2127 TypeLocBuilder TLB;
2128 DependentTemplateSpecializationTypeLoc SpecTL
2129 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2130 SpecTL.setKeywordLoc(TagLoc);
2131 SpecTL.setNameLoc(TemplateLoc);
2132 SpecTL.setLAngleLoc(LAngleLoc);
2133 SpecTL.setRAngleLoc(RAngleLoc);
2134 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2135 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2136 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2137 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2138 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002139
2140 if (TypeAliasTemplateDecl *TAT =
2141 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2142 // C++0x [dcl.type.elab]p2:
2143 // If the identifier resolves to a typedef-name or the simple-template-id
2144 // resolves to an alias template specialization, the
2145 // elaborated-type-specifier is ill-formed.
2146 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2147 Diag(TAT->getLocation(), diag::note_declared_at);
2148 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002149
2150 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2151 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002152 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002153
2154 // Check the tag kind
2155 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002156 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002157
John McCall6b2becf2009-09-08 17:47:29 +00002158 IdentifierInfo *Id = D->getIdentifier();
2159 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002160
Richard Trieubbf34c02011-06-10 03:11:26 +00002161 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2162 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002163 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002164 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002165 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002166 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002167 }
2168 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002169
2170 // Provide source-location information for the template specialization.
2171 TypeLocBuilder TLB;
2172 TemplateSpecializationTypeLoc SpecTL
2173 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2174 SpecTL.setTemplateNameLoc(TemplateLoc);
2175 SpecTL.setLAngleLoc(LAngleLoc);
2176 SpecTL.setRAngleLoc(RAngleLoc);
2177 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2178 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002179
Douglas Gregor059101f2011-03-02 00:47:37 +00002180 // Construct an elaborated type containing the nested-name-specifier (if any)
2181 // and keyword.
2182 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2183 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2184 ElabTL.setKeywordLoc(TagLoc);
2185 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2186 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002187}
2188
John McCall60d7b3a2010-08-24 06:29:42 +00002189ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002190 LookupResult &R,
2191 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002192 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002193 // FIXME: Can we do any checking at this point? I guess we could check the
2194 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002195 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002196 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002197 // foo<int> could identify a single function unambiguously
2198 // This approach does NOT work, since f<int>(1);
2199 // gets resolved prior to resorting to overload resolution
2200 // i.e., template<class T> void f(double);
2201 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002202
2203 // These should be filtered out by our callers.
2204 assert(!R.empty() && "empty lookup results when building templateid");
2205 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2206
John McCallc373d482010-01-27 01:50:18 +00002207 // We don't want lookup warnings at this point.
2208 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002209
John McCallf7a1a742009-11-24 19:00:30 +00002210 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002211 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002212 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002213 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002214 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002215 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002216
2217 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002218}
2219
John McCallf7a1a742009-11-24 19:00:30 +00002220// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002221ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002222Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002223 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002224 const TemplateArgumentListInfo &TemplateArgs) {
2225 DeclContext *DC;
2226 if (!(DC = computeDeclContext(SS, false)) ||
2227 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002228 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002229 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002231 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002232 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002233 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2234 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002235
John McCallf7a1a742009-11-24 19:00:30 +00002236 if (R.isAmbiguous())
2237 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002238
John McCallf7a1a742009-11-24 19:00:30 +00002239 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002240 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2241 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002242 return ExprError();
2243 }
2244
2245 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002246 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2247 << (NestedNameSpecifier*) SS.getScopeRep()
2248 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002249 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2250 return ExprError();
2251 }
2252
2253 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002254}
2255
Douglas Gregorc45c2322009-03-31 00:43:58 +00002256/// \brief Form a dependent template name.
2257///
2258/// This action forms a dependent template name given the template
2259/// name and its (presumably dependent) scope specifier. For
2260/// example, given "MetaFun::template apply", the scope specifier \p
2261/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2262/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002263TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002264 SourceLocation TemplateKWLoc,
2265 CXXScopeSpec &SS,
2266 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002267 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002268 bool EnteringContext,
2269 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002270 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2271 Diag(TemplateKWLoc,
2272 getLangOptions().CPlusPlus0x ?
2273 diag::warn_cxx98_compat_template_outside_of_template :
2274 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002275 << FixItHint::CreateRemoval(TemplateKWLoc);
2276
Douglas Gregor0707bc52010-01-19 16:01:07 +00002277 DeclContext *LookupCtx = 0;
2278 if (SS.isSet())
2279 LookupCtx = computeDeclContext(SS, EnteringContext);
2280 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002281 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002282 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002283 // C++0x [temp.names]p5:
2284 // If a name prefixed by the keyword template is not the name of
2285 // a template, the program is ill-formed. [Note: the keyword
2286 // template may not be applied to non-template members of class
2287 // templates. -end note ] [ Note: as is the case with the
2288 // typename prefix, the template prefix is allowed in cases
2289 // where it is not strictly necessary; i.e., when the
2290 // nested-name-specifier or the expression on the left of the ->
2291 // or . is not dependent on a template-parameter, or the use
2292 // does not appear in the scope of a template. -end note]
2293 //
2294 // Note: C++03 was more strict here, because it banned the use of
2295 // the "template" keyword prior to a template-name that was not a
2296 // dependent name. C++ DR468 relaxed this requirement (the
2297 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002298 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002299 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002300 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2301 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002302 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002303 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2304 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002305 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2306 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002307 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002308 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002309 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002310 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002311 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002312 << Name.getSourceRange()
2313 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002314 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002315 } else {
2316 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002317 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002318 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002319 }
2320
Mike Stump1eb44332009-09-09 15:08:12 +00002321 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002322 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002323
Douglas Gregor014e88d2009-11-03 23:16:33 +00002324 switch (Name.getKind()) {
2325 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002326 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002327 Name.Identifier));
2328 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002329
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002330 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002331 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002332 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002333 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002334
2335 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002336 llvm_unreachable(
2337 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002338
Douglas Gregor014e88d2009-11-03 23:16:33 +00002339 default:
2340 break;
2341 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342
2343 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002344 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002345 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002346 << Name.getSourceRange()
2347 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002348 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002349}
2350
Mike Stump1eb44332009-09-09 15:08:12 +00002351bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002352 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002353 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002354 const TemplateArgument &Arg = AL.getArgument();
2355
Anders Carlsson436b1562009-06-13 00:33:33 +00002356 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002357 switch(Arg.getKind()) {
2358 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002359 // C++ [temp.arg.type]p1:
2360 // A template-argument for a template-parameter which is a
2361 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002362 break;
2363 case TemplateArgument::Template: {
2364 // We have a template type parameter but the template argument
2365 // is a template without any arguments.
2366 SourceRange SR = AL.getSourceRange();
2367 TemplateName Name = Arg.getAsTemplate();
2368 Diag(SR.getBegin(), diag::err_template_missing_args)
2369 << Name << SR;
2370 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2371 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002372
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002373 return true;
2374 }
2375 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002376 // We have a template type parameter but the template argument
2377 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002378 SourceRange SR = AL.getSourceRange();
2379 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002380 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Anders Carlsson436b1562009-06-13 00:33:33 +00002382 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002383 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002384 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002385
John McCalla93c9342009-12-07 02:54:59 +00002386 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002387 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002388
Anders Carlsson436b1562009-06-13 00:33:33 +00002389 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002390 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2391
2392 // Objective-C ARC:
2393 // If an explicitly-specified template argument type is a lifetime type
2394 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2395 if (getLangOptions().ObjCAutoRefCount &&
2396 ArgType->isObjCLifetimeType() &&
2397 !ArgType.getObjCLifetime()) {
2398 Qualifiers Qs;
2399 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2400 ArgType = Context.getQualifiedType(ArgType, Qs);
2401 }
2402
2403 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002404 return false;
2405}
2406
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002407/// \brief Substitute template arguments into the default template argument for
2408/// the given template type parameter.
2409///
2410/// \param SemaRef the semantic analysis object for which we are performing
2411/// the substitution.
2412///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002413/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002414/// for.
2415///
2416/// \param TemplateLoc the location of the template name that started the
2417/// template-id we are checking.
2418///
2419/// \param RAngleLoc the location of the right angle bracket ('>') that
2420/// terminates the template-id.
2421///
2422/// \param Param the template template parameter whose default we are
2423/// substituting into.
2424///
2425/// \param Converted the list of template arguments provided for template
2426/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002427/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002428static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002429SubstDefaultTemplateArgument(Sema &SemaRef,
2430 TemplateDecl *Template,
2431 SourceLocation TemplateLoc,
2432 SourceLocation RAngleLoc,
2433 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002434 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002435 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002436
2437 // If the argument type is dependent, instantiate it now based
2438 // on the previously-computed template arguments.
2439 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002440 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002441 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002442
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002443 MultiLevelTemplateArgumentList AllTemplateArgs
2444 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2445
2446 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002447 Template, Converted.data(),
2448 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002449 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002450
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002451 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2452 Param->getDefaultArgumentLoc(),
2453 Param->getDeclName());
2454 }
2455
2456 return ArgType;
2457}
2458
2459/// \brief Substitute template arguments into the default template argument for
2460/// the given non-type template parameter.
2461///
2462/// \param SemaRef the semantic analysis object for which we are performing
2463/// the substitution.
2464///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002465/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002466/// for.
2467///
2468/// \param TemplateLoc the location of the template name that started the
2469/// template-id we are checking.
2470///
2471/// \param RAngleLoc the location of the right angle bracket ('>') that
2472/// terminates the template-id.
2473///
Douglas Gregor788cd062009-11-11 01:00:40 +00002474/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002475/// substituting into.
2476///
2477/// \param Converted the list of template arguments provided for template
2478/// parameters that precede \p Param in the template parameter list.
2479///
2480/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002481static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002482SubstDefaultTemplateArgument(Sema &SemaRef,
2483 TemplateDecl *Template,
2484 SourceLocation TemplateLoc,
2485 SourceLocation RAngleLoc,
2486 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002487 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002488 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002489 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002490
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002491 MultiLevelTemplateArgumentList AllTemplateArgs
2492 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002493
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002494 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002495 Template, Converted.data(),
2496 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002497 SourceRange(TemplateLoc, RAngleLoc));
2498
2499 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2500}
2501
Douglas Gregor788cd062009-11-11 01:00:40 +00002502/// \brief Substitute template arguments into the default template argument for
2503/// the given template template parameter.
2504///
2505/// \param SemaRef the semantic analysis object for which we are performing
2506/// the substitution.
2507///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002508/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002509/// for.
2510///
2511/// \param TemplateLoc the location of the template name that started the
2512/// template-id we are checking.
2513///
2514/// \param RAngleLoc the location of the right angle bracket ('>') that
2515/// terminates the template-id.
2516///
2517/// \param Param the template template parameter whose default we are
2518/// substituting into.
2519///
2520/// \param Converted the list of template arguments provided for template
2521/// parameters that precede \p Param in the template parameter list.
2522///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002523/// \param QualifierLoc Will be set to the nested-name-specifier (with
2524/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002525///
Douglas Gregor788cd062009-11-11 01:00:40 +00002526/// \returns the substituted template argument, or NULL if an error occurred.
2527static TemplateName
2528SubstDefaultTemplateArgument(Sema &SemaRef,
2529 TemplateDecl *Template,
2530 SourceLocation TemplateLoc,
2531 SourceLocation RAngleLoc,
2532 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002533 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002534 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002536 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002537
Douglas Gregor788cd062009-11-11 01:00:40 +00002538 MultiLevelTemplateArgumentList AllTemplateArgs
2539 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002540
Douglas Gregor788cd062009-11-11 01:00:40 +00002541 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002542 Template, Converted.data(),
2543 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002544 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002545
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002546 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002547 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002548 if (QualifierLoc) {
2549 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2550 AllTemplateArgs);
2551 if (!QualifierLoc)
2552 return TemplateName();
2553 }
2554
Douglas Gregor1d752d72011-03-02 18:46:51 +00002555 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002556 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002557 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002558 AllTemplateArgs);
2559}
2560
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002561/// \brief If the given template parameter has a default template
2562/// argument, substitute into that default template argument and
2563/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002564TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002565Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2566 SourceLocation TemplateLoc,
2567 SourceLocation RAngleLoc,
2568 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002569 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002570 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002571 if (!TypeParm->hasDefaultArgument())
2572 return TemplateArgumentLoc();
2573
John McCalla93c9342009-12-07 02:54:59 +00002574 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002575 TemplateLoc,
2576 RAngleLoc,
2577 TypeParm,
2578 Converted);
2579 if (DI)
2580 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2581
2582 return TemplateArgumentLoc();
2583 }
2584
2585 if (NonTypeTemplateParmDecl *NonTypeParm
2586 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2587 if (!NonTypeParm->hasDefaultArgument())
2588 return TemplateArgumentLoc();
2589
John McCall60d7b3a2010-08-24 06:29:42 +00002590 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002591 TemplateLoc,
2592 RAngleLoc,
2593 NonTypeParm,
2594 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002595 if (Arg.isInvalid())
2596 return TemplateArgumentLoc();
2597
2598 Expr *ArgE = Arg.takeAs<Expr>();
2599 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2600 }
2601
2602 TemplateTemplateParmDecl *TempTempParm
2603 = cast<TemplateTemplateParmDecl>(Param);
2604 if (!TempTempParm->hasDefaultArgument())
2605 return TemplateArgumentLoc();
2606
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002607
Douglas Gregor1d752d72011-03-02 18:46:51 +00002608 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002609 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002610 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002611 RAngleLoc,
2612 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002613 Converted,
2614 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002615 if (TName.isNull())
2616 return TemplateArgumentLoc();
2617
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002618 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002619 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002620 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2621}
2622
Douglas Gregore7526412009-11-11 19:31:23 +00002623/// \brief Check that the given template argument corresponds to the given
2624/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002625///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002626/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002627/// checked.
2628///
2629/// \param Arg The template argument.
2630///
2631/// \param Template The template in which the template argument resides.
2632///
2633/// \param TemplateLoc The location of the template name for the template
2634/// whose argument list we're matching.
2635///
2636/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2637/// the template argument list.
2638///
2639/// \param ArgumentPackIndex The index into the argument pack where this
2640/// argument will be placed. Only valid if the parameter is a parameter pack.
2641///
2642/// \param Converted The checked, converted argument will be added to the
2643/// end of this small vector.
2644///
2645/// \param CTAK Describes how we arrived at this particular template argument:
2646/// explicitly written, deduced, etc.
2647///
2648/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002649bool Sema::CheckTemplateArgument(NamedDecl *Param,
2650 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002651 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002652 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002653 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002654 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002655 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002656 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002657 // Check template type parameters.
2658 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002659 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002660
Douglas Gregord9e15302009-11-11 19:41:09 +00002661 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002662 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002663 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002664 // with the template arguments we've seen thus far. But if the
2665 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002666 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002667 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2668 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002669
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002670 if (NTTPType->isDependentType() &&
2671 !isa<TemplateTemplateParmDecl>(Template) &&
2672 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002673 // Do substitution on the type of the non-type template parameter.
2674 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002675 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002676 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002677
2678 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002679 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002680 NTTPType = SubstType(NTTPType,
2681 MultiLevelTemplateArgumentList(TemplateArgs),
2682 NTTP->getLocation(),
2683 NTTP->getDeclName());
2684 // If that worked, check the non-type template parameter type
2685 // for validity.
2686 if (!NTTPType.isNull())
2687 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2688 NTTP->getLocation());
2689 if (NTTPType.isNull())
2690 return true;
2691 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002692
Douglas Gregore7526412009-11-11 19:31:23 +00002693 switch (Arg.getArgument().getKind()) {
2694 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002695 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696
Douglas Gregore7526412009-11-11 19:31:23 +00002697 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002698 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002699 ExprResult Res =
2700 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2701 Result, CTAK);
2702 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002703 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002704
Douglas Gregor910f8002010-11-07 23:05:16 +00002705 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002706 break;
2707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002708
Douglas Gregore7526412009-11-11 19:31:23 +00002709 case TemplateArgument::Declaration:
2710 case TemplateArgument::Integral:
2711 // We've already checked this template argument, so just copy
2712 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002713 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002714 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002715
Douglas Gregore7526412009-11-11 19:31:23 +00002716 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002717 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002718 // We were given a template template argument. It may not be ill-formed;
2719 // see below.
2720 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002721 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2722 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002723 // We have a template argument such as \c T::template X, which we
2724 // parsed as a template template argument. However, since we now
2725 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002726 // template name into an expression.
2727
2728 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2729 Arg.getTemplateNameLoc());
2730
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002731 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002732 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002733 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002734 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002735 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002736
Douglas Gregora7fc9012011-01-05 18:58:31 +00002737 // If we parsed the template argument as a pack expansion, create a
2738 // pack expansion expression.
2739 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002740 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2741 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002742 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002743 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002744
Douglas Gregore7526412009-11-11 19:31:23 +00002745 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002746 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2747 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002748 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002749
Douglas Gregor910f8002010-11-07 23:05:16 +00002750 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002751 break;
2752 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002753
Douglas Gregore7526412009-11-11 19:31:23 +00002754 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002755 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002756 // therefore cannot be a non-type template argument.
2757 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2758 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002759
Douglas Gregore7526412009-11-11 19:31:23 +00002760 Diag(Param->getLocation(), diag::note_template_param_here);
2761 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002762
Douglas Gregore7526412009-11-11 19:31:23 +00002763 case TemplateArgument::Type: {
2764 // We have a non-type template parameter but the template
2765 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002766
Douglas Gregore7526412009-11-11 19:31:23 +00002767 // C++ [temp.arg]p2:
2768 // In a template-argument, an ambiguity between a type-id and
2769 // an expression is resolved to a type-id, regardless of the
2770 // form of the corresponding template-parameter.
2771 //
2772 // We warn specifically about this case, since it can be rather
2773 // confusing for users.
2774 QualType T = Arg.getArgument().getAsType();
2775 SourceRange SR = Arg.getSourceRange();
2776 if (T->isFunctionType())
2777 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2778 else
2779 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2780 Diag(Param->getLocation(), diag::note_template_param_here);
2781 return true;
2782 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002783
Douglas Gregore7526412009-11-11 19:31:23 +00002784 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002785 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002786 break;
2787 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002788
Douglas Gregore7526412009-11-11 19:31:23 +00002789 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002790 }
2791
2792
Douglas Gregore7526412009-11-11 19:31:23 +00002793 // Check template template parameters.
2794 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002795
Douglas Gregore7526412009-11-11 19:31:23 +00002796 // Substitute into the template parameter list of the template
2797 // template parameter, since previously-supplied template arguments
2798 // may appear within the template template parameter.
2799 {
2800 // Set up a template instantiation context.
2801 LocalInstantiationScope Scope(*this);
2802 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002803 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002804 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002805
2806 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002807 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002808 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002809 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002810 MultiLevelTemplateArgumentList(TemplateArgs)));
2811 if (!TempParm)
2812 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002813 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002814
Douglas Gregore7526412009-11-11 19:31:23 +00002815 switch (Arg.getArgument().getKind()) {
2816 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002817 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002818
Douglas Gregore7526412009-11-11 19:31:23 +00002819 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002820 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002821 if (CheckTemplateArgument(TempParm, Arg))
2822 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002823
Douglas Gregor910f8002010-11-07 23:05:16 +00002824 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002825 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002826
Douglas Gregore7526412009-11-11 19:31:23 +00002827 case TemplateArgument::Expression:
2828 case TemplateArgument::Type:
2829 // We have a template template parameter but the template
2830 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002831 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2832 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002833 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002834
Douglas Gregore7526412009-11-11 19:31:23 +00002835 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002836 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002837 "Declaration argument with template template parameter");
2838 break;
2839 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002840 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002841 "Integral argument with template template parameter");
2842 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002843
Douglas Gregore7526412009-11-11 19:31:23 +00002844 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002845 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002846 break;
2847 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002848
Douglas Gregore7526412009-11-11 19:31:23 +00002849 return false;
2850}
2851
Douglas Gregorc15cb382009-02-09 23:23:08 +00002852/// \brief Check that the given template argument list is well-formed
2853/// for specializing the given template.
2854bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2855 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002856 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002857 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002858 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002859 TemplateParameterList *Params = Template->getTemplateParameters();
2860 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002861 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002862 bool Invalid = false;
2863
John McCalld5532b62009-11-23 01:53:49 +00002864 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2865
Mike Stump1eb44332009-09-09 15:08:12 +00002866 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002867 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002869 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002870 (NumArgs < Params->getMinRequiredArguments() &&
2871 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002872 // FIXME: point at either the first arg beyond what we can handle,
2873 // or the '>', depending on whether we have too many or too few
2874 // arguments.
2875 SourceRange Range;
2876 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002877 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002878 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2879 << (NumArgs > NumParams)
2880 << (isa<ClassTemplateDecl>(Template)? 0 :
2881 isa<FunctionTemplateDecl>(Template)? 1 :
2882 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2883 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002884 Diag(Template->getLocation(), diag::note_template_decl_here)
2885 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002886 Invalid = true;
2887 }
Mike Stump1eb44332009-09-09 15:08:12 +00002888
2889 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002890 // [...] The type and form of each template-argument specified in
2891 // a template-id shall match the type and form specified for the
2892 // corresponding parameter declared by the template in its
2893 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002894 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002895 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002896 TemplateParameterList::iterator Param = Params->begin(),
2897 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002898 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002899 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002900 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002901 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002902 // If we have an expanded parameter pack, make sure we don't have too
2903 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002904 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002905 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002906 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002907 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2908 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2909 << true
2910 << (isa<ClassTemplateDecl>(Template)? 0 :
2911 isa<FunctionTemplateDecl>(Template)? 1 :
2912 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2913 << Template;
2914 Diag(Template->getLocation(), diag::note_template_decl_here)
2915 << Params->getSourceRange();
2916 return true;
2917 }
2918 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002919
Douglas Gregorf35f8282009-11-11 21:54:23 +00002920 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002921 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2922 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002923 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002924 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002925
Douglas Gregor14be16b2010-12-20 16:57:52 +00002926 if ((*Param)->isTemplateParameterPack()) {
2927 // The template parameter was a template parameter pack, so take the
2928 // deduced argument and place it on the argument pack. Note that we
2929 // stay on the same template parameter so that we can deduce more
2930 // arguments.
2931 ArgumentPack.push_back(Converted.back());
2932 Converted.pop_back();
2933 } else {
2934 // Move to the next template parameter.
2935 ++Param;
2936 }
2937 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002938 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002939 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002940
Douglas Gregor8735b292011-06-03 02:59:40 +00002941 // If we're checking a partial template argument list, we're done.
2942 if (PartialTemplateArgs) {
2943 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2944 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2945 ArgumentPack.data(),
2946 ArgumentPack.size()));
2947
2948 return Invalid;
2949 }
2950
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002951 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002952 // arguments, just break out now and we'll fill in the argument pack below.
2953 if ((*Param)->isTemplateParameterPack())
2954 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002955
Douglas Gregorf35f8282009-11-11 21:54:23 +00002956 // We have a default template argument that we will use.
2957 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002958
Douglas Gregorf35f8282009-11-11 21:54:23 +00002959 // Retrieve the default template argument from the template
2960 // parameter. For each kind of template parameter, we substitute the
2961 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002962 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002963 // the default argument.
2964 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2965 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002966 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002967 break;
2968 }
2969
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002970 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002971 Template,
2972 TemplateLoc,
2973 RAngleLoc,
2974 TTP,
2975 Converted);
2976 if (!ArgType)
2977 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002978
Douglas Gregorf35f8282009-11-11 21:54:23 +00002979 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2980 ArgType);
2981 } else if (NonTypeTemplateParmDecl *NTTP
2982 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2983 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002984 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002985 break;
2986 }
2987
John McCall60d7b3a2010-08-24 06:29:42 +00002988 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002989 TemplateLoc,
2990 RAngleLoc,
2991 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002992 Converted);
2993 if (E.isInvalid())
2994 return true;
2995
2996 Expr *Ex = E.takeAs<Expr>();
2997 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2998 } else {
2999 TemplateTemplateParmDecl *TempParm
3000 = cast<TemplateTemplateParmDecl>(*Param);
3001
3002 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003003 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00003004 break;
3005 }
3006
Douglas Gregor1d752d72011-03-02 18:46:51 +00003007 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003008 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003009 TemplateLoc,
3010 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003011 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003012 Converted,
3013 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003014 if (Name.isNull())
3015 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003016
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003017 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3018 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003019 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003020
Douglas Gregorf35f8282009-11-11 21:54:23 +00003021 // Introduce an instantiation record that describes where we are using
3022 // the default template argument.
3023 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003024 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003025 SourceRange(TemplateLoc, RAngleLoc));
3026
Douglas Gregorf35f8282009-11-11 21:54:23 +00003027 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003028 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003029 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003030 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031
Douglas Gregor67714232011-03-03 02:41:12 +00003032 // Core issue 150 (assumed resolution): if this is a template template
3033 // parameter, keep track of the default template arguments from the
3034 // template definition.
3035 if (isTemplateTemplateParameter)
3036 TemplateArgs.addArgument(Arg);
3037
Douglas Gregor14be16b2010-12-20 16:57:52 +00003038 // Move to the next template parameter and argument.
3039 ++Param;
3040 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003041 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003042
Douglas Gregor14be16b2010-12-20 16:57:52 +00003043 // Form argument packs for each of the parameter packs remaining.
3044 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003045 // If we're checking a partial list of template arguments, don't fill
3046 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003047
3048 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003049 if (!HasParameterPack)
3050 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003051 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003052 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003053 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003054 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3055 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003056 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003057 ArgumentPack.clear();
3058 }
3059 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003060
Douglas Gregor14be16b2010-12-20 16:57:52 +00003061 ++Param;
3062 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003063
Douglas Gregorc15cb382009-02-09 23:23:08 +00003064 return Invalid;
3065}
3066
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003067namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003068 class UnnamedLocalNoLinkageFinder
3069 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003070 {
3071 Sema &S;
3072 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003073
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003074 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003075
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003076 public:
3077 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3078
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003079 bool Visit(QualType T) {
3080 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003081 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003082
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003083#define TYPE(Class, Parent) \
3084 bool Visit##Class##Type(const Class##Type *);
3085#define ABSTRACT_TYPE(Class, Parent) \
3086 bool Visit##Class##Type(const Class##Type *) { return false; }
3087#define NON_CANONICAL_TYPE(Class, Parent) \
3088 bool Visit##Class##Type(const Class##Type *) { return false; }
3089#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003090
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003091 bool VisitTagDecl(const TagDecl *Tag);
3092 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3093 };
3094}
3095
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003096bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003097 return false;
3098}
3099
3100bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3101 return Visit(T->getElementType());
3102}
3103
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003104bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003105 return Visit(T->getPointeeType());
3106}
3107
3108bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003109 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003110 return Visit(T->getPointeeType());
3111}
3112
3113bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003114 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003115 return Visit(T->getPointeeType());
3116}
3117
3118bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003119 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003120 return Visit(T->getPointeeType());
3121}
3122
3123bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003124 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003125 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3126}
3127
3128bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003129 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003130 return Visit(T->getElementType());
3131}
3132
3133bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003134 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003135 return Visit(T->getElementType());
3136}
3137
3138bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003139 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003140 return Visit(T->getElementType());
3141}
3142
3143bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003144 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003145 return Visit(T->getElementType());
3146}
3147
3148bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003149 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003150 return Visit(T->getElementType());
3151}
3152
3153bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3154 return Visit(T->getElementType());
3155}
3156
3157bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3158 return Visit(T->getElementType());
3159}
3160
3161bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3162 const FunctionProtoType* T) {
3163 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003164 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003165 A != AEnd; ++A) {
3166 if (Visit(*A))
3167 return true;
3168 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003169
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003170 return Visit(T->getResultType());
3171}
3172
3173bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3174 const FunctionNoProtoType* T) {
3175 return Visit(T->getResultType());
3176}
3177
3178bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3179 const UnresolvedUsingType*) {
3180 return false;
3181}
3182
3183bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3184 return false;
3185}
3186
3187bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3188 return Visit(T->getUnderlyingType());
3189}
3190
3191bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3192 return false;
3193}
3194
Sean Huntca63c202011-05-24 22:41:36 +00003195bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3196 const UnaryTransformType*) {
3197 return false;
3198}
3199
Richard Smith34b41d92011-02-20 03:19:35 +00003200bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3201 return Visit(T->getDeducedType());
3202}
3203
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003204bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3205 return VisitTagDecl(T->getDecl());
3206}
3207
3208bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3209 return VisitTagDecl(T->getDecl());
3210}
3211
3212bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3213 const TemplateTypeParmType*) {
3214 return false;
3215}
3216
Douglas Gregorc3069d62011-01-14 02:55:32 +00003217bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3218 const SubstTemplateTypeParmPackType *) {
3219 return false;
3220}
3221
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003222bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3223 const TemplateSpecializationType*) {
3224 return false;
3225}
3226
3227bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3228 const InjectedClassNameType* T) {
3229 return VisitTagDecl(T->getDecl());
3230}
3231
3232bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3233 const DependentNameType* T) {
3234 return VisitNestedNameSpecifier(T->getQualifier());
3235}
3236
3237bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3238 const DependentTemplateSpecializationType* T) {
3239 return VisitNestedNameSpecifier(T->getQualifier());
3240}
3241
Douglas Gregor7536dd52010-12-20 02:24:11 +00003242bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3243 const PackExpansionType* T) {
3244 return Visit(T->getPattern());
3245}
3246
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003247bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3248 return false;
3249}
3250
3251bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3252 const ObjCInterfaceType *) {
3253 return false;
3254}
3255
3256bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3257 const ObjCObjectPointerType *) {
3258 return false;
3259}
3260
Eli Friedmanb001de72011-10-06 23:00:33 +00003261bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3262 return Visit(T->getValueType());
3263}
3264
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003265bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3266 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003267 S.Diag(SR.getBegin(),
3268 S.getLangOptions().CPlusPlus0x ?
3269 diag::warn_cxx98_compat_template_arg_local_type :
3270 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003271 << S.Context.getTypeDeclType(Tag) << SR;
3272 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003273 }
3274
Richard Smith162e1c12011-04-15 14:24:37 +00003275 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003276 S.Diag(SR.getBegin(),
3277 S.getLangOptions().CPlusPlus0x ?
3278 diag::warn_cxx98_compat_template_arg_unnamed_type :
3279 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003280 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3281 return true;
3282 }
3283
3284 return false;
3285}
3286
3287bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3288 NestedNameSpecifier *NNS) {
3289 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3290 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003291
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003292 switch (NNS->getKind()) {
3293 case NestedNameSpecifier::Identifier:
3294 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003295 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003296 case NestedNameSpecifier::Global:
3297 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003298
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003299 case NestedNameSpecifier::TypeSpec:
3300 case NestedNameSpecifier::TypeSpecWithTemplate:
3301 return Visit(QualType(NNS->getAsType(), 0));
3302 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003303 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003304}
3305
3306
Douglas Gregorc15cb382009-02-09 23:23:08 +00003307/// \brief Check a template argument against its corresponding
3308/// template type parameter.
3309///
3310/// This routine implements the semantics of C++ [temp.arg.type]. It
3311/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003312bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003313 TypeSourceInfo *ArgInfo) {
3314 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003315 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003316 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003317
3318 if (Arg->isVariablyModifiedType()) {
3319 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003320 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003321 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003322 }
3323
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003324 // C++03 [temp.arg.type]p2:
3325 // A local type, a type with no linkage, an unnamed type or a type
3326 // compounded from any of these types shall not be used as a
3327 // template-argument for a template type-parameter.
3328 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003329 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003330 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003331 if (LangOpts.CPlusPlus0x ?
3332 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3333 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3334 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3335 SR.getBegin()) != DiagnosticsEngine::Ignored :
3336 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003337 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3338 (void)Finder.Visit(Context.getCanonicalType(Arg));
3339 }
3340
Douglas Gregorc15cb382009-02-09 23:23:08 +00003341 return false;
3342}
3343
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003344/// \brief Checks whether the given template argument is the address
3345/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003346static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003347CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3348 NonTypeTemplateParmDecl *Param,
3349 QualType ParamType,
3350 Expr *ArgIn,
3351 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003352 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003353 Expr *Arg = ArgIn;
3354 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003355
3356 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003357 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003358
3359 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003360 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003361 // A template-argument for a non-type, non-template
3362 // template-parameter shall be one of: [...]
3363 //
3364 // -- the address of an object or function with external
3365 // linkage, including function templates and function
3366 // template-ids but excluding non-static class members,
3367 // expressed as & id-expression where the & is optional if
3368 // the name refers to a function or array, or if the
3369 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003370
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003371 // In C++98/03 mode, give an extension warning on any extra parentheses.
3372 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3373 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003374 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003375 if (!Invalid && !ExtraParens) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003376 S.Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003377 S.getLangOptions().CPlusPlus0x ?
3378 diag::warn_cxx98_compat_template_arg_extra_parens :
3379 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003380 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003381 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003382 }
3383
3384 Arg = Parens->getSubExpr();
3385 }
3386
John McCall91a57552011-07-15 05:09:51 +00003387 while (SubstNonTypeTemplateParmExpr *subst =
3388 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3389 Arg = subst->getReplacement()->IgnoreImpCasts();
3390
Douglas Gregorb7a09262010-04-01 18:32:35 +00003391 bool AddressTaken = false;
3392 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003393 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003394 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003395 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003396 AddressTaken = true;
3397 AddrOpLoc = UnOp->getOperatorLoc();
3398 }
Francois Picheta343a412011-04-29 09:08:14 +00003399 }
John McCall91a57552011-07-15 05:09:51 +00003400
Francois Pichet62ec1f22011-09-17 17:15:52 +00003401 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003402 Converted = TemplateArgument(ArgIn);
3403 return false;
3404 }
3405
3406 while (SubstNonTypeTemplateParmExpr *subst =
3407 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3408 Arg = subst->getReplacement()->IgnoreImpCasts();
3409
3410 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003411 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003412 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3413 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003414 S.Diag(Param->getLocation(), diag::note_template_param_here);
3415 return true;
3416 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003417
3418 // Stop checking the precise nature of the argument if it is value dependent,
3419 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003420 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003421 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003422 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003423 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003424
Douglas Gregorb7a09262010-04-01 18:32:35 +00003425 if (!isa<ValueDecl>(DRE->getDecl())) {
3426 S.Diag(Arg->getSourceRange().getBegin(),
3427 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003428 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003429 S.Diag(Param->getLocation(), diag::note_template_param_here);
3430 return true;
3431 }
3432
3433 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003434
3435 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003436 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3437 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003438 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003439 S.Diag(Param->getLocation(), diag::note_template_param_here);
3440 return true;
3441 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003442
3443 // Cannot refer to non-static member functions
3444 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003445 if (!Method->isStatic()) {
3446 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003447 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003448 S.Diag(Param->getLocation(), diag::note_template_param_here);
3449 return true;
3450 }
Mike Stump1eb44332009-09-09 15:08:12 +00003451
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003452 // Functions must have external linkage.
3453 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003454 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003455 S.Diag(Arg->getSourceRange().getBegin(),
3456 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003457 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003458 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003459 << true;
3460 return true;
3461 }
3462
3463 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003464 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003465
Douglas Gregorb7a09262010-04-01 18:32:35 +00003466 // If the template parameter has pointer type, the function decays.
3467 if (ParamType->isPointerType() && !AddressTaken)
3468 ArgType = S.Context.getPointerType(Func->getType());
3469 else if (AddressTaken && ParamType->isReferenceType()) {
3470 // If we originally had an address-of operator, but the
3471 // parameter has reference type, complain and (if things look
3472 // like they will work) drop the address-of operator.
3473 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3474 ParamType.getNonReferenceType())) {
3475 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3476 << ParamType;
3477 S.Diag(Param->getLocation(), diag::note_template_param_here);
3478 return true;
3479 }
3480
3481 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3482 << ParamType
3483 << FixItHint::CreateRemoval(AddrOpLoc);
3484 S.Diag(Param->getLocation(), diag::note_template_param_here);
3485
3486 ArgType = Func->getType();
3487 }
3488 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003489 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003490 S.Diag(Arg->getSourceRange().getBegin(),
3491 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003492 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003493 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003494 << true;
3495 return true;
3496 }
3497
Douglas Gregorb7a09262010-04-01 18:32:35 +00003498 // A value of reference type is not an object.
3499 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003500 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003501 diag::err_template_arg_reference_var)
3502 << Var->getType() << Arg->getSourceRange();
3503 S.Diag(Param->getLocation(), diag::note_template_param_here);
3504 return true;
3505 }
3506
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003507 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003508 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003509
3510 // If the template parameter has pointer type, we must have taken
3511 // the address of this object.
3512 if (ParamType->isReferenceType()) {
3513 if (AddressTaken) {
3514 // If we originally had an address-of operator, but the
3515 // parameter has reference type, complain and (if things look
3516 // like they will work) drop the address-of operator.
3517 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3518 ParamType.getNonReferenceType())) {
3519 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3520 << ParamType;
3521 S.Diag(Param->getLocation(), diag::note_template_param_here);
3522 return true;
3523 }
3524
3525 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3526 << ParamType
3527 << FixItHint::CreateRemoval(AddrOpLoc);
3528 S.Diag(Param->getLocation(), diag::note_template_param_here);
3529
3530 ArgType = Var->getType();
3531 }
3532 } else if (!AddressTaken && ParamType->isPointerType()) {
3533 if (Var->getType()->isArrayType()) {
3534 // Array-to-pointer decay.
3535 ArgType = S.Context.getArrayDecayedType(Var->getType());
3536 } else {
3537 // If the template parameter has pointer type but the address of
3538 // this object was not taken, complain and (possibly) recover by
3539 // taking the address of the entity.
3540 ArgType = S.Context.getPointerType(Var->getType());
3541 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3542 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3543 << ParamType;
3544 S.Diag(Param->getLocation(), diag::note_template_param_here);
3545 return true;
3546 }
3547
3548 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3549 << ParamType
3550 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3551
3552 S.Diag(Param->getLocation(), diag::note_template_param_here);
3553 }
3554 }
3555 } else {
3556 // We found something else, but we don't know specifically what it is.
3557 S.Diag(Arg->getSourceRange().getBegin(),
3558 diag::err_template_arg_not_object_or_func)
3559 << Arg->getSourceRange();
3560 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3561 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003562 }
Mike Stump1eb44332009-09-09 15:08:12 +00003563
John McCallf85e1932011-06-15 23:02:42 +00003564 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003565 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003566 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003567 S.IsQualificationConversion(ArgType, ParamType, false,
3568 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003569 // For pointer-to-object types, qualification conversions are
3570 // permitted.
3571 } else {
3572 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3573 if (!ParamRef->getPointeeType()->isFunctionType()) {
3574 // C++ [temp.arg.nontype]p5b3:
3575 // For a non-type template-parameter of type reference to
3576 // object, no conversions apply. The type referred to by the
3577 // reference may be more cv-qualified than the (otherwise
3578 // identical) type of the template- argument. The
3579 // template-parameter is bound directly to the
3580 // template-argument, which shall be an lvalue.
3581
3582 // FIXME: Other qualifiers?
3583 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3584 unsigned ArgQuals = ArgType.getCVRQualifiers();
3585
3586 if ((ParamQuals | ArgQuals) != ParamQuals) {
3587 S.Diag(Arg->getSourceRange().getBegin(),
3588 diag::err_template_arg_ref_bind_ignores_quals)
3589 << ParamType << Arg->getType()
3590 << Arg->getSourceRange();
3591 S.Diag(Param->getLocation(), diag::note_template_param_here);
3592 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003593 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003594 }
3595 }
3596
3597 // At this point, the template argument refers to an object or
3598 // function with external linkage. We now need to check whether the
3599 // argument and parameter types are compatible.
3600 if (!S.Context.hasSameUnqualifiedType(ArgType,
3601 ParamType.getNonReferenceType())) {
3602 // We can't perform this conversion or binding.
3603 if (ParamType->isReferenceType())
3604 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003605 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003606 else
3607 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003608 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003609 S.Diag(Param->getLocation(), diag::note_template_param_here);
3610 return true;
3611 }
3612 }
3613
3614 // Create the template argument.
3615 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003616 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003617 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003618}
3619
3620/// \brief Checks whether the given template argument is a pointer to
3621/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003622bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003623 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003624 bool Invalid = false;
3625
3626 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003627 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003628 Arg = Cast->getSubExpr();
3629
3630 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003631 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003632 // A template-argument for a non-type, non-template
3633 // template-parameter shall be one of: [...]
3634 //
3635 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003636 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003637
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003638 // In C++98/03 mode, give an extension warning on any extra parentheses.
3639 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3640 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003641 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003642 if (!Invalid && !ExtraParens) {
Mike Stump1eb44332009-09-09 15:08:12 +00003643 Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003644 getLangOptions().CPlusPlus0x ?
3645 diag::warn_cxx98_compat_template_arg_extra_parens :
3646 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003647 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003648 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003649 }
3650
3651 Arg = Parens->getSubExpr();
3652 }
3653
John McCall91a57552011-07-15 05:09:51 +00003654 while (SubstNonTypeTemplateParmExpr *subst =
3655 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3656 Arg = subst->getReplacement()->IgnoreImpCasts();
3657
Douglas Gregorcaddba02009-11-12 18:38:13 +00003658 // A pointer-to-member constant written &Class::member.
3659 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003660 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003661 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3662 if (DRE && !DRE->getQualifier())
3663 DRE = 0;
3664 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003665 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003666 // A constant of pointer-to-member type.
3667 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3668 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3669 if (VD->getType()->isMemberPointerType()) {
3670 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003671 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003672 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3673 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003674 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003675 else
3676 Converted = TemplateArgument(VD->getCanonicalDecl());
3677 return Invalid;
3678 }
3679 }
3680 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003681
Douglas Gregorcaddba02009-11-12 18:38:13 +00003682 DRE = 0;
3683 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003684
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003685 if (!DRE)
3686 return Diag(Arg->getSourceRange().getBegin(),
3687 diag::err_template_arg_not_pointer_to_member_form)
3688 << Arg->getSourceRange();
3689
3690 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3691 assert((isa<FieldDecl>(DRE->getDecl()) ||
3692 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3693 "Only non-static member pointers can make it here");
3694
3695 // Okay: this is the address of a non-static member, and therefore
3696 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003697 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003698 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003699 else
3700 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003701 return Invalid;
3702 }
3703
3704 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003705 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003706 diag::err_template_arg_not_pointer_to_member_form)
3707 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003708 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003709 diag::note_template_arg_refers_here);
3710 return true;
3711}
3712
Douglas Gregorc15cb382009-02-09 23:23:08 +00003713/// \brief Check a template argument against its corresponding
3714/// non-type template parameter.
3715///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003716/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003717/// If an error occurred, it returns ExprError(); otherwise, it
3718/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003719/// InstantiatedParamType is the type of the non-type template
3720/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003721ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3722 QualType InstantiatedParamType, Expr *Arg,
3723 TemplateArgument &Converted,
3724 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003725 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3726
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003727 // If either the parameter has a dependent type or the argument is
3728 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003729 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3730 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003731 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003732 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003733 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003734
3735 // C++ [temp.arg.nontype]p5:
3736 // The following conversions are performed on each expression used
3737 // as a non-type template-argument. If a non-type
3738 // template-argument cannot be converted to the type of the
3739 // corresponding template-parameter then the program is
3740 // ill-formed.
3741 //
3742 // -- for a non-type template-parameter of integral or
3743 // enumeration type, integral promotions (4.5) and integral
3744 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003745 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003746 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith4f870622011-10-27 22:11:44 +00003747 // FIXME: In C++11, the argument is a converted constant expression of the
3748 // type of the template parameter.
3749 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3750 if (ArgResult.isInvalid())
3751 return ExprError();
3752 Arg = ArgResult.take();
3753
3754 QualType ArgType = Arg->getType();
3755
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003756 // C++ [temp.arg.nontype]p1:
3757 // A template-argument for a non-type, non-template
3758 // template-parameter shall be one of:
3759 //
3760 // -- an integral constant-expression of integral or enumeration
3761 // type; or
3762 // -- the name of a non-type template-parameter; or
3763 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003764 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003765 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003766 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003767 diag::err_template_arg_not_integral_or_enumeral)
3768 << ArgType << Arg->getSourceRange();
3769 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003770 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003771 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003772 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003773 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3774 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003775 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003776 }
3777
Douglas Gregor02024a92010-03-28 02:42:43 +00003778 // From here on out, all we care about are the unqualified forms
3779 // of the parameter and argument types.
3780 ParamType = ParamType.getUnqualifiedType();
3781 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003782
3783 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003784 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003785 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003786 } else if (CTAK == CTAK_Deduced) {
3787 // C++ [temp.deduct.type]p17:
3788 // If, in the declaration of a function template with a non-type
3789 // template-parameter, the non-type template- parameter is used
3790 // in an expression in the function parameter-list and, if the
3791 // corresponding template-argument is deduced, the
3792 // template-argument type shall match the type of the
3793 // template-parameter exactly, except that a template-argument
3794 // deduced from an array bound may be of any integral type.
3795 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3796 << ArgType << ParamType;
3797 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003798 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003799 } else if (ParamType->isBooleanType()) {
3800 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003801 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003802 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3803 !ParamType->isEnumeralType()) {
3804 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003805 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003806 } else {
3807 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003808 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003809 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003810 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003811 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003812 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003813 }
3814
Douglas Gregorc7469372011-05-04 21:55:00 +00003815 // Add the value of this argument to the list of converted
3816 // arguments. We use the bitwidth and signedness of the template
3817 // parameter.
3818 if (Arg->isValueDependent()) {
3819 // The argument is value-dependent. Create a new
3820 // TemplateArgument with the converted expression.
3821 Converted = TemplateArgument(Arg);
3822 return Owned(Arg);
3823 }
3824
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003825 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003826 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003827 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003828
Douglas Gregorc7469372011-05-04 21:55:00 +00003829 if (ParamType->isBooleanType()) {
3830 // Value must be zero or one.
3831 Value = Value != 0;
3832 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3833 if (Value.getBitWidth() != AllowedBits)
3834 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003835 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003836 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003837 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003838
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003839 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003840 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003841 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003842 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003843 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003844 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003845
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003846 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003847 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003848 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003849 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3850 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3851 << Arg->getSourceRange();
3852 Diag(Param->getLocation(), diag::note_template_param_here);
3853 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003854
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003855 // Complain if we overflowed the template parameter's type.
3856 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003857 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003858 RequiredBits = OldValue.getActiveBits();
3859 else if (OldValue.isUnsigned())
3860 RequiredBits = OldValue.getActiveBits() + 1;
3861 else
3862 RequiredBits = OldValue.getMinSignedBits();
3863 if (RequiredBits > AllowedBits) {
3864 Diag(Arg->getSourceRange().getBegin(),
3865 diag::warn_template_arg_too_large)
3866 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3867 << Arg->getSourceRange();
3868 Diag(Param->getLocation(), diag::note_template_param_here);
3869 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003870 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003871
John McCall833ca992009-10-29 08:12:44 +00003872 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003873 ParamType->isEnumeralType()
3874 ? Context.getCanonicalType(ParamType)
3875 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003876 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003877 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003878
Richard Smith4f870622011-10-27 22:11:44 +00003879 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00003880 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3881
Douglas Gregorb7a09262010-04-01 18:32:35 +00003882 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3883 // from a template argument of type std::nullptr_t to a non-type
3884 // template parameter of type pointer to object, pointer to
3885 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003886 if (ArgType->isNullPtrType()) {
3887 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3888 Converted = TemplateArgument((NamedDecl *)0);
3889 return Owned(Arg);
3890 }
3891
3892 if (ParamType->isNullPtrType()) {
3893 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3894 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3895 return Owned(Arg);
3896 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003897 }
3898
Douglas Gregorb86b0572009-02-11 01:18:59 +00003899 // Handle pointer-to-function, reference-to-function, and
3900 // pointer-to-member-function all in (roughly) the same way.
3901 if (// -- For a non-type template-parameter of type pointer to
3902 // function, only the function-to-pointer conversion (4.3) is
3903 // applied. If the template-argument represents a set of
3904 // overloaded functions (or a pointer to such), the matching
3905 // function is selected from the set (13.4).
3906 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003907 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003908 // -- For a non-type template-parameter of type reference to
3909 // function, no conversions apply. If the template-argument
3910 // represents a set of overloaded functions, the matching
3911 // function is selected from the set (13.4).
3912 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003913 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003914 // -- For a non-type template-parameter of type pointer to
3915 // member function, no conversions apply. If the
3916 // template-argument represents a set of overloaded member
3917 // functions, the matching member function is selected from
3918 // the set (13.4).
3919 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003920 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003921 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003922
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003923 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003924 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003925 true,
3926 FoundResult)) {
3927 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003928 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003929
3930 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3931 ArgType = Arg->getType();
3932 } else
John Wiegley429bb272011-04-08 18:41:53 +00003933 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003934 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003935
John Wiegley429bb272011-04-08 18:41:53 +00003936 if (!ParamType->isMemberPointerType()) {
3937 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3938 ParamType,
3939 Arg, Converted))
3940 return ExprError();
3941 return Owned(Arg);
3942 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003943
John McCallf85e1932011-06-15 23:02:42 +00003944 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003945 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003946 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00003947 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
3948 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003949 } else if (!Context.hasSameUnqualifiedType(ArgType,
3950 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003951 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003952 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003953 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003954 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003955 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003956 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003957 }
Mike Stump1eb44332009-09-09 15:08:12 +00003958
John Wiegley429bb272011-04-08 18:41:53 +00003959 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3960 return ExprError();
3961 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003962 }
3963
Chris Lattnerfe90de72009-02-20 21:37:53 +00003964 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003965 // -- for a non-type template-parameter of type pointer to
3966 // object, qualification conversions (4.4) and the
3967 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003968 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003969 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003970 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003971
John Wiegley429bb272011-04-08 18:41:53 +00003972 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3973 ParamType,
3974 Arg, Converted))
3975 return ExprError();
3976 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003977 }
Mike Stump1eb44332009-09-09 15:08:12 +00003978
Ted Kremenek6217b802009-07-29 21:53:49 +00003979 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003980 // -- For a non-type template-parameter of type reference to
3981 // object, no conversions apply. The type referred to by the
3982 // reference may be more cv-qualified than the (otherwise
3983 // identical) type of the template-argument. The
3984 // template-parameter is bound directly to the
3985 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003986 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003987 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003988
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003989 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003990 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3991 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003992 true,
3993 FoundResult)) {
3994 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003995 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003996
3997 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3998 ArgType = Arg->getType();
3999 } else
John Wiegley429bb272011-04-08 18:41:53 +00004000 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004001 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004002
John Wiegley429bb272011-04-08 18:41:53 +00004003 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4004 ParamType,
4005 Arg, Converted))
4006 return ExprError();
4007 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004008 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004009
4010 // -- For a non-type template-parameter of type pointer to data
4011 // member, qualification conversions (4.4) are applied.
4012 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4013
John McCallf85e1932011-06-15 23:02:42 +00004014 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004015 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004016 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004017 } else if (IsQualificationConversion(ArgType, ParamType, false,
4018 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004019 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4020 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004021 } else {
4022 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004023 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004024 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004025 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004026 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004027 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004028 }
4029
John Wiegley429bb272011-04-08 18:41:53 +00004030 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4031 return ExprError();
4032 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004033}
4034
4035/// \brief Check a template argument against its corresponding
4036/// template template parameter.
4037///
4038/// This routine implements the semantics of C++ [temp.arg.template].
4039/// It returns true if an error occurred, and false otherwise.
4040bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004041 const TemplateArgumentLoc &Arg) {
4042 TemplateName Name = Arg.getArgument().getAsTemplate();
4043 TemplateDecl *Template = Name.getAsTemplateDecl();
4044 if (!Template) {
4045 // Any dependent template name is fine.
4046 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4047 return false;
4048 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004049
Richard Smith3e4c6c42011-05-05 21:57:07 +00004050 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004051 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004052 // the name of a class template or an alias template, expressed as an
4053 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004054 // primary class templates are considered when matching the
4055 // template template argument with the corresponding parameter;
4056 // partial specializations are not considered even if their
4057 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004058 //
4059 // Note that we also allow template template parameters here, which
4060 // will happen when we are dealing with, e.g., class template
4061 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004062 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004063 !isa<TemplateTemplateParmDecl>(Template) &&
4064 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004065 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004066 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004067 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004068 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004069 << Template;
4070 }
4071
4072 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4073 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004074 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004075 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004076 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004077}
4078
Douglas Gregor02024a92010-03-28 02:42:43 +00004079/// \brief Given a non-type template argument that refers to a
4080/// declaration and the type of its corresponding non-type template
4081/// parameter, produce an expression that properly refers to that
4082/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004083ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004084Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4085 QualType ParamType,
4086 SourceLocation Loc) {
4087 assert(Arg.getKind() == TemplateArgument::Declaration &&
4088 "Only declaration template arguments permitted here");
4089 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4090
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004091 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004092 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4093 // If the value is a class member, we might have a pointer-to-member.
4094 // Determine whether the non-type template template parameter is of
4095 // pointer-to-member type. If so, we need to build an appropriate
4096 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4097 // would refer to the member itself.
4098 if (ParamType->isMemberPointerType()) {
4099 QualType ClassType
4100 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4101 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004102 = NestedNameSpecifier::Create(Context, 0, false,
4103 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004104 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004105 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004106
4107 // The actual value-ness of this is unimportant, but for
4108 // internal consistency's sake, references to instance methods
4109 // are r-values.
4110 ExprValueKind VK = VK_LValue;
4111 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4112 VK = VK_RValue;
4113
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004114 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004115 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004116 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004117 Loc,
4118 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004119 if (RefExpr.isInvalid())
4120 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004121
John McCall2de56d12010-08-25 11:45:40 +00004122 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004123
Douglas Gregorc0c83002010-04-30 21:46:38 +00004124 // We might need to perform a trailing qualification conversion, since
4125 // the element type on the parameter could be more qualified than the
4126 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004127 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004128 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004129 ParamType.getUnqualifiedType(), false,
4130 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004131 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004132
Douglas Gregor02024a92010-03-28 02:42:43 +00004133 assert(!RefExpr.isInvalid() &&
4134 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004135 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004136 return move(RefExpr);
4137 }
4138 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004139
Douglas Gregor02024a92010-03-28 02:42:43 +00004140 QualType T = VD->getType().getNonReferenceType();
4141 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004142 // When the non-type template parameter is a pointer, take the
4143 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004144 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004145 if (RefExpr.isInvalid())
4146 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004147
4148 if (T->isFunctionType() || T->isArrayType()) {
4149 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004150 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4151 if (RefExpr.isInvalid())
4152 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004153
4154 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004155 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004156
Douglas Gregorb7a09262010-04-01 18:32:35 +00004157 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004158 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004159 }
4160
John McCallf89e55a2010-11-18 06:31:45 +00004161 ExprValueKind VK = VK_RValue;
4162
Douglas Gregor02024a92010-03-28 02:42:43 +00004163 // If the non-type template parameter has reference type, qualify the
4164 // resulting declaration reference with the extra qualifiers on the
4165 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004166 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4167 VK = VK_LValue;
4168 T = Context.getQualifiedType(T,
4169 TargetRef->getPointeeType().getQualifiers());
4170 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004171
John McCallf89e55a2010-11-18 06:31:45 +00004172 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004173}
4174
4175/// \brief Construct a new expression that refers to the given
4176/// integral template argument with the given source-location
4177/// information.
4178///
4179/// This routine takes care of the mapping from an integral template
4180/// argument (which may have any integral type) to the appropriate
4181/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004182ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004183Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4184 SourceLocation Loc) {
4185 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004186 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004187 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004188 if (T->isAnyCharacterType()) {
4189 CharacterLiteral::CharacterKind Kind;
4190 if (T->isWideCharType())
4191 Kind = CharacterLiteral::Wide;
4192 else if (T->isChar16Type())
4193 Kind = CharacterLiteral::UTF16;
4194 else if (T->isChar32Type())
4195 Kind = CharacterLiteral::UTF32;
4196 else
4197 Kind = CharacterLiteral::Ascii;
4198
Douglas Gregor02024a92010-03-28 02:42:43 +00004199 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004200 Arg.getAsIntegral()->getZExtValue(),
4201 Kind, T, Loc));
4202 }
4203
Douglas Gregor02024a92010-03-28 02:42:43 +00004204 if (T->isBooleanType())
4205 return Owned(new (Context) CXXBoolLiteralExpr(
4206 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004207 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004208
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004209 if (T->isNullPtrType())
4210 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4211
Chris Lattner223de242011-04-25 20:37:58 +00004212 // If this is an enum type that we're instantiating, we need to use an integer
4213 // type the same size as the enumerator. We don't want to build an
4214 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004215 QualType BT;
4216 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004217 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004218 else
4219 BT = T;
4220
John McCall4e9272d2011-07-15 07:47:58 +00004221 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4222 if (T->isEnumeralType()) {
4223 // FIXME: This is a hack. We need a better way to handle substituted
4224 // non-type template parameters.
4225 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4226 Context.getTrivialTypeSourceInfo(T, Loc),
4227 Loc, Loc);
4228 }
4229
4230 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004231}
4232
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004233/// \brief Match two template parameters within template parameter lists.
4234static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4235 bool Complain,
4236 Sema::TemplateParameterListEqualKind Kind,
4237 SourceLocation TemplateArgLoc) {
4238 // Check the actual kind (type, non-type, template).
4239 if (Old->getKind() != New->getKind()) {
4240 if (Complain) {
4241 unsigned NextDiag = diag::err_template_param_different_kind;
4242 if (TemplateArgLoc.isValid()) {
4243 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4244 NextDiag = diag::note_template_param_different_kind;
4245 }
4246 S.Diag(New->getLocation(), NextDiag)
4247 << (Kind != Sema::TPL_TemplateMatch);
4248 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4249 << (Kind != Sema::TPL_TemplateMatch);
4250 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004251
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004252 return false;
4253 }
4254
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004255 // Check that both are parameter packs are neither are parameter packs.
4256 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004257 // template template parameter, the template template parameter can have
4258 // a parameter pack where the template template argument does not.
4259 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4260 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4261 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004262 if (Complain) {
4263 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4264 if (TemplateArgLoc.isValid()) {
4265 S.Diag(TemplateArgLoc,
4266 diag::err_template_arg_template_params_mismatch);
4267 NextDiag = diag::note_template_parameter_pack_non_pack;
4268 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004269
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004270 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4271 : isa<NonTypeTemplateParmDecl>(New)? 1
4272 : 2;
4273 S.Diag(New->getLocation(), NextDiag)
4274 << ParamKind << New->isParameterPack();
4275 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4276 << ParamKind << Old->isParameterPack();
4277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004278
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004279 return false;
4280 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004281
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004282 // For non-type template parameters, check the type of the parameter.
4283 if (NonTypeTemplateParmDecl *OldNTTP
4284 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4285 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004286
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004287 // If we are matching a template template argument to a template
4288 // template parameter and one of the non-type template parameter types
4289 // is dependent, then we must wait until template instantiation time
4290 // to actually compare the arguments.
4291 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4292 (OldNTTP->getType()->isDependentType() ||
4293 NewNTTP->getType()->isDependentType()))
4294 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004295
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004296 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4297 if (Complain) {
4298 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4299 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004300 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004301 diag::err_template_arg_template_params_mismatch);
4302 NextDiag = diag::note_template_nontype_parm_different_type;
4303 }
4304 S.Diag(NewNTTP->getLocation(), NextDiag)
4305 << NewNTTP->getType()
4306 << (Kind != Sema::TPL_TemplateMatch);
4307 S.Diag(OldNTTP->getLocation(),
4308 diag::note_template_nontype_parm_prev_declaration)
4309 << OldNTTP->getType();
4310 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004311
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004312 return false;
4313 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004314
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004315 return true;
4316 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004317
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004318 // For template template parameters, check the template parameter types.
4319 // The template parameter lists of template template
4320 // parameters must agree.
4321 if (TemplateTemplateParmDecl *OldTTP
4322 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004323 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004324 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4325 OldTTP->getTemplateParameters(),
4326 Complain,
4327 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004328 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004329 : Kind),
4330 TemplateArgLoc);
4331 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004332
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004333 return true;
4334}
Douglas Gregor02024a92010-03-28 02:42:43 +00004335
Douglas Gregora0347822011-01-13 00:08:50 +00004336/// \brief Diagnose a known arity mismatch when comparing template argument
4337/// lists.
4338static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004339void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004340 TemplateParameterList *New,
4341 TemplateParameterList *Old,
4342 Sema::TemplateParameterListEqualKind Kind,
4343 SourceLocation TemplateArgLoc) {
4344 unsigned NextDiag = diag::err_template_param_list_different_arity;
4345 if (TemplateArgLoc.isValid()) {
4346 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4347 NextDiag = diag::note_template_param_list_different_arity;
4348 }
4349 S.Diag(New->getTemplateLoc(), NextDiag)
4350 << (New->size() > Old->size())
4351 << (Kind != Sema::TPL_TemplateMatch)
4352 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4353 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4354 << (Kind != Sema::TPL_TemplateMatch)
4355 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4356}
4357
Douglas Gregorddc29e12009-02-06 22:42:48 +00004358/// \brief Determine whether the given template parameter lists are
4359/// equivalent.
4360///
Mike Stump1eb44332009-09-09 15:08:12 +00004361/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004362/// source code as part of a new template declaration.
4363///
4364/// \param Old The old template parameter list, typically found via
4365/// name lookup of the template declared with this template parameter
4366/// list.
4367///
4368/// \param Complain If true, this routine will produce a diagnostic if
4369/// the template parameter lists are not equivalent.
4370///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004371/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004372///
4373/// \param TemplateArgLoc If this source location is valid, then we
4374/// are actually checking the template parameter list of a template
4375/// argument (New) against the template parameter list of its
4376/// corresponding template template parameter (Old). We produce
4377/// slightly different diagnostics in this scenario.
4378///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004379/// \returns True if the template parameter lists are equal, false
4380/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004381bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004382Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4383 TemplateParameterList *Old,
4384 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004385 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004386 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004387 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4388 if (Complain)
4389 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4390 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004391
4392 return false;
4393 }
4394
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004395 // C++0x [temp.arg.template]p3:
4396 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004397 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004398 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004399 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004400 // template-parameter-list of P. [...]
4401 TemplateParameterList::iterator NewParm = New->begin();
4402 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004403 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004404 OldParmEnd = Old->end();
4405 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004406 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4407 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004408 if (NewParm == NewParmEnd) {
4409 if (Complain)
4410 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4411 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004412
Douglas Gregora0347822011-01-13 00:08:50 +00004413 return false;
4414 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004415
Douglas Gregora0347822011-01-13 00:08:50 +00004416 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4417 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004418 return false;
4419
Douglas Gregora0347822011-01-13 00:08:50 +00004420 ++NewParm;
4421 continue;
4422 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004423
Douglas Gregora0347822011-01-13 00:08:50 +00004424 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004425 // [...] When P's template- parameter-list contains a template parameter
4426 // pack (14.5.3), the template parameter pack will match zero or more
4427 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004428 // template-parameter-list of A with the same type and form as the
4429 // template parameter pack in P (ignoring whether those template
4430 // parameters are template parameter packs).
4431 for (; NewParm != NewParmEnd; ++NewParm) {
4432 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4433 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004434 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004435 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004436 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004437
Douglas Gregora0347822011-01-13 00:08:50 +00004438 // Make sure we exhausted all of the arguments.
4439 if (NewParm != NewParmEnd) {
4440 if (Complain)
4441 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4442 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004443
Douglas Gregora0347822011-01-13 00:08:50 +00004444 return false;
4445 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004446
Douglas Gregorddc29e12009-02-06 22:42:48 +00004447 return true;
4448}
4449
4450/// \brief Check whether a template can be declared within this scope.
4451///
4452/// If the template declaration is valid in this scope, returns
4453/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004454bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004455Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004456 // Find the nearest enclosing declaration scope.
4457 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4458 (S->getFlags() & Scope::TemplateParamScope) != 0)
4459 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004460
Douglas Gregorddc29e12009-02-06 22:42:48 +00004461 // C++ [temp]p2:
4462 // A template-declaration can appear only as a namespace scope or
4463 // class scope declaration.
4464 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004465 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4466 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004467 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004468 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004469
Eli Friedman1503f772009-07-31 01:43:05 +00004470 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004471 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004472
4473 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4474 return false;
4475
Mike Stump1eb44332009-09-09 15:08:12 +00004476 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004477 diag::err_template_outside_namespace_or_class_scope)
4478 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004479}
Douglas Gregorcc636682009-02-17 23:15:12 +00004480
Douglas Gregord5cb8762009-10-07 00:13:32 +00004481/// \brief Determine what kind of template specialization the given declaration
4482/// is.
4483static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4484 if (!D)
4485 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004486
Douglas Gregorf6b11852009-10-08 15:14:33 +00004487 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4488 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004489 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4490 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004491 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4492 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004493
Douglas Gregord5cb8762009-10-07 00:13:32 +00004494 return TSK_Undeclared;
4495}
4496
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004497/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004498/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004499///
Douglas Gregor9302da62009-10-14 23:50:59 +00004500/// This routine determines whether a template specialization can be declared
4501/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004502///
4503/// \param S the semantic analysis object for which this check is being
4504/// performed.
4505///
4506/// \param Specialized the entity being specialized or instantiated, which
4507/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004508/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004509/// member class).
4510///
4511/// \param PrevDecl the previous declaration of this entity, if any.
4512///
4513/// \param Loc the location of the explicit specialization or instantiation of
4514/// this entity.
4515///
4516/// \param IsPartialSpecialization whether this is a partial specialization of
4517/// a class template.
4518///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004519/// \returns true if there was an error that we cannot recover from, false
4520/// otherwise.
4521static bool CheckTemplateSpecializationScope(Sema &S,
4522 NamedDecl *Specialized,
4523 NamedDecl *PrevDecl,
4524 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004525 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004526 // Keep these "kind" numbers in sync with the %select statements in the
4527 // various diagnostics emitted by this routine.
4528 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004529 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004530 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004531 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004532 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004533 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004534 EntityKind = 3;
4535 else if (isa<VarDecl>(Specialized))
4536 EntityKind = 4;
4537 else if (isa<RecordDecl>(Specialized))
4538 EntityKind = 5;
4539 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004540 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4541 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004542 return true;
4543 }
4544
Douglas Gregor88b70942009-02-25 22:02:03 +00004545 // C++ [temp.expl.spec]p2:
4546 // An explicit specialization shall be declared in the namespace
4547 // of which the template is a member, or, for member templates, in
4548 // the namespace of which the enclosing class or enclosing class
4549 // template is a member. An explicit specialization of a member
4550 // function, member class or static data member of a class
4551 // template shall be declared in the namespace of which the class
4552 // template is a member. Such a declaration may also be a
4553 // definition. If the declaration is not a definition, the
4554 // specialization may be defined later in the name- space in which
4555 // the explicit specialization was declared, or in a namespace
4556 // that encloses the one in which the explicit specialization was
4557 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004558 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004559 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004560 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004561 return true;
4562 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004563
Douglas Gregor0a407472009-10-07 17:30:37 +00004564 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004565 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004566 // Do not warn for class scope explicit specialization during
4567 // instantiation, warning was already emitted during pattern
4568 // semantic analysis.
4569 if (!S.ActiveTemplateInstantiations.size())
4570 S.Diag(Loc, diag::ext_function_specialization_in_class)
4571 << Specialized;
4572 } else {
4573 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4574 << Specialized;
4575 return true;
4576 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004577 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004578
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004579 if (S.CurContext->isRecord() &&
4580 !S.CurContext->Equals(Specialized->getDeclContext())) {
4581 // Make sure that we're specializing in the right record context.
4582 // Otherwise, things can go horribly wrong.
4583 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4584 << Specialized;
4585 return true;
4586 }
4587
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004588 // C++ [temp.class.spec]p6:
4589 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004590 // in any namespace scope in which its definition may be defined (14.5.1
4591 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004592 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004593 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004594 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004595 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004596 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004597 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4598 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004599 // C++ [temp.exp.spec]p2:
4600 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004601 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004602 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004603 // An explicit specialization of a member function, member class or
4604 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004605 // namespace of which the class template is a member.
4606 //
4607 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004608 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004609 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004610 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4611 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4612 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4613 assert(!IsCPlusPlus0xExtension &&
4614 "DC encloses TU but isn't in enclosing namespace set");
4615 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004616 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004617 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4618 int Diag;
4619 if (!IsCPlusPlus0xExtension)
4620 Diag = diag::err_template_spec_decl_out_of_scope;
4621 else if (!S.getLangOptions().CPlusPlus0x)
4622 Diag = diag::ext_template_spec_decl_out_of_scope;
4623 else
4624 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4625 S.Diag(Loc, Diag)
4626 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4627 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004628
Douglas Gregor9302da62009-10-14 23:50:59 +00004629 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004630 ComplainedAboutScope =
4631 !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004632 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004633 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004634
4635 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004636 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004637 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004638 // specializations of function templates, static data members, and member
4639 // functions, so we skip the check here for those kinds of entities.
4640 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004641 // Should we refactor that check, so that it occurs later?
4642 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004643 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4644 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004645 if (isa<TranslationUnitDecl>(SpecializedContext))
4646 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4647 << EntityKind << Specialized;
4648 else if (isa<NamespaceDecl>(SpecializedContext))
4649 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4650 << EntityKind << Specialized
4651 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004652
Douglas Gregor9302da62009-10-14 23:50:59 +00004653 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004654 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004655
Douglas Gregord5cb8762009-10-07 00:13:32 +00004656 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004657
Douglas Gregor88b70942009-02-25 22:02:03 +00004658 return false;
4659}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004660
Douglas Gregorbacb9492011-01-03 21:13:47 +00004661/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4662/// that checks non-type template partial specialization arguments.
4663static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4664 NonTypeTemplateParmDecl *Param,
4665 const TemplateArgument *Args,
4666 unsigned NumArgs) {
4667 for (unsigned I = 0; I != NumArgs; ++I) {
4668 if (Args[I].getKind() == TemplateArgument::Pack) {
4669 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004670 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004671 Args[I].pack_size()))
4672 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004673
Douglas Gregore94866f2009-06-12 21:21:02 +00004674 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004676
Douglas Gregorbacb9492011-01-03 21:13:47 +00004677 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004678 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004679 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004680 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004681
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004682 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004683 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4684 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004685
4686 // Strip off any implicit casts we added as part of type checking.
4687 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4688 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004689
Douglas Gregore94866f2009-06-12 21:21:02 +00004690 // C++ [temp.class.spec]p8:
4691 // A non-type argument is non-specialized if it is the name of a
4692 // non-type parameter. All other non-type arguments are
4693 // specialized.
4694 //
4695 // Below, we check the two conditions that only apply to
4696 // specialized non-type arguments, so skip any non-specialized
4697 // arguments.
4698 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004699 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004700 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004701
Douglas Gregore94866f2009-06-12 21:21:02 +00004702 // C++ [temp.class.spec]p9:
4703 // Within the argument list of a class template partial
4704 // specialization, the following restrictions apply:
4705 // -- A partially specialized non-type argument expression
4706 // shall not involve a template parameter of the partial
4707 // specialization except when the argument expression is a
4708 // simple identifier.
4709 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004710 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004711 diag::err_dependent_non_type_arg_in_partial_spec)
4712 << ArgExpr->getSourceRange();
4713 return true;
4714 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004715
Douglas Gregore94866f2009-06-12 21:21:02 +00004716 // -- The type of a template parameter corresponding to a
4717 // specialized non-type argument shall not be dependent on a
4718 // parameter of the specialization.
4719 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004720 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004721 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4722 << Param->getType()
4723 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004724 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004725 return true;
4726 }
4727 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004728
Douglas Gregorbacb9492011-01-03 21:13:47 +00004729 return false;
4730}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004731
Douglas Gregorbacb9492011-01-03 21:13:47 +00004732/// \brief Check the non-type template arguments of a class template
4733/// partial specialization according to C++ [temp.class.spec]p9.
4734///
4735/// \param TemplateParams the template parameters of the primary class
4736/// template.
4737///
4738/// \param TemplateArg the template arguments of the class template
4739/// partial specialization.
4740///
4741/// \returns true if there was an error, false otherwise.
4742static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4743 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004744 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004745 const TemplateArgument *ArgList = TemplateArgs.data();
4746
4747 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4748 NonTypeTemplateParmDecl *Param
4749 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4750 if (!Param)
4751 continue;
4752
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004753 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004754 &ArgList[I], 1))
4755 return true;
4756 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004757
4758 return false;
4759}
4760
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004761/// \brief Retrieve the previous declaration of the given declaration.
4762static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4763 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4764 return VD->getPreviousDeclaration();
4765 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4766 return FD->getPreviousDeclaration();
4767 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4768 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004769 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004770 return TD->getPreviousDeclaration();
4771 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4772 return FTD->getPreviousDeclaration();
4773 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4774 return CTD->getPreviousDeclaration();
4775 return 0;
4776}
4777
John McCalld226f652010-08-21 09:40:31 +00004778DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004779Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4780 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004781 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004782 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004783 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004784 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004785 SourceLocation TemplateNameLoc,
4786 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004787 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004788 SourceLocation RAngleLoc,
4789 AttributeList *Attr,
4790 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004791 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004792
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004793 // NOTE: KWLoc is the location of the tag keyword. This will instead
4794 // store the location of the outermost template keyword in the declaration.
4795 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4796 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4797
Douglas Gregorcc636682009-02-17 23:15:12 +00004798 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004799 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004800 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004801 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4802
4803 if (!ClassTemplate) {
4804 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004805 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004806 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4807 return true;
4808 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004809
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004810 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004811 bool isPartialSpecialization = false;
4812
Douglas Gregor88b70942009-02-25 22:02:03 +00004813 // Check the validity of the template headers that introduce this
4814 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004815 // FIXME: We probably shouldn't complain about these headers for
4816 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004817 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004818 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004819 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4820 TemplateNameLoc,
4821 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004822 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004823 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004824 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004825 isExplicitSpecialization,
4826 Invalid);
4827 if (Invalid)
4828 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004829
Douglas Gregor05396e22009-08-25 17:23:04 +00004830 if (TemplateParams && TemplateParams->size() > 0) {
4831 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004832
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004833 if (TUK == TUK_Friend) {
4834 Diag(KWLoc, diag::err_partial_specialization_friend)
4835 << SourceRange(LAngleLoc, RAngleLoc);
4836 return true;
4837 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004838
Douglas Gregor05396e22009-08-25 17:23:04 +00004839 // C++ [temp.class.spec]p10:
4840 // The template parameter list of a specialization shall not
4841 // contain default template argument values.
4842 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4843 Decl *Param = TemplateParams->getParam(I);
4844 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4845 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004846 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004847 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004848 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004849 }
4850 } else if (NonTypeTemplateParmDecl *NTTP
4851 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4852 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004853 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004854 diag::err_default_arg_in_partial_spec)
4855 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004856 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004857 }
4858 } else {
4859 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004860 if (TTP->hasDefaultArgument()) {
4861 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004862 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004863 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004864 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004865 }
4866 }
4867 }
Douglas Gregora735b202009-10-13 14:39:41 +00004868 } else if (TemplateParams) {
4869 if (TUK == TUK_Friend)
4870 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004871 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004872 SourceRange(TemplateParams->getTemplateLoc(),
4873 TemplateParams->getRAngleLoc()))
4874 << SourceRange(LAngleLoc, RAngleLoc);
4875 else
4876 isExplicitSpecialization = true;
4877 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004878 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004879 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004880 isExplicitSpecialization = true;
4881 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004882
Douglas Gregorcc636682009-02-17 23:15:12 +00004883 // Check that the specialization uses the same tag kind as the
4884 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004885 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4886 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004887 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004888 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004889 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004890 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004891 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004892 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004893 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004894 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004895 diag::note_previous_use);
4896 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4897 }
4898
Douglas Gregor40808ce2009-03-09 23:48:35 +00004899 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004900 TemplateArgumentListInfo TemplateArgs;
4901 TemplateArgs.setLAngleLoc(LAngleLoc);
4902 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004903 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004904
Douglas Gregor925910d2011-01-03 20:35:03 +00004905 // Check for unexpanded parameter packs in any of the template arguments.
4906 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004907 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004908 UPPC_PartialSpecialization))
4909 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004910
Douglas Gregorcc636682009-02-17 23:15:12 +00004911 // Check that the template argument list is well-formed for this
4912 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004913 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004914 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4915 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004916 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004917
Douglas Gregor910f8002010-11-07 23:05:16 +00004918 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004919 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004920
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004921 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004922 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004923 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004924 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004925 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004926 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004927 return true;
4928
Douglas Gregor561f8122011-07-01 01:22:09 +00004929 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004930 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004931 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004932 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004933 TemplateArgs.size(),
4934 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004935 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4936 << ClassTemplate->getDeclName();
4937 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004938 }
4939 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004940
Douglas Gregorcc636682009-02-17 23:15:12 +00004941 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004942 ClassTemplateSpecializationDecl *PrevDecl = 0;
4943
4944 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004945 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004946 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004947 = ClassTemplate->findPartialSpecialization(Converted.data(),
4948 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004949 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004950 else
4951 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004952 = ClassTemplate->findSpecialization(Converted.data(),
4953 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004954
4955 ClassTemplateSpecializationDecl *Specialization = 0;
4956
Douglas Gregor88b70942009-02-25 22:02:03 +00004957 // Check whether we can declare a class template specialization in
4958 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004959 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004960 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4961 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004962 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004963 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004964
Douglas Gregorb88e8882009-07-30 17:40:51 +00004965 // The canonical type
4966 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004967 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004968 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004969 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004970 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004971 // arguments was referenced but not declared, or we're only
4972 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004973 // declaration node as our own, updating its source location and
4974 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004975 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004976 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004977 if (TemplateParameterLists.size() > 0) {
4978 Specialization->setTemplateParameterListsInfo(Context,
4979 TemplateParameterLists.size(),
4980 (TemplateParameterList**) TemplateParameterLists.release());
4981 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004982 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004983 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004984 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004985 // Build the canonical type that describes the converted template
4986 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004987 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4988 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004989 Converted.data(),
4990 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004991
4992 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004993 ClassTemplate->getInjectedClassNameSpecialization())) {
4994 // C++ [temp.class.spec]p9b3:
4995 //
4996 // -- The argument list of the specialization shall not be identical
4997 // to the implicit argument list of the primary template.
4998 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00004999 << (TUK == TUK_Definition)
5000 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00005001 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5002 ClassTemplate->getIdentifier(),
5003 TemplateNameLoc,
5004 Attr,
5005 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005006 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005007 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005008 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005009 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005010
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005011 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005012 ClassTemplatePartialSpecializationDecl *PrevPartial
5013 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005014 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005015 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005016 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005017 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005018 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005019 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005020 TemplateParams,
5021 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005022 Converted.data(),
5023 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005024 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005025 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005026 PrevPartial,
5027 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005028 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005029 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005030 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005031 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005032 (TemplateParameterList**) TemplateParameterLists.release());
5033 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005034
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005035 if (!PrevPartial)
5036 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005037 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005038
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005039 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005040 // template specialization, make a note of that.
5041 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5042 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005043
Douglas Gregor031a5882009-06-13 00:26:55 +00005044 // Check that all of the template parameters of the class template
5045 // partial specialization are deducible from the template
5046 // arguments. If not, this class template partial specialization
5047 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005048 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00005049 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005050 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005051 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005052 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005053 unsigned NumNonDeducible = 0;
5054 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
5055 if (!DeducibleParams[I])
5056 ++NumNonDeducible;
5057
5058 if (NumNonDeducible) {
5059 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5060 << (NumNonDeducible > 1)
5061 << SourceRange(TemplateNameLoc, RAngleLoc);
5062 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5063 if (!DeducibleParams[I]) {
5064 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5065 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005066 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005067 diag::note_partial_spec_unused_parameter)
5068 << Param->getDeclName();
5069 else
Mike Stump1eb44332009-09-09 15:08:12 +00005070 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005071 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005072 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005073 }
5074 }
5075 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005076 } else {
5077 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005078 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005079 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005080 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005081 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005082 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005083 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005084 Converted.data(),
5085 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005086 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005087 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005088 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005089 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005090 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005091 (TemplateParameterList**) TemplateParameterLists.release());
5092 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005093
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005094 if (!PrevDecl)
5095 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005096
5097 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005098 }
5099
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005100 // C++ [temp.expl.spec]p6:
5101 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005102 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005103 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005104 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005105 // use occurs; no diagnostic is required.
5106 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005107 bool Okay = false;
5108 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5109 // Is there any previous explicit specialization declaration?
5110 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5111 Okay = true;
5112 break;
5113 }
5114 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005115
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005116 if (!Okay) {
5117 SourceRange Range(TemplateNameLoc, RAngleLoc);
5118 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5119 << Context.getTypeDeclType(Specialization) << Range;
5120
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005121 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005122 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005123 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005124 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005125 return true;
5126 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005127 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005128
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005129 // If this is not a friend, note that this is an explicit specialization.
5130 if (TUK != TUK_Friend)
5131 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005132
5133 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005134 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005135 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005136 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005137 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005138 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005139 Diag(Def->getLocation(), diag::note_previous_definition);
5140 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005141 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005142 }
5143 }
5144
John McCall7f1b9872010-12-18 03:30:47 +00005145 if (Attr)
5146 ProcessDeclAttributeList(S, Specialization, Attr);
5147
Douglas Gregord023aec2011-09-09 20:53:38 +00005148 if (ModulePrivateLoc.isValid())
5149 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5150 << (isPartialSpecialization? 1 : 0)
5151 << FixItHint::CreateRemoval(ModulePrivateLoc);
5152
Douglas Gregorfc705b82009-02-26 22:19:44 +00005153 // Build the fully-sugared type for this class template
5154 // specialization as the user wrote in the specialization
5155 // itself. This means that we'll pretty-print the type retrieved
5156 // from the specialization's declaration the way that the user
5157 // actually wrote the specialization, rather than formatting the
5158 // name based on the "canonical" representation used to store the
5159 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005160 TypeSourceInfo *WrittenTy
5161 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5162 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005163 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005164 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005165 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005166 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005167 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005168
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005169 // C++ [temp.expl.spec]p9:
5170 // A template explicit specialization is in the scope of the
5171 // namespace in which the template was defined.
5172 //
5173 // We actually implement this paragraph where we set the semantic
5174 // context (in the creation of the ClassTemplateSpecializationDecl),
5175 // but we also maintain the lexical context where the actual
5176 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005177 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005178
Douglas Gregorcc636682009-02-17 23:15:12 +00005179 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005180 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005181 Specialization->startDefinition();
5182
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005183 if (TUK == TUK_Friend) {
5184 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5185 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005186 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005187 /*FIXME:*/KWLoc);
5188 Friend->setAccess(AS_public);
5189 CurContext->addDecl(Friend);
5190 } else {
5191 // Add the specialization into its lexical context, so that it can
5192 // be seen when iterating through the list of declarations in that
5193 // context. However, specializations are not found by name lookup.
5194 CurContext->addDecl(Specialization);
5195 }
John McCalld226f652010-08-21 09:40:31 +00005196 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005197}
Douglas Gregord57959a2009-03-27 23:10:48 +00005198
John McCalld226f652010-08-21 09:40:31 +00005199Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005200 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005201 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005202 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005203}
5204
John McCalld226f652010-08-21 09:40:31 +00005205Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005206 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005207 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005208 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005209 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005210
Douglas Gregor52591bf2009-06-24 00:54:41 +00005211 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005212 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005213 }
Mike Stump1eb44332009-09-09 15:08:12 +00005214
Douglas Gregor52591bf2009-06-24 00:54:41 +00005215 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005216
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005217 D.setFunctionDefinition(true);
John McCalld226f652010-08-21 09:40:31 +00005218 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005219 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005220 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005221 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005222 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005223 FunctionTemplate->getTemplatedDecl());
5224 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5225 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5226 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005227}
5228
John McCall75042392010-02-11 01:33:53 +00005229/// \brief Strips various properties off an implicit instantiation
5230/// that has just been explicitly specialized.
5231static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005232 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005233
5234 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5235 FD->setInlineSpecified(false);
5236 }
5237}
5238
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005239/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005240/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005241/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005242/// new specialization/instantiation will have any effect.
5243///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005244/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005245/// instantiation.
5246///
5247/// \param NewTSK the kind of the new explicit specialization or instantiation.
5248///
5249/// \param PrevDecl the previous declaration of the entity.
5250///
5251/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5252///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005253/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005254/// declaration was instantiated (either implicitly or explicitly).
5255///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005256/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005257/// specialization or instantiation has no effect and should be ignored.
5258///
5259/// \returns true if there was an error that should prevent the introduction of
5260/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005261bool
5262Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5263 TemplateSpecializationKind NewTSK,
5264 NamedDecl *PrevDecl,
5265 TemplateSpecializationKind PrevTSK,
5266 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005267 bool &HasNoEffect) {
5268 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005269
Douglas Gregor454885e2009-10-15 15:54:05 +00005270 switch (NewTSK) {
5271 case TSK_Undeclared:
5272 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005273 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005274
Douglas Gregor454885e2009-10-15 15:54:05 +00005275 case TSK_ExplicitSpecialization:
5276 switch (PrevTSK) {
5277 case TSK_Undeclared:
5278 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005279 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005280 // explicitly specialized or has merely been mentioned without any
5281 // instantiation.
5282 return false;
5283
5284 case TSK_ImplicitInstantiation:
5285 if (PrevPointOfInstantiation.isInvalid()) {
5286 // The declaration itself has not actually been instantiated, so it is
5287 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005288 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005289 return false;
5290 }
5291 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005292
Douglas Gregor454885e2009-10-15 15:54:05 +00005293 case TSK_ExplicitInstantiationDeclaration:
5294 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005295 assert((PrevTSK == TSK_ImplicitInstantiation ||
5296 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005297 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005298
Douglas Gregor454885e2009-10-15 15:54:05 +00005299 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005300 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005301 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005302 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005303 // implicit instantiation to take place, in every translation unit in
5304 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005305 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5306 // Is there any previous explicit specialization declaration?
5307 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5308 return false;
5309 }
5310
Douglas Gregor0d035142009-10-27 18:42:08 +00005311 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005312 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005313 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005314 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005315
Douglas Gregor454885e2009-10-15 15:54:05 +00005316 return true;
5317 }
5318 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005319
Douglas Gregor454885e2009-10-15 15:54:05 +00005320 case TSK_ExplicitInstantiationDeclaration:
5321 switch (PrevTSK) {
5322 case TSK_ExplicitInstantiationDeclaration:
5323 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005324 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005325 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005326
Douglas Gregor454885e2009-10-15 15:54:05 +00005327 case TSK_Undeclared:
5328 case TSK_ImplicitInstantiation:
5329 // We're explicitly instantiating something that may have already been
5330 // implicitly instantiated; that's fine.
5331 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005332
Douglas Gregor454885e2009-10-15 15:54:05 +00005333 case TSK_ExplicitSpecialization:
5334 // C++0x [temp.explicit]p4:
5335 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005336 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005337 // specialization for that template, the explicit instantiation has no
5338 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005339 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005340 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005341
Douglas Gregor454885e2009-10-15 15:54:05 +00005342 case TSK_ExplicitInstantiationDefinition:
5343 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005344 // If an entity is the subject of both an explicit instantiation
5345 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005346 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005347 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005348 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005349 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005350 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005351 assert(PrevPointOfInstantiation.isValid() &&
5352 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005353 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005354 return false;
5355 }
5356 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005357
Douglas Gregor454885e2009-10-15 15:54:05 +00005358 case TSK_ExplicitInstantiationDefinition:
5359 switch (PrevTSK) {
5360 case TSK_Undeclared:
5361 case TSK_ImplicitInstantiation:
5362 // We're explicitly instantiating something that may have already been
5363 // implicitly instantiated; that's fine.
5364 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005365
Douglas Gregor454885e2009-10-15 15:54:05 +00005366 case TSK_ExplicitSpecialization:
5367 // C++ DR 259, C++0x [temp.explicit]p4:
5368 // For a given set of template parameters, if an explicit
5369 // instantiation of a template appears after a declaration of
5370 // an explicit specialization for that template, the explicit
5371 // instantiation has no effect.
5372 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005373 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005374 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005375 // has been explicitly specialized.
Richard Smithebaf0e62011-10-18 20:49:44 +00005376 Diag(NewLoc, getLangOptions().CPlusPlus0x ?
5377 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5378 diag::ext_explicit_instantiation_after_specialization)
5379 << PrevDecl;
5380 Diag(PrevDecl->getLocation(),
5381 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005382 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005383 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005384
Douglas Gregor454885e2009-10-15 15:54:05 +00005385 case TSK_ExplicitInstantiationDeclaration:
5386 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005387 // were previously asked to suppress instantiations. That's fine.
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_ExplicitInstantiationDefinition:
5391 // C++0x [temp.spec]p5:
5392 // For a given template and a given set of template-arguments,
5393 // - an explicit instantiation definition shall appear at most once
5394 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005395 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005396 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005397 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005398 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005399 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005400 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005401 }
5402 break;
5403 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005404
David Blaikieb219cfc2011-09-23 05:06:16 +00005405 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005406}
5407
John McCallaf2094e2010-04-08 09:05:18 +00005408/// \brief Perform semantic analysis for the given dependent function
5409/// template specialization. The only possible way to get a dependent
5410/// function template specialization is with a friend declaration,
5411/// like so:
5412///
5413/// template <class T> void foo(T);
5414/// template <class T> class A {
5415/// friend void foo<>(T);
5416/// };
5417///
5418/// There really isn't any useful analysis we can do here, so we
5419/// just store the information.
5420bool
5421Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5422 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5423 LookupResult &Previous) {
5424 // Remove anything from Previous that isn't a function template in
5425 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005426 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005427 LookupResult::Filter F = Previous.makeFilter();
5428 while (F.hasNext()) {
5429 NamedDecl *D = F.next()->getUnderlyingDecl();
5430 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005431 !FDLookupContext->InEnclosingNamespaceSetOf(
5432 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005433 F.erase();
5434 }
5435 F.done();
5436
5437 // Should this be diagnosed here?
5438 if (Previous.empty()) return true;
5439
5440 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5441 ExplicitTemplateArgs);
5442 return false;
5443}
5444
Abramo Bagnarae03db982010-05-20 15:32:11 +00005445/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005446/// specialization.
5447///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005448/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005449/// explicit function template specialization. On successful completion,
5450/// the function declaration \p FD will become a function template
5451/// specialization.
5452///
5453/// \param FD the function declaration, which will be updated to become a
5454/// function template specialization.
5455///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005456/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5457/// if any. Note that this may be valid info even when 0 arguments are
5458/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5459/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005460///
Francois Pichet59e7c562011-07-08 06:21:47 +00005461/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005462/// this function specialization.
5463bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005464Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005465 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005466 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005467 // The set of function template specializations that could match this
5468 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005469 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005470
Sebastian Redl7a126a42010-08-31 00:36:30 +00005471 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005472 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5473 I != E; ++I) {
5474 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5475 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005476 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005477 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005478 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5479 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005480 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005481
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005482 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005483 // A trailing template-argument can be left unspecified in the
5484 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005485 // provided it can be deduced from the function argument type.
5486 // Perform template argument deduction to determine whether we may be
5487 // specializing this template.
5488 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005489 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005490 FunctionDecl *Specialization = 0;
5491 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005492 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005493 FD->getType(),
5494 Specialization,
5495 Info)) {
5496 // FIXME: Template argument deduction failed; record why it failed, so
5497 // that we can provide nifty diagnostics.
5498 (void)TDK;
5499 continue;
5500 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005501
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005502 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005503 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005504 }
5505 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005506
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005507 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005508 UnresolvedSetIterator Result
5509 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005510 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005511 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005512 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005513 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005514 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005515 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005516 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005517 return true;
John McCallc373d482010-01-27 01:50:18 +00005518
5519 // Ignore access information; it doesn't figure into redeclaration checking.
5520 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005521
5522 FunctionTemplateSpecializationInfo *SpecInfo
5523 = Specialization->getTemplateSpecializationInfo();
5524 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005525
5526 // Note: do not overwrite location info if previous template
5527 // specialization kind was explicit.
5528 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5529 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5530 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005531
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005532 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005533 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005534
5535 // If this is a friend declaration, then we're not really declaring
5536 // an explicit specialization.
5537 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005538
Douglas Gregord5cb8762009-10-07 00:13:32 +00005539 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005540 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005541 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005542 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005543 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005544 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005545 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005546
5547 // C++ [temp.expl.spec]p6:
5548 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005549 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005550 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005551 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005552 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005553 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005554 if (!isFriend &&
5555 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005556 TSK_ExplicitSpecialization,
5557 Specialization,
5558 SpecInfo->getTemplateSpecializationKind(),
5559 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005560 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005561 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005562
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005563 // Mark the prior declaration as an explicit specialization, so that later
5564 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005565 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005566 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005567 MarkUnusedFileScopedDecl(Specialization);
5568 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005569
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005570 // Turn the given function declaration into a function template
5571 // specialization, with the template arguments from the previous
5572 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005573 // Take copies of (semantic and syntactic) template argument lists.
5574 const TemplateArgumentList* TemplArgs = new (Context)
5575 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005576 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005577 TemplArgs, /*InsertPos=*/0,
5578 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005579 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005580 FD->setStorageClass(Specialization->getStorageClass());
5581
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005582 // The "previous declaration" for this function template specialization is
5583 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005584 Previous.clear();
5585 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005586 return false;
5587}
5588
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005589/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005590/// specialization.
5591///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005592/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005593/// explicit member function specialization. On successful completion,
5594/// the function declaration \p FD will become a member function
5595/// specialization.
5596///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005597/// \param Member the member declaration, which will be updated to become a
5598/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005599///
John McCall68263142009-11-18 22:49:29 +00005600/// \param Previous the set of declarations, one of which may be specialized
5601/// by this function specialization; the set will be modified to contain the
5602/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005603bool
John McCall68263142009-11-18 22:49:29 +00005604Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005605 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005606
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005607 // Try to find the member we are instantiating.
5608 NamedDecl *Instantiation = 0;
5609 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005610 MemberSpecializationInfo *MSInfo = 0;
5611
John McCall68263142009-11-18 22:49:29 +00005612 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005613 // Nowhere to look anyway.
5614 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005615 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5616 I != E; ++I) {
5617 NamedDecl *D = (*I)->getUnderlyingDecl();
5618 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005619 if (Context.hasSameType(Function->getType(), Method->getType())) {
5620 Instantiation = Method;
5621 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005622 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005623 break;
5624 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005625 }
5626 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005627 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005628 VarDecl *PrevVar;
5629 if (Previous.isSingleResult() &&
5630 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005631 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005632 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005633 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005634 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005635 }
5636 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005637 CXXRecordDecl *PrevRecord;
5638 if (Previous.isSingleResult() &&
5639 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5640 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005641 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005642 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005643 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005644 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005645
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005646 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005647 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005648 // specializations are always out-of-line, the caller will complain about
5649 // this mismatch later.
5650 return false;
5651 }
John McCall77e8b112010-04-13 20:37:33 +00005652
5653 // If this is a friend, just bail out here before we start turning
5654 // things into explicit specializations.
5655 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5656 // Preserve instantiation information.
5657 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5658 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5659 cast<CXXMethodDecl>(InstantiatedFrom),
5660 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5661 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5662 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5663 cast<CXXRecordDecl>(InstantiatedFrom),
5664 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5665 }
5666
5667 Previous.clear();
5668 Previous.addDecl(Instantiation);
5669 return false;
5670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005671
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005672 // Make sure that this is a specialization of a member.
5673 if (!InstantiatedFrom) {
5674 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5675 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005676 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5677 return true;
5678 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005679
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005680 // C++ [temp.expl.spec]p6:
5681 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005682 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005683 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005684 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005685 // use occurs; no diagnostic is required.
5686 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005687
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005688 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005689 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5690 TSK_ExplicitSpecialization,
5691 Instantiation,
5692 MSInfo->getTemplateSpecializationKind(),
5693 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005694 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005695 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005696
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005697 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005698 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005699 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005700 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005701 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005702 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005703
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005704 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005705 // the original declaration to note that it is an explicit specialization
5706 // (if it was previously an implicit instantiation). This latter step
5707 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005708 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005709 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5710 if (InstantiationFunction->getTemplateSpecializationKind() ==
5711 TSK_ImplicitInstantiation) {
5712 InstantiationFunction->setTemplateSpecializationKind(
5713 TSK_ExplicitSpecialization);
5714 InstantiationFunction->setLocation(Member->getLocation());
5715 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005716
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005717 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5718 cast<CXXMethodDecl>(InstantiatedFrom),
5719 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005720 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005721 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005722 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5723 if (InstantiationVar->getTemplateSpecializationKind() ==
5724 TSK_ImplicitInstantiation) {
5725 InstantiationVar->setTemplateSpecializationKind(
5726 TSK_ExplicitSpecialization);
5727 InstantiationVar->setLocation(Member->getLocation());
5728 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005729
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005730 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5731 cast<VarDecl>(InstantiatedFrom),
5732 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005733 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005734 } else {
5735 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005736 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5737 if (InstantiationClass->getTemplateSpecializationKind() ==
5738 TSK_ImplicitInstantiation) {
5739 InstantiationClass->setTemplateSpecializationKind(
5740 TSK_ExplicitSpecialization);
5741 InstantiationClass->setLocation(Member->getLocation());
5742 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005743
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005744 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005745 cast<CXXRecordDecl>(InstantiatedFrom),
5746 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005747 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005748
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005749 // Save the caller the trouble of having to figure out which declaration
5750 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005751 Previous.clear();
5752 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005753 return false;
5754}
5755
Douglas Gregor558c0322009-10-14 23:41:34 +00005756/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005757///
5758/// \returns true if a serious error occurs, false otherwise.
5759static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005760 SourceLocation InstLoc,
5761 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005762 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5763 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005764
Douglas Gregor669eed82010-07-13 00:10:04 +00005765 if (CurContext->isRecord()) {
5766 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5767 << D;
5768 return true;
5769 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005770
Richard Smith3e2e91e2011-10-18 02:28:33 +00005771 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005772 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005773 // template. If the name declared in the explicit instantiation is an
5774 // unqualified name, the explicit instantiation shall appear in the
5775 // namespace where its template is declared or, if that namespace is inline
5776 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005777 //
5778 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005779 if (WasQualifiedName) {
5780 if (CurContext->Encloses(OrigContext))
5781 return false;
5782 } else {
5783 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5784 return false;
5785 }
5786
5787 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5788 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005789 S.Diag(InstLoc,
5790 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005791 diag::err_explicit_instantiation_out_of_scope :
5792 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005793 << D << NS;
5794 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005795 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005796 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005797 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5798 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5799 << D << NS;
5800 } else
5801 S.Diag(InstLoc,
5802 S.getLangOptions().CPlusPlus0x?
5803 diag::err_explicit_instantiation_must_be_global :
5804 diag::warn_explicit_instantiation_must_be_global_0x)
5805 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005806 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005807 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005808}
5809
5810/// \brief Determine whether the given scope specifier has a template-id in it.
5811static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5812 if (!SS.isSet())
5813 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005814
Richard Smith3e2e91e2011-10-18 02:28:33 +00005815 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005816 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005817 // or a static data member of a class template specialization, the name of
5818 // the class template specialization in the qualified-id for the member
5819 // name shall be a simple-template-id.
5820 //
5821 // C++98 has the same restriction, just worded differently.
5822 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5823 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005824 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005825 if (isa<TemplateSpecializationType>(T))
5826 return true;
5827
5828 return false;
5829}
5830
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005831// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005832DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005833Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005834 SourceLocation ExternLoc,
5835 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005836 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005837 SourceLocation KWLoc,
5838 const CXXScopeSpec &SS,
5839 TemplateTy TemplateD,
5840 SourceLocation TemplateNameLoc,
5841 SourceLocation LAngleLoc,
5842 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005843 SourceLocation RAngleLoc,
5844 AttributeList *Attr) {
5845 // Find the class template we're specializing
5846 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005847 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005848 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5849
5850 // Check that the specialization uses the same tag kind as the
5851 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005852 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5853 assert(Kind != TTK_Enum &&
5854 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005855 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005856 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005857 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005858 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005859 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005860 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005861 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005862 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005863 diag::note_previous_use);
5864 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5865 }
5866
Douglas Gregor558c0322009-10-14 23:41:34 +00005867 // C++0x [temp.explicit]p2:
5868 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005869 // definition and an explicit instantiation declaration. An explicit
5870 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005871 TemplateSpecializationKind TSK
5872 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5873 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005874
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005875 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005876 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005877 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005878
5879 // Check that the template argument list is well-formed for this
5880 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005881 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005882 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5883 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005884 return true;
5885
Douglas Gregor910f8002010-11-07 23:05:16 +00005886 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005887 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005888
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005889 // Find the class template specialization declaration that
5890 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005891 void *InsertPos = 0;
5892 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005893 = ClassTemplate->findSpecialization(Converted.data(),
5894 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005895
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005896 TemplateSpecializationKind PrevDecl_TSK
5897 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5898
Douglas Gregord5cb8762009-10-07 00:13:32 +00005899 // C++0x [temp.explicit]p2:
5900 // [...] An explicit instantiation shall appear in an enclosing
5901 // namespace of its template. [...]
5902 //
5903 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005904 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5905 SS.isSet()))
5906 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005907
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005908 ClassTemplateSpecializationDecl *Specialization = 0;
5909
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005910 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005911 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005912 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005913 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005914 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005915 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005916 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005917
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005918 // Even though HasNoEffect == true means that this explicit instantiation
5919 // has no effect on semantics, we go on to put its syntax in the AST.
5920
5921 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5922 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005923 // Since the only prior class template specialization with these
5924 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005925 // declaration node as our own, updating the source location
5926 // for the template name to reflect our new declaration.
5927 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005928 Specialization = PrevDecl;
5929 Specialization->setLocation(TemplateNameLoc);
5930 PrevDecl = 0;
5931 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005932 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005933
Douglas Gregor52604ab2009-09-11 21:19:12 +00005934 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005935 // Create a new class template specialization declaration node for
5936 // this explicit specialization.
5937 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005938 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005939 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005940 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005941 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005942 Converted.data(),
5943 Converted.size(),
5944 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005945 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005946
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005947 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005948 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005949 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005950 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005951 }
5952
5953 // Build the fully-sugared type for this explicit instantiation as
5954 // the user wrote in the explicit instantiation itself. This means
5955 // that we'll pretty-print the type retrieved from the
5956 // specialization's declaration the way that the user actually wrote
5957 // the explicit instantiation, rather than formatting the name based
5958 // on the "canonical" representation used to store the template
5959 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005960 TypeSourceInfo *WrittenTy
5961 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5962 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005963 Context.getTypeDeclType(Specialization));
5964 Specialization->setTypeAsWritten(WrittenTy);
5965 TemplateArgsIn.release();
5966
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005967 // Set source locations for keywords.
5968 Specialization->setExternLoc(ExternLoc);
5969 Specialization->setTemplateKeywordLoc(TemplateLoc);
5970
5971 // Add the explicit instantiation into its lexical context. However,
5972 // since explicit instantiations are never found by name lookup, we
5973 // just put it into the declaration context directly.
5974 Specialization->setLexicalDeclContext(CurContext);
5975 CurContext->addDecl(Specialization);
5976
5977 // Syntax is now OK, so return if it has no other effect on semantics.
5978 if (HasNoEffect) {
5979 // Set the template specialization kind.
5980 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005981 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005982 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005983
5984 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005985 // A definition of a class template or class member template
5986 // shall be in scope at the point of the explicit instantiation of
5987 // the class template or class member template.
5988 //
5989 // This check comes when we actually try to perform the
5990 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005991 ClassTemplateSpecializationDecl *Def
5992 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005993 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005994 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005995 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005996 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005997 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005998 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5999 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006000
Douglas Gregor0d035142009-10-27 18:42:08 +00006001 // Instantiate the members of this class template specialization.
6002 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006003 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006004 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006005 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6006
6007 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6008 // TSK_ExplicitInstantiationDefinition
6009 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6010 TSK == TSK_ExplicitInstantiationDefinition)
6011 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006012
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006013 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006014 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006015
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006016 // Set the template specialization kind.
6017 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006018 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006019}
6020
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006021// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006022DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006023Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006024 SourceLocation ExternLoc,
6025 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006026 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006027 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006028 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006029 IdentifierInfo *Name,
6030 SourceLocation NameLoc,
6031 AttributeList *Attr) {
6032
Douglas Gregor402abb52009-05-28 23:31:59 +00006033 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006034 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006035 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006036 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006037 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006038 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00006039 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006040 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006041 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6042
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006043 if (!TagD)
6044 return true;
6045
John McCalld226f652010-08-21 09:40:31 +00006046 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006047 if (Tag->isEnum()) {
6048 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6049 << Context.getTypeDeclType(Tag);
6050 return true;
6051 }
6052
Douglas Gregord0c87372009-05-27 17:30:49 +00006053 if (Tag->isInvalidDecl())
6054 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006055
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006056 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6057 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6058 if (!Pattern) {
6059 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6060 << Context.getTypeDeclType(Record);
6061 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6062 return true;
6063 }
6064
Douglas Gregor558c0322009-10-14 23:41:34 +00006065 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006066 // If the explicit instantiation is for a class or member class, the
6067 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006068 // simple-template-id.
6069 //
6070 // C++98 has the same restriction, just worded differently.
6071 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006072 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006073 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006074
Douglas Gregor558c0322009-10-14 23:41:34 +00006075 // C++0x [temp.explicit]p2:
6076 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006077 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006078 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006079 TemplateSpecializationKind TSK
6080 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6081 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006082
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006083 // C++0x [temp.explicit]p2:
6084 // [...] An explicit instantiation shall appear in an enclosing
6085 // namespace of its template. [...]
6086 //
6087 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006088 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006089
Douglas Gregor454885e2009-10-15 15:54:05 +00006090 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006091 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00006092 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00006093 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006094 PrevDecl = Record;
6095 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006096 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006097 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006098 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006099 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006100 PrevDecl,
6101 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006102 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006103 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006104 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006105 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006106 return TagD;
6107 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006108
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006109 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006110 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006111 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006112 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006113 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006114 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006115 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006116 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006117 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006118 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6119 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006120 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6121 << Pattern;
6122 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006123 } else {
6124 if (InstantiateClass(NameLoc, Record, Def,
6125 getTemplateInstantiationArgs(Record),
6126 TSK))
6127 return true;
6128
Douglas Gregor952b0172010-02-11 01:04:33 +00006129 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006130 if (!RecordDef)
6131 return true;
6132 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006133 }
6134
Douglas Gregor0d035142009-10-27 18:42:08 +00006135 // Instantiate all of the members of the class.
6136 InstantiateClassMembers(NameLoc, RecordDef,
6137 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006138
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006139 if (TSK == TSK_ExplicitInstantiationDefinition)
6140 MarkVTableUsed(NameLoc, RecordDef, true);
6141
Mike Stump390b4cc2009-05-16 07:39:55 +00006142 // FIXME: We don't have any representation for explicit instantiations of
6143 // member classes. Such a representation is not needed for compilation, but it
6144 // should be available for clients that want to see all of the declarations in
6145 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006146 return TagD;
6147}
6148
John McCallf312b1e2010-08-26 23:41:50 +00006149DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6150 SourceLocation ExternLoc,
6151 SourceLocation TemplateLoc,
6152 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006153 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006154 // TODO: check if/when DNInfo should replace Name.
6155 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6156 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006157 if (!Name) {
6158 if (!D.isInvalidType())
6159 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6160 diag::err_explicit_instantiation_requires_name)
6161 << D.getDeclSpec().getSourceRange()
6162 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006163
Douglas Gregord5a423b2009-09-25 18:43:00 +00006164 return true;
6165 }
6166
6167 // The scope passed in may not be a decl scope. Zip up the scope tree until
6168 // we find one that is.
6169 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6170 (S->getFlags() & Scope::TemplateParamScope) != 0)
6171 S = S->getParent();
6172
6173 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006174 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6175 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006176 if (R.isNull())
6177 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006178
Douglas Gregore885e182011-05-21 18:53:30 +00006179 // C++ [dcl.stc]p1:
6180 // A storage-class-specifier shall not be specified in [...] an explicit
6181 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006182 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006183 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6184 << Name;
6185 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006186 } else if (D.getDeclSpec().getStorageClassSpec()
6187 != DeclSpec::SCS_unspecified) {
6188 // Complain about then remove the storage class specifier.
6189 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6190 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6191
6192 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006193 }
6194
Douglas Gregor663b5a02009-10-14 20:14:33 +00006195 // C++0x [temp.explicit]p1:
6196 // [...] An explicit instantiation of a function template shall not use the
6197 // inline or constexpr specifiers.
6198 // Presumably, this also applies to member functions of class templates as
6199 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006200 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006201 Diag(D.getDeclSpec().getInlineSpecLoc(),
Richard Smith2dc7ece2011-10-18 03:44:03 +00006202 getLangOptions().CPlusPlus0x ?
6203 diag::err_explicit_instantiation_inline :
6204 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006205 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6206 if (D.getDeclSpec().isConstexprSpecified())
6207 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6208 // not already specified.
6209 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6210 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006211
Douglas Gregor558c0322009-10-14 23:41:34 +00006212 // C++0x [temp.explicit]p2:
6213 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006214 // definition and an explicit instantiation declaration. An explicit
6215 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006216 TemplateSpecializationKind TSK
6217 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6218 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006219
Abramo Bagnara25777432010-08-11 22:01:17 +00006220 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006221 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006222
6223 if (!R->isFunctionType()) {
6224 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006225 // A [...] static data member of a class template can be explicitly
6226 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006227 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006228 if (Previous.isAmbiguous())
6229 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006230
John McCall1bcee0a2009-12-02 08:25:40 +00006231 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006232 if (!Prev || !Prev->isStaticDataMember()) {
6233 // We expect to see a data data member here.
6234 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6235 << Name;
6236 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6237 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006238 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006239 return true;
6240 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006241
Douglas Gregord5a423b2009-09-25 18:43:00 +00006242 if (!Prev->getInstantiatedFromStaticDataMember()) {
6243 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006244 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006245 diag::err_explicit_instantiation_data_member_not_instantiated)
6246 << Prev;
6247 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6248 // FIXME: Can we provide a note showing where this was declared?
6249 return true;
6250 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006251
Douglas Gregor558c0322009-10-14 23:41:34 +00006252 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006253 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006254 // or a static data member of a class template specialization, the name of
6255 // the class template specialization in the qualified-id for the member
6256 // name shall be a simple-template-id.
6257 //
6258 // C++98 has the same restriction, just worded differently.
6259 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006260 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006261 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006262 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006263
Douglas Gregor558c0322009-10-14 23:41:34 +00006264 // Check the scope of this explicit instantiation.
6265 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006266
Douglas Gregor454885e2009-10-15 15:54:05 +00006267 // Verify that it is okay to explicitly instantiate here.
6268 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6269 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006270 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006271 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006272 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006273 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006274 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006275 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006276 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006277 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006278
Douglas Gregord5a423b2009-09-25 18:43:00 +00006279 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006280 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006281 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006282 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006283
Douglas Gregord5a423b2009-09-25 18:43:00 +00006284 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006285 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006286 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006287
6288 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006289 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006290 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006291 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006292 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6293 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006294 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6295 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006296 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6297 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006298 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006299 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006300 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006301 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006302 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006303
Douglas Gregord5a423b2009-09-25 18:43:00 +00006304 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006305 // A [...] function [...] can be explicitly instantiated from its template.
6306 // A member function [...] of a class template can be explicitly
6307 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006308 // template.
John McCallc373d482010-01-27 01:50:18 +00006309 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006310 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6311 P != PEnd; ++P) {
6312 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006313 if (!HasExplicitTemplateArgs) {
6314 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6315 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6316 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006317
John McCallc373d482010-01-27 01:50:18 +00006318 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006319 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6320 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006321 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006322 }
6323 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006324
Douglas Gregord5a423b2009-09-25 18:43:00 +00006325 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6326 if (!FunTmpl)
6327 continue;
6328
John McCall5769d612010-02-08 23:07:23 +00006329 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006330 FunctionDecl *Specialization = 0;
6331 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006332 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006333 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006334 R, Specialization, Info)) {
6335 // FIXME: Keep track of almost-matches?
6336 (void)TDK;
6337 continue;
6338 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006339
John McCallc373d482010-01-27 01:50:18 +00006340 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006341 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006342
Douglas Gregord5a423b2009-09-25 18:43:00 +00006343 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006344 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006345 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006346 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006347 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6348 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6349 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006350
John McCallc373d482010-01-27 01:50:18 +00006351 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006352 return true;
John McCallc373d482010-01-27 01:50:18 +00006353
6354 // Ignore access control bits, we don't need them for redeclaration checking.
6355 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006356
Douglas Gregor0a897e32009-10-15 17:21:20 +00006357 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006358 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006359 diag::err_explicit_instantiation_member_function_not_instantiated)
6360 << Specialization
6361 << (Specialization->getTemplateSpecializationKind() ==
6362 TSK_ExplicitSpecialization);
6363 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6364 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006365 }
6366
Douglas Gregor0a897e32009-10-15 17:21:20 +00006367 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006368 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6369 PrevDecl = Specialization;
6370
Douglas Gregor0a897e32009-10-15 17:21:20 +00006371 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006372 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006373 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006374 PrevDecl,
6375 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006376 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006377 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006378 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006379
Douglas Gregor0a897e32009-10-15 17:21:20 +00006380 // FIXME: We may still want to build some representation of this
6381 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006382 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006383 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006384 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006385
6386 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006387
Douglas Gregor0a897e32009-10-15 17:21:20 +00006388 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006389 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006390
Douglas Gregor558c0322009-10-14 23:41:34 +00006391 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006392 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006393 // or a static data member of a class template specialization, the name of
6394 // the class template specialization in the qualified-id for the member
6395 // name shall be a simple-template-id.
6396 //
6397 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006398 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006399 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006400 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006401 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006402 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006403 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006404 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006405
Douglas Gregor558c0322009-10-14 23:41:34 +00006406 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006407 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006408 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006409 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006410 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006411
Douglas Gregord5a423b2009-09-25 18:43:00 +00006412 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006413 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006414}
6415
John McCallf312b1e2010-08-26 23:41:50 +00006416TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006417Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6418 const CXXScopeSpec &SS, IdentifierInfo *Name,
6419 SourceLocation TagLoc, SourceLocation NameLoc) {
6420 // This has to hold, because SS is expected to be defined.
6421 assert(Name && "Expected a name in a dependent tag");
6422
6423 NestedNameSpecifier *NNS
6424 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6425 if (!NNS)
6426 return true;
6427
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006428 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006429
Douglas Gregor48c89f42010-04-24 16:38:41 +00006430 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6431 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006432 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006433 return true;
6434 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006435
Douglas Gregor059101f2011-03-02 00:47:37 +00006436 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006437 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006438 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6439
6440 // Create type-source location information for this type.
6441 TypeLocBuilder TLB;
6442 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6443 TL.setKeywordLoc(TagLoc);
6444 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6445 TL.setNameLoc(NameLoc);
6446 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006447}
6448
John McCallf312b1e2010-08-26 23:41:50 +00006449TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006450Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6451 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006452 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006453 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006454 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006455
Richard Smithebaf0e62011-10-18 20:49:44 +00006456 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6457 Diag(TypenameLoc,
6458 getLangOptions().CPlusPlus0x ?
6459 diag::warn_cxx98_compat_typename_outside_of_template :
6460 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006461 << FixItHint::CreateRemoval(TypenameLoc);
6462
Douglas Gregor2494dd02011-03-01 01:34:45 +00006463 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006464 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6465 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006466 if (T.isNull())
6467 return true;
John McCall63b43852010-04-29 23:50:39 +00006468
6469 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6470 if (isa<DependentNameType>(T)) {
6471 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006472 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006473 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006474 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006475 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006476 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006477 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006478 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006479 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006480 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006481
John McCallb3d87482010-08-24 05:47:05 +00006482 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006483}
6484
John McCallf312b1e2010-08-26 23:41:50 +00006485TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006486Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6487 const CXXScopeSpec &SS,
6488 SourceLocation TemplateLoc,
6489 TemplateTy TemplateIn,
6490 SourceLocation TemplateNameLoc,
6491 SourceLocation LAngleLoc,
6492 ASTTemplateArgsPtr TemplateArgsIn,
6493 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006494 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6495 Diag(TypenameLoc,
6496 getLangOptions().CPlusPlus0x ?
6497 diag::warn_cxx98_compat_typename_outside_of_template :
6498 diag::ext_typename_outside_of_template)
6499 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006500
6501 // Translate the parser's template argument list in our AST format.
6502 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6503 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6504
6505 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006506 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6507 // Construct a dependent template specialization type.
6508 assert(DTN && "dependent template has non-dependent name?");
6509 assert(DTN->getQualifier()
6510 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6511 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6512 DTN->getQualifier(),
6513 DTN->getIdentifier(),
6514 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006515
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006516 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006517 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006518 DependentTemplateSpecializationTypeLoc SpecTL
6519 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006520 SpecTL.setLAngleLoc(LAngleLoc);
6521 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006522 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006523 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006524 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006525 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6526 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006527 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006528 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006529
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006530 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6531 if (T.isNull())
6532 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006533
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006534 // Provide source-location information for the template specialization
6535 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006536 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006537 TemplateSpecializationTypeLoc SpecTL
6538 = Builder.push<TemplateSpecializationTypeLoc>(T);
6539
6540 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006541 SpecTL.setLAngleLoc(LAngleLoc);
6542 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006543 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006544 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6545 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6546
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006547 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6548 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6549 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006550 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6551
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006552 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6553 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006554}
6555
Douglas Gregora02411e2011-02-27 22:46:49 +00006556
Douglas Gregord57959a2009-03-27 23:10:48 +00006557/// \brief Build the type that describes a C++ typename specifier,
6558/// e.g., "typename T::type".
6559QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006560Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6561 SourceLocation KeywordLoc,
6562 NestedNameSpecifierLoc QualifierLoc,
6563 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006564 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006565 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006566 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006567
John McCall77bb1aa2010-05-01 00:40:08 +00006568 DeclContext *Ctx = computeDeclContext(SS);
6569 if (!Ctx) {
6570 // If the nested-name-specifier is dependent and couldn't be
6571 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006572 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6573 return Context.getDependentNameType(Keyword,
6574 QualifierLoc.getNestedNameSpecifier(),
6575 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006576 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006577
John McCall77bb1aa2010-05-01 00:40:08 +00006578 // If the nested-name-specifier refers to the current instantiation,
6579 // the "typename" keyword itself is superfluous. In C++03, the
6580 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6581 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006582 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006583
John McCall77bb1aa2010-05-01 00:40:08 +00006584 if (RequireCompleteDeclContext(SS, Ctx))
6585 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006586
6587 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006588 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006589 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006590 unsigned DiagID = 0;
6591 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006592 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006593 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006594 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006595 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006596
6597 case LookupResult::FoundUnresolvedValue: {
6598 // We found a using declaration that is a value. Most likely, the using
6599 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006600 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006601 IILoc);
6602 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6603 << Name << Ctx << FullRange;
6604 if (UnresolvedUsingValueDecl *Using
6605 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006606 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006607 Diag(Loc, diag::note_using_value_decl_missing_typename)
6608 << FixItHint::CreateInsertion(Loc, "typename ");
6609 }
6610 }
6611 // Fall through to create a dependent typename type, from which we can recover
6612 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006613
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006614 case LookupResult::NotFoundInCurrentInstantiation:
6615 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006616 return Context.getDependentNameType(Keyword,
6617 QualifierLoc.getNestedNameSpecifier(),
6618 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006619
6620 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006621 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006622 // We found a type. Build an ElaboratedType, since the
6623 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006624 return Context.getElaboratedType(ETK_Typename,
6625 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006626 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006627 }
6628
6629 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006630 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006631 break;
6632
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006633
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006634 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006635 return QualType();
6636
Douglas Gregord57959a2009-03-27 23:10:48 +00006637 case LookupResult::FoundOverloaded:
6638 DiagID = diag::err_typename_nested_not_type;
6639 Referenced = *Result.begin();
6640 break;
6641
John McCall6e247262009-10-10 05:48:19 +00006642 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006643 return QualType();
6644 }
6645
6646 // If we get here, it's because name lookup did not find a
6647 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006648 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006649 IILoc);
6650 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006651 if (Referenced)
6652 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6653 << Name;
6654 return QualType();
6655}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006656
6657namespace {
6658 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006659 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006660 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006661 SourceLocation Loc;
6662 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006663
Douglas Gregor4a959d82009-08-06 16:20:37 +00006664 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006665 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006666
Mike Stump1eb44332009-09-09 15:08:12 +00006667 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006668 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006669 DeclarationName Entity)
6670 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006671 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006672
6673 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006674 /// transformed.
6675 ///
6676 /// For the purposes of type reconstruction, a type has already been
6677 /// transformed if it is NULL or if it is not dependent.
6678 bool AlreadyTransformed(QualType T) {
6679 return T.isNull() || !T->isDependentType();
6680 }
Mike Stump1eb44332009-09-09 15:08:12 +00006681
6682 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006683 /// rebuilt.
6684 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006685
Douglas Gregor4a959d82009-08-06 16:20:37 +00006686 /// \brief Returns the name of the entity whose type is being rebuilt.
6687 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006688
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006689 /// \brief Sets the "base" location and entity when that
6690 /// information is known based on another transformation.
6691 void setBase(SourceLocation Loc, DeclarationName Entity) {
6692 this->Loc = Loc;
6693 this->Entity = Entity;
6694 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006695 };
6696}
6697
Douglas Gregor4a959d82009-08-06 16:20:37 +00006698/// \brief Rebuilds a type within the context of the current instantiation.
6699///
Mike Stump1eb44332009-09-09 15:08:12 +00006700/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006701/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006702/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006703/// partial specialization thereof). This routine will rebuild that type now
6704/// that we have entered the declarator's scope, which may produce different
6705/// canonical types, e.g.,
6706///
6707/// \code
6708/// template<typename T>
6709/// struct X {
6710/// typedef T* pointer;
6711/// pointer data();
6712/// };
6713///
6714/// template<typename T>
6715/// typename X<T>::pointer X<T>::data() { ... }
6716/// \endcode
6717///
Douglas Gregor4714c122010-03-31 17:34:00 +00006718/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006719/// since we do not know that we can look into X<T> when we parsed the type.
6720/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006721/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006722/// as the canonical type of T*, allowing the return types of the out-of-line
6723/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006724TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6725 SourceLocation Loc,
6726 DeclarationName Name) {
6727 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006728 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006729
Douglas Gregor4a959d82009-08-06 16:20:37 +00006730 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6731 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006732}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006733
John McCall60d7b3a2010-08-24 06:29:42 +00006734ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006735 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6736 DeclarationName());
6737 return Rebuilder.TransformExpr(E);
6738}
6739
John McCall63b43852010-04-29 23:50:39 +00006740bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006741 if (SS.isInvalid())
6742 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006743
Douglas Gregor7e384942011-02-25 16:07:42 +00006744 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006745 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6746 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006747 NestedNameSpecifierLoc Rebuilt
6748 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6749 if (!Rebuilt)
6750 return true;
John McCall63b43852010-04-29 23:50:39 +00006751
Douglas Gregor7e384942011-02-25 16:07:42 +00006752 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006753 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006754}
6755
Douglas Gregor20606502011-10-14 15:31:12 +00006756/// \brief Rebuild the template parameters now that we know we're in a current
6757/// instantiation.
6758bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6759 TemplateParameterList *Params) {
6760 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6761 Decl *Param = Params->getParam(I);
6762
6763 // There is nothing to rebuild in a type parameter.
6764 if (isa<TemplateTypeParmDecl>(Param))
6765 continue;
6766
6767 // Rebuild the template parameter list of a template template parameter.
6768 if (TemplateTemplateParmDecl *TTP
6769 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6770 if (RebuildTemplateParamsInCurrentInstantiation(
6771 TTP->getTemplateParameters()))
6772 return true;
6773
6774 continue;
6775 }
6776
6777 // Rebuild the type of a non-type template parameter.
6778 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6779 TypeSourceInfo *NewTSI
6780 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6781 NTTP->getLocation(),
6782 NTTP->getDeclName());
6783 if (!NewTSI)
6784 return true;
6785
6786 if (NewTSI != NTTP->getTypeSourceInfo()) {
6787 NTTP->setTypeSourceInfo(NewTSI);
6788 NTTP->setType(NewTSI->getType());
6789 }
6790 }
6791
6792 return false;
6793}
6794
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006795/// \brief Produces a formatted string that describes the binding of
6796/// template parameters to template arguments.
6797std::string
6798Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6799 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006800 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006801}
6802
6803std::string
6804Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6805 const TemplateArgument *Args,
6806 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006807 llvm::SmallString<128> Str;
6808 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006809
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006810 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006811 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006812
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006813 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006814 if (I >= NumArgs)
6815 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006816
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006817 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006818 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006819 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006820 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006821
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006822 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006823 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006824 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006825 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006826 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006827
Douglas Gregor87dd6972010-12-20 16:52:59 +00006828 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006829 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006830 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006831
6832 Out << ']';
6833 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006834}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006835
6836void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6837 if (!FD)
6838 return;
6839 FD->setLateTemplateParsed(Flag);
6840}
6841
6842bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6843 DeclContext *DC = CurContext;
6844
6845 while (DC) {
6846 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6847 const FunctionDecl *FD = RD->isLocalClass();
6848 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6849 } else if (DC->isTranslationUnit() || DC->isNamespace())
6850 return false;
6851
6852 DC = DC->getParent();
6853 }
6854 return false;
6855}