blob: db60f179216e18ba7cedc41f7b6d9fc3768b7a8e [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.
423bool 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.
427 if (getLangOptions().Microsoft)
428 return false;
429
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);
436 return true;
437}
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 Gregorf57172b2008-12-08 18:40:42 +0000531 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000532 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000533 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000534 }
535
Douglas Gregorddc29e12009-02-06 22:42:48 +0000536 SourceLocation Loc = ParamNameLoc;
537 if (!ParamName)
538 Loc = KeyLoc;
539
Douglas Gregor72c3f312008-12-05 18:15:24 +0000540 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000541 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000542 KeyLoc, Loc, Depth, Position, ParamName,
543 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000544 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000545 if (Invalid)
546 Param->setInvalidDecl();
547
548 if (ParamName) {
549 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000550 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000551 IdResolver.AddDecl(Param);
552 }
553
Douglas Gregor61c4d282011-01-05 15:48:55 +0000554 // C++0x [temp.param]p9:
555 // A default template-argument may be specified for any kind of
556 // template-parameter that is not a template parameter pack.
557 if (DefaultArg && Ellipsis) {
558 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
559 DefaultArg = ParsedType();
560 }
561
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000562 // Handle the default argument, if provided.
563 if (DefaultArg) {
564 TypeSourceInfo *DefaultTInfo;
565 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000567 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000568
Douglas Gregor6f526752010-12-16 08:48:57 +0000569 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000570 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000571 UPPC_DefaultArgument))
572 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000573
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000574 // Check the template argument itself.
575 if (CheckTemplateArgument(Param, DefaultTInfo)) {
576 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000577 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000578 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000579
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000580 Param->setDefaultArgument(DefaultTInfo, false);
581 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000582
John McCalld226f652010-08-21 09:40:31 +0000583 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000584}
585
Douglas Gregor2943aed2009-03-03 04:44:36 +0000586/// \brief Check that the type of a non-type template parameter is
587/// well-formed.
588///
589/// \returns the (possibly-promoted) parameter type if valid;
590/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000591QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000592Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000593 // We don't allow variably-modified types as the type of non-type template
594 // parameters.
595 if (T->isVariablyModifiedType()) {
596 Diag(Loc, diag::err_variably_modified_nontype_template_param)
597 << T;
598 return QualType();
599 }
600
Douglas Gregor2943aed2009-03-03 04:44:36 +0000601 // C++ [temp.param]p4:
602 //
603 // A non-type template-parameter shall have one of the following
604 // (optionally cv-qualified) types:
605 //
606 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000607 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000608 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000609 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000610 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000611 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000612 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000613 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000614 // -- std::nullptr_t.
615 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000616 // If T is a dependent type, we can't do the check now, so we
617 // assume that it is well-formed.
618 T->isDependentType())
619 return T;
620 // C++ [temp.param]p8:
621 //
622 // A non-type template-parameter of type "array of T" or
623 // "function returning T" is adjusted to be of type "pointer to
624 // T" or "pointer to function returning T", respectively.
625 else if (T->isArrayType())
626 // FIXME: Keep the type prior to promotion?
627 return Context.getArrayDecayedType(T);
628 else if (T->isFunctionType())
629 // FIXME: Keep the type prior to promotion?
630 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000631
Douglas Gregor2943aed2009-03-03 04:44:36 +0000632 Diag(Loc, diag::err_template_nontype_parm_bad_type)
633 << T;
634
635 return QualType();
636}
637
John McCalld226f652010-08-21 09:40:31 +0000638Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
639 unsigned Depth,
640 unsigned Position,
641 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000642 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000643 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
644 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000645
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000646 assert(S->isTemplateParamScope() &&
647 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000648 bool Invalid = false;
649
650 IdentifierInfo *ParamName = D.getIdentifier();
651 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000652 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000653 LookupOrdinaryName,
654 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000655 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000656 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000657 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000658 }
659
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000660 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
661 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000662 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000663 Invalid = true;
664 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000665
Douglas Gregor10738d32010-12-23 23:51:58 +0000666 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000667 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000668 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000669 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000670 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000671 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000672 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000673 Param->setAccess(AS_public);
674
Douglas Gregor72c3f312008-12-05 18:15:24 +0000675 if (Invalid)
676 Param->setInvalidDecl();
677
678 if (D.getIdentifier()) {
679 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000680 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000681 IdResolver.AddDecl(Param);
682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683
Douglas Gregor61c4d282011-01-05 15:48:55 +0000684 // C++0x [temp.param]p9:
685 // A default template-argument may be specified for any kind of
686 // template-parameter that is not a template parameter pack.
687 if (Default && IsParameterPack) {
688 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
689 Default = 0;
690 }
691
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000692 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000693 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000694 // Check for unexpanded parameter packs.
695 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
696 return Param;
697
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000698 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000699 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
700 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000701 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000702 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000703 }
John Wiegley429bb272011-04-08 18:41:53 +0000704 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000705
John McCall9ae2f072010-08-23 23:25:46 +0000706 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000708
John McCalld226f652010-08-21 09:40:31 +0000709 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000710}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000711
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000712/// ActOnTemplateTemplateParameter - Called when a C++ template template
713/// parameter (e.g. T in template <template <typename> class T> class array)
714/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000715Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
716 SourceLocation TmpLoc,
717 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000718 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000719 IdentifierInfo *Name,
720 SourceLocation NameLoc,
721 unsigned Depth,
722 unsigned Position,
723 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000724 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000725 assert(S->isTemplateParamScope() &&
726 "Template template parameter not in template parameter scope!");
727
728 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000729 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000730 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000731 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000732 NameLoc.isInvalid()? TmpLoc : NameLoc,
733 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000734 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000735 Param->setAccess(AS_public);
736
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000737 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000738 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000739 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000740 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000741 IdResolver.AddDecl(Param);
742 }
743
Douglas Gregor6f526752010-12-16 08:48:57 +0000744 if (Params->size() == 0) {
745 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
746 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
747 Param->setInvalidDecl();
748 }
749
Douglas Gregor61c4d282011-01-05 15:48:55 +0000750 // C++0x [temp.param]p9:
751 // A default template-argument may be specified for any kind of
752 // template-parameter that is not a template parameter pack.
753 if (IsParameterPack && !Default.isInvalid()) {
754 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
755 Default = ParsedTemplateArgument();
756 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000758 if (!Default.isInvalid()) {
759 // Check only that we have a template template argument. We don't want to
760 // try to check well-formedness now, because our template template parameter
761 // might have dependent types in its template parameters, which we wouldn't
762 // be able to match now.
763 //
764 // If none of the template template parameter's template arguments mention
765 // other template parameters, we could actually perform more checking here.
766 // However, it isn't worth doing.
767 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
768 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
769 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
770 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000771 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000773
Douglas Gregor6f526752010-12-16 08:48:57 +0000774 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000776 DefaultArg.getArgument().getAsTemplate(),
777 UPPC_DefaultArgument))
778 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000779
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000780 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000781 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000782
John McCalld226f652010-08-21 09:40:31 +0000783 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000784}
785
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000786/// ActOnTemplateParameterList - Builds a TemplateParameterList that
787/// contains the template parameters in Params/NumParams.
788Sema::TemplateParamsTy *
789Sema::ActOnTemplateParameterList(unsigned Depth,
790 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000791 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000792 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000793 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000794 SourceLocation RAngleLoc) {
795 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000796 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000797
Douglas Gregorddc29e12009-02-06 22:42:48 +0000798 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000799 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000800 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000801}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000802
John McCallb6217662010-03-15 10:12:16 +0000803static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
804 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000805 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000806}
807
John McCallf312b1e2010-08-26 23:41:50 +0000808DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000809Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000810 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000811 IdentifierInfo *Name, SourceLocation NameLoc,
812 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000813 TemplateParameterList *TemplateParams,
Douglas Gregor8d267c52011-09-09 02:06:17 +0000814 AccessSpecifier AS, bool IsModulePrivate,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000815 unsigned NumOuterTemplateParamLists,
816 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000817 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000818 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000819 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000820 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000821
822 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000823 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000824 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000825
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000826 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
827 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000828
829 // There is no such thing as an unnamed class template.
830 if (!Name) {
831 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000832 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000833 }
834
835 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000836 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000837 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000838 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000839 if (SS.isNotEmpty() && !SS.isInvalid()) {
840 SemanticContext = computeDeclContext(SS, true);
841 if (!SemanticContext) {
842 // FIXME: Produce a reasonable diagnostic here
843 return true;
844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
John McCall77bb1aa2010-05-01 00:40:08 +0000846 if (RequireCompleteDeclContext(SS, SemanticContext))
847 return true;
848
John McCalla24dc2e2009-11-17 02:14:36 +0000849 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000850 } else {
851 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000852 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000853 }
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Douglas Gregor57265e32010-04-12 16:00:01 +0000855 if (Previous.isAmbiguous())
856 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000857
Douglas Gregorddc29e12009-02-06 22:42:48 +0000858 NamedDecl *PrevDecl = 0;
859 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000860 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000861
Douglas Gregorddc29e12009-02-06 22:42:48 +0000862 // If there is a previous declaration with the same name, check
863 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000864 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000865 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000866
867 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000868 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000869 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000870 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000871 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
872 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000873 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000874 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
875 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
876 PrevClassTemplate
877 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
878 ->getSpecializedTemplate();
879 }
880 }
881
John McCall65c49462009-12-18 11:25:59 +0000882 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000883 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884 // [...] When looking for a prior declaration of a class or a function
885 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000886 // function is neither a qualified name nor a template-id, scopes outside
887 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000888 if (!SS.isSet()) {
889 DeclContext *OutermostContext = CurContext;
890 while (!OutermostContext->isFileContext())
891 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000892
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000893 if (PrevDecl &&
894 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
895 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
896 SemanticContext = PrevDecl->getDeclContext();
897 } else {
898 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000899 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000900 // declaration.
901 PrevDecl = PrevClassTemplate = 0;
902 SemanticContext = OutermostContext;
903 }
John McCalle129d442009-12-17 23:21:11 +0000904 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000905
John McCalle129d442009-12-17 23:21:11 +0000906 if (CurContext->isDependentContext()) {
907 // If this is a dependent context, we don't want to link the friend
908 // class template to the template in scope, because that would perform
909 // checking of the template parameter lists that can't be performed
910 // until the outer context is instantiated.
911 PrevDecl = PrevClassTemplate = 0;
912 }
913 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
914 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000915
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 if (PrevClassTemplate) {
917 // Ensure that the template parameter lists are compatible.
918 if (!TemplateParameterListsAreEqual(TemplateParams,
919 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000920 /*Complain=*/true,
921 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000922 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000923
924 // C++ [temp.class]p4:
925 // In a redeclaration, partial specialization, explicit
926 // specialization or explicit instantiation of a class template,
927 // the class-key shall agree in kind with the original class
928 // template declaration (7.1.5.3).
929 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000930 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
931 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000932 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000933 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000934 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000935 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000936 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000937 }
938
Douglas Gregorddc29e12009-02-06 22:42:48 +0000939 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000940 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000941 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000942 Diag(NameLoc, diag::err_redefinition) << Name;
943 Diag(Def->getLocation(), diag::note_previous_definition);
944 // FIXME: Would it make sense to try to "forget" the previous
945 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000946 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000947 }
948 }
949 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
950 // Maybe we will complain about the shadowed template parameter.
951 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
952 // Just pretend that we didn't see the previous declaration.
953 PrevDecl = 0;
954 } else if (PrevDecl) {
955 // C++ [temp]p5:
956 // A class template shall not have the same name as any other
957 // template, class, function, object, enumeration, enumerator,
958 // namespace, or type in the same scope (3.3), except as specified
959 // in (14.5.4).
960 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
961 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000962 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000963 }
964
Douglas Gregord684b002009-02-10 19:49:53 +0000965 // Check the template parameter list of this declaration, possibly
966 // merging in the template parameter list from the previous class
967 // template declaration.
968 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000969 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000970 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000971 SemanticContext->isRecord() &&
972 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000973 ? TPC_ClassTemplateMember
974 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000975 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor57265e32010-04-12 16:00:01 +0000977 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000978 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000979 // template out-of-line.
980 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
981 !(TUK == TUK_Friend && CurContext->isDependentContext()))
982 Diag(NameLoc, diag::err_member_def_does_not_match)
983 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000984 }
985
Mike Stump1eb44332009-09-09 15:08:12 +0000986 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000987 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000988 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000989 PrevClassTemplate->getTemplatedDecl() : 0,
990 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000991 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000992 if (NumOuterTemplateParamLists > 0)
993 NewClass->setTemplateParameterListsInfo(Context,
994 NumOuterTemplateParamLists,
995 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000996
997 ClassTemplateDecl *NewTemplate
998 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
999 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001000 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001001 NewClass->setDescribedClassTemplate(NewTemplate);
1002
Douglas Gregor8d267c52011-09-09 02:06:17 +00001003 if (IsModulePrivate)
1004 NewTemplate->setModulePrivate();
1005
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001006 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001007 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001008 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001009 assert(T->isDependentType() && "Class template type is not dependent?");
1010 (void)T;
1011
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001012 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001013 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001014 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001015 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1016 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001017
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001018 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001019 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001020 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Douglas Gregorddc29e12009-02-06 22:42:48 +00001022 // Set the lexical context of these templates
1023 NewClass->setLexicalDeclContext(CurContext);
1024 NewTemplate->setLexicalDeclContext(CurContext);
1025
John McCall0f434ec2009-07-31 02:45:11 +00001026 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001027 NewClass->startDefinition();
1028
1029 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001030 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001031
John McCall05b23ea2009-09-14 21:59:20 +00001032 if (TUK != TUK_Friend)
1033 PushOnScopeChains(NewTemplate, S);
1034 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001035 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001036 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001037 NewClass->setAccess(PrevClassTemplate->getAccess());
1038 }
John McCall05b23ea2009-09-14 21:59:20 +00001039
Douglas Gregord85bea22009-09-26 06:47:28 +00001040 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1041 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001042
John McCall05b23ea2009-09-14 21:59:20 +00001043 // Friend templates are visible in fairly strange ways.
1044 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001045 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001046 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1047 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1048 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001049 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001050 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001051
Douglas Gregord85bea22009-09-26 06:47:28 +00001052 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1053 NewClass->getLocation(),
1054 NewTemplate,
1055 /*FIXME:*/NewClass->getLocation());
1056 Friend->setAccess(AS_public);
1057 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001058 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001059
Douglas Gregord684b002009-02-10 19:49:53 +00001060 if (Invalid) {
1061 NewTemplate->setInvalidDecl();
1062 NewClass->setInvalidDecl();
1063 }
John McCalld226f652010-08-21 09:40:31 +00001064 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001065}
1066
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001067/// \brief Diagnose the presence of a default template argument on a
1068/// template parameter, which is ill-formed in certain contexts.
1069///
1070/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001071static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001072 Sema::TemplateParamListContext TPC,
1073 SourceLocation ParamLoc,
1074 SourceRange DefArgRange) {
1075 switch (TPC) {
1076 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001077 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001078 return false;
1079
1080 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001081 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001082 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001083 // A default template-argument shall not be specified in a
1084 // function template declaration or a function template
1085 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001086 // If a friend function template declaration specifies a default
1087 // template-argument, that declaration shall be a definition and shall be
1088 // the only declaration of the function template in the translation unit.
1089 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001090 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001091 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001092 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001093 << DefArgRange;
1094 return false;
1095
1096 case Sema::TPC_ClassTemplateMember:
1097 // C++0x [temp.param]p9:
1098 // A default template-argument shall not be specified in the
1099 // template-parameter-lists of the definition of a member of a
1100 // class template that appears outside of the member's class.
1101 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1102 << DefArgRange;
1103 return true;
1104
1105 case Sema::TPC_FriendFunctionTemplate:
1106 // C++ [temp.param]p9:
1107 // A default template-argument shall not be specified in a
1108 // friend template declaration.
1109 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1110 << DefArgRange;
1111 return true;
1112
1113 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1114 // for friend function templates if there is only a single
1115 // declaration (and it is a definition). Strange!
1116 }
1117
1118 return false;
1119}
1120
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001121/// \brief Check for unexpanded parameter packs within the template parameters
1122/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001123static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1124 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001125 TemplateParameterList *Params = TTP->getTemplateParameters();
1126 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1127 NamedDecl *P = Params->getParam(I);
1128 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001129 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001130 NTTP->getTypeSourceInfo(),
1131 Sema::UPPC_NonTypeTemplateParameterType))
1132 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001133
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001134 continue;
1135 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001136
1137 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001138 = dyn_cast<TemplateTemplateParmDecl>(P))
1139 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1140 return true;
1141 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001142
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001143 return false;
1144}
1145
Douglas Gregord684b002009-02-10 19:49:53 +00001146/// \brief Checks the validity of a template parameter list, possibly
1147/// considering the template parameter list from a previous
1148/// declaration.
1149///
1150/// If an "old" template parameter list is provided, it must be
1151/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1152/// template parameter list.
1153///
1154/// \param NewParams Template parameter list for a new template
1155/// declaration. This template parameter list will be updated with any
1156/// default arguments that are carried through from the previous
1157/// template parameter list.
1158///
1159/// \param OldParams If provided, template parameter list from a
1160/// previous declaration of the same template. Default template
1161/// arguments will be merged from the old template parameter list to
1162/// the new template parameter list.
1163///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001164/// \param TPC Describes the context in which we are checking the given
1165/// template parameter list.
1166///
Douglas Gregord684b002009-02-10 19:49:53 +00001167/// \returns true if an error occurred, false otherwise.
1168bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001169 TemplateParameterList *OldParams,
1170 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001171 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Douglas Gregord684b002009-02-10 19:49:53 +00001173 // C++ [temp.param]p10:
1174 // The set of default template-arguments available for use with a
1175 // template declaration or definition is obtained by merging the
1176 // default arguments from the definition (if in scope) and all
1177 // declarations in scope in the same way default function
1178 // arguments are (8.3.6).
1179 bool SawDefaultArgument = false;
1180 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001181
Anders Carlsson49d25572009-06-12 23:20:15 +00001182 bool SawParameterPack = false;
1183 SourceLocation ParameterPackLoc;
1184
Mike Stump1a35fde2009-02-11 23:03:27 +00001185 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001186 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001187 if (OldParams)
1188 OldParam = OldParams->begin();
1189
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001190 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001191 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1192 NewParamEnd = NewParams->end();
1193 NewParam != NewParamEnd; ++NewParam) {
1194 // Variables used to diagnose redundant default arguments
1195 bool RedundantDefaultArg = false;
1196 SourceLocation OldDefaultLoc;
1197 SourceLocation NewDefaultLoc;
1198
1199 // Variables used to diagnose missing default arguments
1200 bool MissingDefaultArg = false;
1201
Anders Carlsson49d25572009-06-12 23:20:15 +00001202 // C++0x [temp.param]p11:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001203 // If a template parameter of a primary class template or alias template
1204 // is a template parameter pack, it shall be the last template parameter.
1205 if (SawParameterPack &&
1206 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001207 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001208 diag::err_template_param_pack_must_be_last_template_parameter);
1209 Invalid = true;
1210 }
1211
Douglas Gregord684b002009-02-10 19:49:53 +00001212 if (TemplateTypeParmDecl *NewTypeParm
1213 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001214 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001215 if (NewTypeParm->hasDefaultArgument() &&
1216 DiagnoseDefaultTemplateArgument(*this, TPC,
1217 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001218 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001219 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001220 NewTypeParm->removeDefaultArgument();
1221
1222 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001223 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001224 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Anders Carlsson49d25572009-06-12 23:20:15 +00001226 if (NewTypeParm->isParameterPack()) {
1227 assert(!NewTypeParm->hasDefaultArgument() &&
1228 "Parameter packs can't have a default argument!");
1229 SawParameterPack = true;
1230 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001231 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001232 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001233 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1234 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1235 SawDefaultArgument = true;
1236 RedundantDefaultArg = true;
1237 PreviousDefaultArgLoc = NewDefaultLoc;
1238 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1239 // Merge the default argument from the old declaration to the
1240 // new declaration.
1241 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001242 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001243 true);
1244 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1245 } else if (NewTypeParm->hasDefaultArgument()) {
1246 SawDefaultArgument = true;
1247 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1248 } else if (SawDefaultArgument)
1249 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001250 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001251 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001252 // Check for unexpanded parameter packs.
1253 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001254 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001255 UPPC_NonTypeTemplateParameterType)) {
1256 Invalid = true;
1257 continue;
1258 }
1259
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001260 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001261 if (NewNonTypeParm->hasDefaultArgument() &&
1262 DiagnoseDefaultTemplateArgument(*this, TPC,
1263 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001264 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001265 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001266 }
1267
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001268 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001269 NonTypeTemplateParmDecl *OldNonTypeParm
1270 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001271 if (NewNonTypeParm->isParameterPack()) {
1272 assert(!NewNonTypeParm->hasDefaultArgument() &&
1273 "Parameter packs can't have a default argument!");
1274 SawParameterPack = true;
1275 ParameterPackLoc = NewNonTypeParm->getLocation();
1276 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001277 NewNonTypeParm->hasDefaultArgument()) {
1278 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1279 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1280 SawDefaultArgument = true;
1281 RedundantDefaultArg = true;
1282 PreviousDefaultArgLoc = NewDefaultLoc;
1283 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1284 // Merge the default argument from the old declaration to the
1285 // new declaration.
1286 SawDefaultArgument = true;
1287 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001288 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001289 // parameter.
1290 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001291 OldNonTypeParm->getDefaultArgument(),
1292 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001293 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1294 } else if (NewNonTypeParm->hasDefaultArgument()) {
1295 SawDefaultArgument = true;
1296 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1297 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001298 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001299 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001300 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001301 TemplateTemplateParmDecl *NewTemplateParm
1302 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001303
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001304 // Check for unexpanded parameter packs, recursively.
1305 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1306 Invalid = true;
1307 continue;
1308 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001309
1310 if (NewTemplateParm->hasDefaultArgument() &&
1311 DiagnoseDefaultTemplateArgument(*this, TPC,
1312 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001313 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001314 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001315
1316 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001317 TemplateTemplateParmDecl *OldTemplateParm
1318 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001319 if (NewTemplateParm->isParameterPack()) {
1320 assert(!NewTemplateParm->hasDefaultArgument() &&
1321 "Parameter packs can't have a default argument!");
1322 SawParameterPack = true;
1323 ParameterPackLoc = NewTemplateParm->getLocation();
1324 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001325 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001326 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1327 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001328 SawDefaultArgument = true;
1329 RedundantDefaultArg = true;
1330 PreviousDefaultArgLoc = NewDefaultLoc;
1331 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1332 // Merge the default argument from the old declaration to the
1333 // new declaration.
1334 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001335 // FIXME: We need to create a new kind of "default argument" expression
1336 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001337 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001338 OldTemplateParm->getDefaultArgument(),
1339 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001340 PreviousDefaultArgLoc
1341 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001342 } else if (NewTemplateParm->hasDefaultArgument()) {
1343 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001344 PreviousDefaultArgLoc
1345 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001346 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001347 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001348 }
1349
1350 if (RedundantDefaultArg) {
1351 // C++ [temp.param]p12:
1352 // A template-parameter shall not be given default arguments
1353 // by two different declarations in the same scope.
1354 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1355 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1356 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001357 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001358 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001359 // If a template-parameter of a class template has a default
1360 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001361 // have a default template-argument supplied or be a template parameter
1362 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001363 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001364 diag::err_template_param_default_arg_missing);
1365 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1366 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001367 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001368 }
1369
1370 // If we have an old template parameter list that we're merging
1371 // in, move on to the next parameter.
1372 if (OldParams)
1373 ++OldParam;
1374 }
1375
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001376 // We were missing some default arguments at the end of the list, so remove
1377 // all of the default arguments.
1378 if (RemoveDefaultArguments) {
1379 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1380 NewParamEnd = NewParams->end();
1381 NewParam != NewParamEnd; ++NewParam) {
1382 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1383 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001384 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001385 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1386 NTTP->removeDefaultArgument();
1387 else
1388 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1389 }
1390 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001391
Douglas Gregord684b002009-02-10 19:49:53 +00001392 return Invalid;
1393}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001394
John McCall4e2cbb22010-10-20 05:44:58 +00001395namespace {
1396
1397/// A class which looks for a use of a certain level of template
1398/// parameter.
1399struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1400 typedef RecursiveASTVisitor<DependencyChecker> super;
1401
1402 unsigned Depth;
1403 bool Match;
1404
1405 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1406 NamedDecl *ND = Params->getParam(0);
1407 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1408 Depth = PD->getDepth();
1409 } else if (NonTypeTemplateParmDecl *PD =
1410 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1411 Depth = PD->getDepth();
1412 } else {
1413 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1414 }
1415 }
1416
1417 bool Matches(unsigned ParmDepth) {
1418 if (ParmDepth >= Depth) {
1419 Match = true;
1420 return true;
1421 }
1422 return false;
1423 }
1424
1425 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1426 return !Matches(T->getDepth());
1427 }
1428
1429 bool TraverseTemplateName(TemplateName N) {
1430 if (TemplateTemplateParmDecl *PD =
1431 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1432 if (Matches(PD->getDepth())) return false;
1433 return super::TraverseTemplateName(N);
1434 }
1435
1436 bool VisitDeclRefExpr(DeclRefExpr *E) {
1437 if (NonTypeTemplateParmDecl *PD =
1438 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1439 if (PD->getDepth() == Depth) {
1440 Match = true;
1441 return false;
1442 }
1443 }
1444 return super::VisitDeclRefExpr(E);
1445 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001446
1447 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1448 return TraverseType(T->getInjectedSpecializationType());
1449 }
John McCall4e2cbb22010-10-20 05:44:58 +00001450};
1451}
1452
Douglas Gregorc8406492011-05-10 18:27:06 +00001453/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001454/// list.
1455static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001456DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001457 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001458 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001459 return Checker.Match;
1460}
1461
Douglas Gregorc8406492011-05-10 18:27:06 +00001462// Find the source range corresponding to the named type in the given
1463// nested-name-specifier, if any.
1464static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1465 QualType T,
1466 const CXXScopeSpec &SS) {
1467 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1468 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1469 if (const Type *CurType = NNS->getAsType()) {
1470 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1471 return NNSLoc.getTypeLoc().getSourceRange();
1472 } else
1473 break;
1474
1475 NNSLoc = NNSLoc.getPrefix();
1476 }
1477
1478 return SourceRange();
1479}
1480
Mike Stump1eb44332009-09-09 15:08:12 +00001481/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001482/// specifier, returning the template parameter list that applies to the
1483/// name.
1484///
1485/// \param DeclStartLoc the start of the declaration that has a scope
1486/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001487///
Douglas Gregorc8406492011-05-10 18:27:06 +00001488/// \param DeclLoc The location of the declaration itself.
1489///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001490/// \param SS the scope specifier that will be matched to the given template
1491/// parameter lists. This scope specifier precedes a qualified name that is
1492/// being declared.
1493///
1494/// \param ParamLists the template parameter lists, from the outermost to the
1495/// innermost template parameter lists.
1496///
1497/// \param NumParamLists the number of template parameter lists in ParamLists.
1498///
John McCall77e8b112010-04-13 20:37:33 +00001499/// \param IsFriend Whether to apply the slightly different rules for
1500/// matching template parameters to scope specifiers in friend
1501/// declarations.
1502///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001503/// \param IsExplicitSpecialization will be set true if the entity being
1504/// declared is an explicit specialization, false otherwise.
1505///
Mike Stump1eb44332009-09-09 15:08:12 +00001506/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001507/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001508/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001509/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001510/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001511/// itself a template).
1512TemplateParameterList *
1513Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001514 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001515 const CXXScopeSpec &SS,
1516 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001517 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001518 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001519 bool &IsExplicitSpecialization,
1520 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001521 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001522 Invalid = false;
1523
1524 // The sequence of nested types to which we will match up the template
1525 // parameter lists. We first build this list by starting with the type named
1526 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001527 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001528 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001529 if (SS.getScopeRep()) {
1530 if (CXXRecordDecl *Record
1531 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1532 T = Context.getTypeDeclType(Record);
1533 else
1534 T = QualType(SS.getScopeRep()->getAsType(), 0);
1535 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001536
1537 // If we found an explicit specialization that prevents us from needing
1538 // 'template<>' headers, this will be set to the location of that
1539 // explicit specialization.
1540 SourceLocation ExplicitSpecLoc;
1541
1542 while (!T.isNull()) {
1543 NestedTypes.push_back(T);
1544
1545 // Retrieve the parent of a record type.
1546 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1547 // If this type is an explicit specialization, we're done.
1548 if (ClassTemplateSpecializationDecl *Spec
1549 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1550 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1551 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1552 ExplicitSpecLoc = Spec->getLocation();
1553 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001554 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001555 } else if (Record->getTemplateSpecializationKind()
1556 == TSK_ExplicitSpecialization) {
1557 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001558 break;
1559 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001560
1561 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1562 T = Context.getTypeDeclType(Parent);
1563 else
1564 T = QualType();
1565 continue;
1566 }
1567
1568 if (const TemplateSpecializationType *TST
1569 = T->getAs<TemplateSpecializationType>()) {
1570 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1571 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1572 T = Context.getTypeDeclType(Parent);
1573 else
1574 T = QualType();
1575 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001576 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001577 }
1578
1579 // Look one step prior in a dependent template specialization type.
1580 if (const DependentTemplateSpecializationType *DependentTST
1581 = T->getAs<DependentTemplateSpecializationType>()) {
1582 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1583 T = QualType(NNS->getAsType(), 0);
1584 else
1585 T = QualType();
1586 continue;
1587 }
1588
1589 // Look one step prior in a dependent name type.
1590 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1591 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1592 T = QualType(NNS->getAsType(), 0);
1593 else
1594 T = QualType();
1595 continue;
1596 }
1597
1598 // Retrieve the parent of an enumeration type.
1599 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1600 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1601 // check here.
1602 EnumDecl *Enum = EnumT->getDecl();
1603
1604 // Get to the parent type.
1605 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1606 T = Context.getTypeDeclType(Parent);
1607 else
1608 T = QualType();
1609 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001610 }
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Douglas Gregorc8406492011-05-10 18:27:06 +00001612 T = QualType();
1613 }
1614 // Reverse the nested types list, since we want to traverse from the outermost
1615 // to the innermost while checking template-parameter-lists.
1616 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001617
Douglas Gregorc8406492011-05-10 18:27:06 +00001618 // C++0x [temp.expl.spec]p17:
1619 // A member or a member template may be nested within many
1620 // enclosing class templates. In an explicit specialization for
1621 // such a member, the member declaration shall be preceded by a
1622 // template<> for each enclosing class template that is
1623 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001624 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001625 unsigned ParamIdx = 0;
1626 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1627 ++TypeIdx) {
1628 T = NestedTypes[TypeIdx];
1629
1630 // Whether we expect a 'template<>' header.
1631 bool NeedEmptyTemplateHeader = false;
1632
1633 // Whether we expect a template header with parameters.
1634 bool NeedNonemptyTemplateHeader = false;
1635
1636 // For a dependent type, the set of template parameters that we
1637 // expect to see.
1638 TemplateParameterList *ExpectedTemplateParams = 0;
1639
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001640 // C++0x [temp.expl.spec]p15:
1641 // A member or a member template may be nested within many enclosing
1642 // class templates. In an explicit specialization for such a member, the
1643 // member declaration shall be preceded by a template<> for each
1644 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001645 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1646 if (ClassTemplatePartialSpecializationDecl *Partial
1647 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1648 ExpectedTemplateParams = Partial->getTemplateParameters();
1649 NeedNonemptyTemplateHeader = true;
1650 } else if (Record->isDependentType()) {
1651 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001652 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001653 ->getTemplateParameters();
1654 NeedNonemptyTemplateHeader = true;
1655 }
1656 } else if (ClassTemplateSpecializationDecl *Spec
1657 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1658 // C++0x [temp.expl.spec]p4:
1659 // Members of an explicitly specialized class template are defined
1660 // in the same manner as members of normal classes, and not using
1661 // the template<> syntax.
1662 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1663 NeedEmptyTemplateHeader = true;
1664 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001665 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001666 } else if (Record->getTemplateSpecializationKind()) {
1667 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001668 != TSK_ExplicitSpecialization &&
1669 TypeIdx == NumTypes - 1)
1670 IsExplicitSpecialization = true;
1671
1672 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001673 }
1674 } else if (const TemplateSpecializationType *TST
1675 = T->getAs<TemplateSpecializationType>()) {
1676 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1677 ExpectedTemplateParams = Template->getTemplateParameters();
1678 NeedNonemptyTemplateHeader = true;
1679 }
1680 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1681 // FIXME: We actually could/should check the template arguments here
1682 // against the corresponding template parameter list.
1683 NeedNonemptyTemplateHeader = false;
1684 }
1685
Douglas Gregor89b9f102011-06-06 15:22:55 +00001686 // C++ [temp.expl.spec]p16:
1687 // In an explicit specialization declaration for a member of a class
1688 // template or a member template that ap- pears in namespace scope, the
1689 // member template and some of its enclosing class templates may remain
1690 // unspecialized, except that the declaration shall not explicitly
1691 // specialize a class member template if its en- closing class templates
1692 // are not explicitly specialized as well.
1693 if (ParamIdx < NumParamLists) {
1694 if (ParamLists[ParamIdx]->size() == 0) {
1695 if (SawNonEmptyTemplateParameterList) {
1696 Diag(DeclLoc, diag::err_specialize_member_of_template)
1697 << ParamLists[ParamIdx]->getSourceRange();
1698 Invalid = true;
1699 IsExplicitSpecialization = false;
1700 return 0;
1701 }
1702 } else
1703 SawNonEmptyTemplateParameterList = true;
1704 }
1705
Douglas Gregorc8406492011-05-10 18:27:06 +00001706 if (NeedEmptyTemplateHeader) {
1707 // If we're on the last of the types, and we need a 'template<>' header
1708 // here, then it's an explicit specialization.
1709 if (TypeIdx == NumTypes - 1)
1710 IsExplicitSpecialization = true;
1711
1712 if (ParamIdx < NumParamLists) {
1713 if (ParamLists[ParamIdx]->size() > 0) {
1714 // The header has template parameters when it shouldn't. Complain.
1715 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1716 diag::err_template_param_list_matches_nontemplate)
1717 << T
1718 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1719 ParamLists[ParamIdx]->getRAngleLoc())
1720 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1721 Invalid = true;
1722 return 0;
1723 }
1724
1725 // Consume this template header.
1726 ++ParamIdx;
1727 continue;
1728 }
1729
1730 if (!IsFriend) {
1731 // We don't have a template header, but we should.
1732 SourceLocation ExpectedTemplateLoc;
1733 if (NumParamLists > 0)
1734 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1735 else
1736 ExpectedTemplateLoc = DeclStartLoc;
1737
1738 Diag(DeclLoc, diag::err_template_spec_needs_header)
1739 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1740 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1741 }
1742
1743 continue;
1744 }
1745
1746 if (NeedNonemptyTemplateHeader) {
1747 // In friend declarations we can have template-ids which don't
1748 // depend on the corresponding template parameter lists. But
1749 // assume that empty parameter lists are supposed to match this
1750 // template-id.
1751 if (IsFriend && T->isDependentType()) {
1752 if (ParamIdx < NumParamLists &&
1753 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1754 ExpectedTemplateParams = 0;
1755 else
1756 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001757 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001758
Douglas Gregorc8406492011-05-10 18:27:06 +00001759 if (ParamIdx < NumParamLists) {
1760 // Check the template parameter list, if we can.
1761 if (ExpectedTemplateParams &&
1762 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1763 ExpectedTemplateParams,
1764 true, TPL_TemplateMatch))
1765 Invalid = true;
1766
1767 if (!Invalid &&
1768 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1769 TPC_ClassTemplateMember))
1770 Invalid = true;
1771
1772 ++ParamIdx;
1773 continue;
1774 }
1775
1776 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1777 << T
1778 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1779 Invalid = true;
1780 continue;
1781 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001782 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001783
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001784 // If there were at least as many template-ids as there were template
1785 // parameter lists, then there are no template parameter lists remaining for
1786 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001787 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001788 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001790 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001791 if (ParamIdx < NumParamLists - 1) {
1792 bool HasAnyExplicitSpecHeader = false;
1793 bool AllExplicitSpecHeaders = true;
1794 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1795 if (ParamLists[I]->size() == 0)
1796 HasAnyExplicitSpecHeader = true;
1797 else
1798 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001799 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001800
1801 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1802 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1803 : diag::err_template_spec_extra_headers)
1804 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1805 ParamLists[NumParamLists - 2]->getRAngleLoc());
1806
1807 // If there was a specialization somewhere, such that 'template<>' is
1808 // not required, and there were any 'template<>' headers, note where the
1809 // specialization occurred.
1810 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1811 Diag(ExplicitSpecLoc,
1812 diag::note_explicit_template_spec_does_not_need_header)
1813 << NestedTypes.back();
1814
1815 // We have a template parameter list with no corresponding scope, which
1816 // means that the resulting template declaration can't be instantiated
1817 // properly (we'll end up with dependent nodes when we shouldn't).
1818 if (!AllExplicitSpecHeaders)
1819 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001820 }
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Douglas Gregor89b9f102011-06-06 15:22:55 +00001822 // C++ [temp.expl.spec]p16:
1823 // In an explicit specialization declaration for a member of a class
1824 // template or a member template that ap- pears in namespace scope, the
1825 // member template and some of its enclosing class templates may remain
1826 // unspecialized, except that the declaration shall not explicitly
1827 // specialize a class member template if its en- closing class templates
1828 // are not explicitly specialized as well.
1829 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1830 SawNonEmptyTemplateParameterList) {
1831 Diag(DeclLoc, diag::err_specialize_member_of_template)
1832 << ParamLists[ParamIdx]->getSourceRange();
1833 Invalid = true;
1834 IsExplicitSpecialization = false;
1835 return 0;
1836 }
1837
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001838 // Return the last template parameter list, which corresponds to the
1839 // entity being declared.
1840 return ParamLists[NumParamLists - 1];
1841}
1842
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001843void Sema::NoteAllFoundTemplates(TemplateName Name) {
1844 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1845 Diag(Template->getLocation(), diag::note_template_declared_here)
1846 << (isa<FunctionTemplateDecl>(Template)? 0
1847 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001848 : isa<TypeAliasTemplateDecl>(Template)? 2
1849 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001850 << Template->getDeclName();
1851 return;
1852 }
1853
1854 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1855 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1856 IEnd = OST->end();
1857 I != IEnd; ++I)
1858 Diag((*I)->getLocation(), diag::note_template_declared_here)
1859 << 0 << (*I)->getDeclName();
1860
1861 return;
1862 }
1863}
1864
1865
Douglas Gregor7532dc62009-03-30 22:58:21 +00001866QualType Sema::CheckTemplateIdType(TemplateName Name,
1867 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001868 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001869 DependentTemplateName *DTN
1870 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001871 if (DTN && DTN->isIdentifier())
1872 // When building a template-id where the template-name is dependent,
1873 // assume the template is a type template. Either our assumption is
1874 // correct, or the code is ill-formed and will be diagnosed when the
1875 // dependent name is substituted.
1876 return Context.getDependentTemplateSpecializationType(ETK_None,
1877 DTN->getQualifier(),
1878 DTN->getIdentifier(),
1879 TemplateArgs);
1880
Douglas Gregor7532dc62009-03-30 22:58:21 +00001881 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001882 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1883 // We might have a substituted template template parameter pack. If so,
1884 // build a template specialization type for it.
1885 if (Name.getAsSubstTemplateTemplateParmPack())
1886 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001887
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001888 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1889 << Name;
1890 NoteAllFoundTemplates(Name);
1891 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001892 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001893
Douglas Gregor40808ce2009-03-09 23:48:35 +00001894 // Check that the template argument list is well-formed for this
1895 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001896 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001897 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001898 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001899 return QualType();
1900
Douglas Gregor910f8002010-11-07 23:05:16 +00001901 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001902 "Converted template argument list is too short!");
1903
1904 QualType CanonType;
1905
Douglas Gregor561f8122011-07-01 01:22:09 +00001906 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001907 if (TypeAliasTemplateDecl *AliasTemplate
1908 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1909 // Find the canonical type for this type alias template specialization.
1910 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1911 if (Pattern->isInvalidDecl())
1912 return QualType();
1913
1914 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1915 Converted.data(), Converted.size());
1916
1917 // Only substitute for the innermost template argument list.
1918 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001919 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001920 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1921 for (unsigned I = 0; I < Depth; ++I)
1922 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001923
1924 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1925 CanonType = SubstType(Pattern->getUnderlyingType(),
1926 TemplateArgLists, AliasTemplate->getLocation(),
1927 AliasTemplate->getDeclName());
1928 if (CanonType.isNull())
1929 return QualType();
1930 } else if (Name.isDependent() ||
1931 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001932 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001933 // This class template specialization is a dependent
1934 // type. Therefore, its canonical type is another class template
1935 // specialization type that contains all of the converted
1936 // arguments in canonical form. This ensures that, e.g., A<T> and
1937 // A<T, T> have identical types when A is declared as:
1938 //
1939 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001940 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001941 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001942 Converted.data(),
1943 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Douglas Gregor1275ae02009-07-28 23:00:59 +00001945 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001946 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001947 // In the future, we need to teach getTemplateSpecializationType to only
1948 // build the canonical type and return that to us.
1949 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001950
1951 // This might work out to be a current instantiation, in which
1952 // case the canonical type needs to be the InjectedClassNameType.
1953 //
1954 // TODO: in theory this could be a simple hashtable lookup; most
1955 // changes to CurContext don't change the set of current
1956 // instantiations.
1957 if (isa<ClassTemplateDecl>(Template)) {
1958 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1959 // If we get out to a namespace, we're done.
1960 if (Ctx->isFileContext()) break;
1961
1962 // If this isn't a record, keep looking.
1963 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1964 if (!Record) continue;
1965
1966 // Look for one of the two cases with InjectedClassNameTypes
1967 // and check whether it's the same template.
1968 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1969 !Record->getDescribedClassTemplate())
1970 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001971
John McCall31f17ec2010-04-27 00:57:59 +00001972 // Fetch the injected class name type and check whether its
1973 // injected type is equal to the type we just built.
1974 QualType ICNT = Context.getTypeDeclType(Record);
1975 QualType Injected = cast<InjectedClassNameType>(ICNT)
1976 ->getInjectedSpecializationType();
1977
1978 if (CanonType != Injected->getCanonicalTypeInternal())
1979 continue;
1980
1981 // If so, the canonical type of this TST is the injected
1982 // class name type of the record we just found.
1983 assert(ICNT.isCanonical());
1984 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001985 break;
1986 }
1987 }
Mike Stump1eb44332009-09-09 15:08:12 +00001988 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001989 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001990 // Find the class template specialization declaration that
1991 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001992 void *InsertPos = 0;
1993 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001994 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001995 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001996 if (!Decl) {
1997 // This is the first time we have referenced this class template
1998 // specialization. Create the canonical declaration and add it to
1999 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002000 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002001 ClassTemplate->getTemplatedDecl()->getTagKind(),
2002 ClassTemplate->getDeclContext(),
2003 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002004 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002005 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002006 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002007 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002008 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002009 Decl->setLexicalDeclContext(CurContext);
2010 }
2011
2012 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002013 assert(isa<RecordType>(CanonType) &&
2014 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002015 }
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Douglas Gregor40808ce2009-03-09 23:48:35 +00002017 // Build the fully-sugared type for this class template
2018 // specialization, which refers back to the class template
2019 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002020 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002021}
2022
John McCallf312b1e2010-08-26 23:41:50 +00002023TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002024Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2025 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002026 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002027 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00002028 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002029 if (SS.isInvalid())
2030 return true;
2031
Douglas Gregor7532dc62009-03-30 22:58:21 +00002032 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002033
Douglas Gregor40808ce2009-03-09 23:48:35 +00002034 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002035 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002036 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002037
Douglas Gregora88f09f2011-02-28 17:23:35 +00002038 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2039 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
2040 DTN->getQualifier(),
2041 DTN->getIdentifier(),
2042 TemplateArgs);
2043
2044 // Build type-source information.
2045 TypeLocBuilder TLB;
2046 DependentTemplateSpecializationTypeLoc SpecTL
2047 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002048 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002049 SpecTL.setNameLoc(TemplateLoc);
2050 SpecTL.setLAngleLoc(LAngleLoc);
2051 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002052 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002053 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2054 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2055 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2056 }
2057
John McCalld5532b62009-11-23 01:53:49 +00002058 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002059 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002060
2061 if (Result.isNull())
2062 return true;
2063
Douglas Gregor059101f2011-03-02 00:47:37 +00002064 // Build type-source information.
2065 TypeLocBuilder TLB;
2066 TemplateSpecializationTypeLoc SpecTL
2067 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2068 SpecTL.setTemplateNameLoc(TemplateLoc);
2069 SpecTL.setLAngleLoc(LAngleLoc);
2070 SpecTL.setRAngleLoc(RAngleLoc);
2071 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2072 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002073
Douglas Gregor059101f2011-03-02 00:47:37 +00002074 if (SS.isNotEmpty()) {
2075 // Create an elaborated-type-specifier containing the nested-name-specifier.
2076 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2077 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2078 ElabTL.setKeywordLoc(SourceLocation());
2079 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2080 }
2081
2082 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002083}
John McCallf1bbbb42009-09-04 01:14:41 +00002084
Douglas Gregor059101f2011-03-02 00:47:37 +00002085TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002086 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002087 SourceLocation TagLoc,
2088 CXXScopeSpec &SS,
2089 TemplateTy TemplateD,
2090 SourceLocation TemplateLoc,
2091 SourceLocation LAngleLoc,
2092 ASTTemplateArgsPtr TemplateArgsIn,
2093 SourceLocation RAngleLoc) {
2094 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2095
2096 // Translate the parser's template argument list in our AST format.
2097 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2098 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2099
2100 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002101 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002102 ElaboratedTypeKeyword Keyword
2103 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Douglas Gregor059101f2011-03-02 00:47:37 +00002105 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2106 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2107 DTN->getQualifier(),
2108 DTN->getIdentifier(),
2109 TemplateArgs);
2110
2111 // Build type-source information.
2112 TypeLocBuilder TLB;
2113 DependentTemplateSpecializationTypeLoc SpecTL
2114 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2115 SpecTL.setKeywordLoc(TagLoc);
2116 SpecTL.setNameLoc(TemplateLoc);
2117 SpecTL.setLAngleLoc(LAngleLoc);
2118 SpecTL.setRAngleLoc(RAngleLoc);
2119 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2120 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2121 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2122 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2123 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002124
2125 if (TypeAliasTemplateDecl *TAT =
2126 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2127 // C++0x [dcl.type.elab]p2:
2128 // If the identifier resolves to a typedef-name or the simple-template-id
2129 // resolves to an alias template specialization, the
2130 // elaborated-type-specifier is ill-formed.
2131 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2132 Diag(TAT->getLocation(), diag::note_declared_at);
2133 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002134
2135 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2136 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002137 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002138
2139 // Check the tag kind
2140 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002141 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002142
John McCall6b2becf2009-09-08 17:47:29 +00002143 IdentifierInfo *Id = D->getIdentifier();
2144 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002145
Richard Trieubbf34c02011-06-10 03:11:26 +00002146 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2147 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002148 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002149 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002150 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002151 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002152 }
2153 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002154
2155 // Provide source-location information for the template specialization.
2156 TypeLocBuilder TLB;
2157 TemplateSpecializationTypeLoc SpecTL
2158 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2159 SpecTL.setTemplateNameLoc(TemplateLoc);
2160 SpecTL.setLAngleLoc(LAngleLoc);
2161 SpecTL.setRAngleLoc(RAngleLoc);
2162 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2163 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002164
Douglas Gregor059101f2011-03-02 00:47:37 +00002165 // Construct an elaborated type containing the nested-name-specifier (if any)
2166 // and keyword.
2167 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2168 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2169 ElabTL.setKeywordLoc(TagLoc);
2170 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2171 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002172}
2173
John McCall60d7b3a2010-08-24 06:29:42 +00002174ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002175 LookupResult &R,
2176 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002177 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002178 // FIXME: Can we do any checking at this point? I guess we could check the
2179 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002180 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002181 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002182 // foo<int> could identify a single function unambiguously
2183 // This approach does NOT work, since f<int>(1);
2184 // gets resolved prior to resorting to overload resolution
2185 // i.e., template<class T> void f(double);
2186 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002187
2188 // These should be filtered out by our callers.
2189 assert(!R.empty() && "empty lookup results when building templateid");
2190 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2191
John McCallc373d482010-01-27 01:50:18 +00002192 // We don't want lookup warnings at this point.
2193 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002194
John McCallf7a1a742009-11-24 19:00:30 +00002195 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002196 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002197 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002198 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002199 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002200 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002201
2202 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002203}
2204
John McCallf7a1a742009-11-24 19:00:30 +00002205// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002206ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002207Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00002208 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002209 const TemplateArgumentListInfo &TemplateArgs) {
2210 DeclContext *DC;
2211 if (!(DC = computeDeclContext(SS, false)) ||
2212 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002213 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00002214 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002215
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002216 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002217 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002218 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2219 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002220
John McCallf7a1a742009-11-24 19:00:30 +00002221 if (R.isAmbiguous())
2222 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002223
John McCallf7a1a742009-11-24 19:00:30 +00002224 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002225 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2226 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002227 return ExprError();
2228 }
2229
2230 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002231 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2232 << (NestedNameSpecifier*) SS.getScopeRep()
2233 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002234 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2235 return ExprError();
2236 }
2237
2238 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002239}
2240
Douglas Gregorc45c2322009-03-31 00:43:58 +00002241/// \brief Form a dependent template name.
2242///
2243/// This action forms a dependent template name given the template
2244/// name and its (presumably dependent) scope specifier. For
2245/// example, given "MetaFun::template apply", the scope specifier \p
2246/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2247/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002248TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002249 SourceLocation TemplateKWLoc,
2250 CXXScopeSpec &SS,
2251 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002252 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002253 bool EnteringContext,
2254 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002255 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2256 !getLangOptions().CPlusPlus0x)
2257 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002258 << FixItHint::CreateRemoval(TemplateKWLoc);
2259
Douglas Gregor0707bc52010-01-19 16:01:07 +00002260 DeclContext *LookupCtx = 0;
2261 if (SS.isSet())
2262 LookupCtx = computeDeclContext(SS, EnteringContext);
2263 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002264 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002265 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002266 // C++0x [temp.names]p5:
2267 // If a name prefixed by the keyword template is not the name of
2268 // a template, the program is ill-formed. [Note: the keyword
2269 // template may not be applied to non-template members of class
2270 // templates. -end note ] [ Note: as is the case with the
2271 // typename prefix, the template prefix is allowed in cases
2272 // where it is not strictly necessary; i.e., when the
2273 // nested-name-specifier or the expression on the left of the ->
2274 // or . is not dependent on a template-parameter, or the use
2275 // does not appear in the scope of a template. -end note]
2276 //
2277 // Note: C++03 was more strict here, because it banned the use of
2278 // the "template" keyword prior to a template-name that was not a
2279 // dependent name. C++ DR468 relaxed this requirement (the
2280 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002281 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002282 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002283 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2284 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002285 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002286 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2287 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002288 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2289 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002290 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002291 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002292 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002293 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002294 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002295 << Name.getSourceRange()
2296 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002297 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002298 } else {
2299 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002300 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002301 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002302 }
2303
Mike Stump1eb44332009-09-09 15:08:12 +00002304 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002305 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002306
Douglas Gregor014e88d2009-11-03 23:16:33 +00002307 switch (Name.getKind()) {
2308 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002309 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002310 Name.Identifier));
2311 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002312
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002313 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002314 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002315 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002316 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002317
2318 case UnqualifiedId::IK_LiteralOperatorId:
2319 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2320
Douglas Gregor014e88d2009-11-03 23:16:33 +00002321 default:
2322 break;
2323 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002324
2325 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002326 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002327 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002328 << Name.getSourceRange()
2329 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002330 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002331}
2332
Mike Stump1eb44332009-09-09 15:08:12 +00002333bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002334 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002335 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002336 const TemplateArgument &Arg = AL.getArgument();
2337
Anders Carlsson436b1562009-06-13 00:33:33 +00002338 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002339 switch(Arg.getKind()) {
2340 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002341 // C++ [temp.arg.type]p1:
2342 // A template-argument for a template-parameter which is a
2343 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002344 break;
2345 case TemplateArgument::Template: {
2346 // We have a template type parameter but the template argument
2347 // is a template without any arguments.
2348 SourceRange SR = AL.getSourceRange();
2349 TemplateName Name = Arg.getAsTemplate();
2350 Diag(SR.getBegin(), diag::err_template_missing_args)
2351 << Name << SR;
2352 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2353 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002354
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002355 return true;
2356 }
2357 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002358 // We have a template type parameter but the template argument
2359 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002360 SourceRange SR = AL.getSourceRange();
2361 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002362 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Anders Carlsson436b1562009-06-13 00:33:33 +00002364 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002365 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002366 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002367
John McCalla93c9342009-12-07 02:54:59 +00002368 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002369 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Anders Carlsson436b1562009-06-13 00:33:33 +00002371 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002372 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2373
2374 // Objective-C ARC:
2375 // If an explicitly-specified template argument type is a lifetime type
2376 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2377 if (getLangOptions().ObjCAutoRefCount &&
2378 ArgType->isObjCLifetimeType() &&
2379 !ArgType.getObjCLifetime()) {
2380 Qualifiers Qs;
2381 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2382 ArgType = Context.getQualifiedType(ArgType, Qs);
2383 }
2384
2385 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002386 return false;
2387}
2388
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002389/// \brief Substitute template arguments into the default template argument for
2390/// the given template type parameter.
2391///
2392/// \param SemaRef the semantic analysis object for which we are performing
2393/// the substitution.
2394///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002395/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002396/// for.
2397///
2398/// \param TemplateLoc the location of the template name that started the
2399/// template-id we are checking.
2400///
2401/// \param RAngleLoc the location of the right angle bracket ('>') that
2402/// terminates the template-id.
2403///
2404/// \param Param the template template parameter whose default we are
2405/// substituting into.
2406///
2407/// \param Converted the list of template arguments provided for template
2408/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002409/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002410static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002411SubstDefaultTemplateArgument(Sema &SemaRef,
2412 TemplateDecl *Template,
2413 SourceLocation TemplateLoc,
2414 SourceLocation RAngleLoc,
2415 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002416 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002417 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002418
2419 // If the argument type is dependent, instantiate it now based
2420 // on the previously-computed template arguments.
2421 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002422 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002423 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002424
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002425 MultiLevelTemplateArgumentList AllTemplateArgs
2426 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2427
2428 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002429 Template, Converted.data(),
2430 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002431 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002432
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002433 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2434 Param->getDefaultArgumentLoc(),
2435 Param->getDeclName());
2436 }
2437
2438 return ArgType;
2439}
2440
2441/// \brief Substitute template arguments into the default template argument for
2442/// the given non-type template parameter.
2443///
2444/// \param SemaRef the semantic analysis object for which we are performing
2445/// the substitution.
2446///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002447/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002448/// for.
2449///
2450/// \param TemplateLoc the location of the template name that started the
2451/// template-id we are checking.
2452///
2453/// \param RAngleLoc the location of the right angle bracket ('>') that
2454/// terminates the template-id.
2455///
Douglas Gregor788cd062009-11-11 01:00:40 +00002456/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002457/// substituting into.
2458///
2459/// \param Converted the list of template arguments provided for template
2460/// parameters that precede \p Param in the template parameter list.
2461///
2462/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002463static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002464SubstDefaultTemplateArgument(Sema &SemaRef,
2465 TemplateDecl *Template,
2466 SourceLocation TemplateLoc,
2467 SourceLocation RAngleLoc,
2468 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002469 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002470 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002471 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002472
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002473 MultiLevelTemplateArgumentList AllTemplateArgs
2474 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002475
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002476 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002477 Template, Converted.data(),
2478 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002479 SourceRange(TemplateLoc, RAngleLoc));
2480
2481 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2482}
2483
Douglas Gregor788cd062009-11-11 01:00:40 +00002484/// \brief Substitute template arguments into the default template argument for
2485/// the given template template parameter.
2486///
2487/// \param SemaRef the semantic analysis object for which we are performing
2488/// the substitution.
2489///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002490/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002491/// for.
2492///
2493/// \param TemplateLoc the location of the template name that started the
2494/// template-id we are checking.
2495///
2496/// \param RAngleLoc the location of the right angle bracket ('>') that
2497/// terminates the template-id.
2498///
2499/// \param Param the template template parameter whose default we are
2500/// substituting into.
2501///
2502/// \param Converted the list of template arguments provided for template
2503/// parameters that precede \p Param in the template parameter list.
2504///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002505/// \param QualifierLoc Will be set to the nested-name-specifier (with
2506/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002507///
Douglas Gregor788cd062009-11-11 01:00:40 +00002508/// \returns the substituted template argument, or NULL if an error occurred.
2509static TemplateName
2510SubstDefaultTemplateArgument(Sema &SemaRef,
2511 TemplateDecl *Template,
2512 SourceLocation TemplateLoc,
2513 SourceLocation RAngleLoc,
2514 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002515 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002516 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002517 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002518 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002519
Douglas Gregor788cd062009-11-11 01:00:40 +00002520 MultiLevelTemplateArgumentList AllTemplateArgs
2521 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002522
Douglas Gregor788cd062009-11-11 01:00:40 +00002523 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002524 Template, Converted.data(),
2525 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002526 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002527
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002528 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002529 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002530 if (QualifierLoc) {
2531 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2532 AllTemplateArgs);
2533 if (!QualifierLoc)
2534 return TemplateName();
2535 }
2536
Douglas Gregor1d752d72011-03-02 18:46:51 +00002537 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002538 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002539 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002540 AllTemplateArgs);
2541}
2542
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002543/// \brief If the given template parameter has a default template
2544/// argument, substitute into that default template argument and
2545/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002546TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002547Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2548 SourceLocation TemplateLoc,
2549 SourceLocation RAngleLoc,
2550 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002551 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002552 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002553 if (!TypeParm->hasDefaultArgument())
2554 return TemplateArgumentLoc();
2555
John McCalla93c9342009-12-07 02:54:59 +00002556 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002557 TemplateLoc,
2558 RAngleLoc,
2559 TypeParm,
2560 Converted);
2561 if (DI)
2562 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2563
2564 return TemplateArgumentLoc();
2565 }
2566
2567 if (NonTypeTemplateParmDecl *NonTypeParm
2568 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2569 if (!NonTypeParm->hasDefaultArgument())
2570 return TemplateArgumentLoc();
2571
John McCall60d7b3a2010-08-24 06:29:42 +00002572 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002573 TemplateLoc,
2574 RAngleLoc,
2575 NonTypeParm,
2576 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002577 if (Arg.isInvalid())
2578 return TemplateArgumentLoc();
2579
2580 Expr *ArgE = Arg.takeAs<Expr>();
2581 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2582 }
2583
2584 TemplateTemplateParmDecl *TempTempParm
2585 = cast<TemplateTemplateParmDecl>(Param);
2586 if (!TempTempParm->hasDefaultArgument())
2587 return TemplateArgumentLoc();
2588
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002589
Douglas Gregor1d752d72011-03-02 18:46:51 +00002590 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002591 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002592 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002593 RAngleLoc,
2594 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002595 Converted,
2596 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002597 if (TName.isNull())
2598 return TemplateArgumentLoc();
2599
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002600 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002601 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002602 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2603}
2604
Douglas Gregore7526412009-11-11 19:31:23 +00002605/// \brief Check that the given template argument corresponds to the given
2606/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002607///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002608/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002609/// checked.
2610///
2611/// \param Arg The template argument.
2612///
2613/// \param Template The template in which the template argument resides.
2614///
2615/// \param TemplateLoc The location of the template name for the template
2616/// whose argument list we're matching.
2617///
2618/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2619/// the template argument list.
2620///
2621/// \param ArgumentPackIndex The index into the argument pack where this
2622/// argument will be placed. Only valid if the parameter is a parameter pack.
2623///
2624/// \param Converted The checked, converted argument will be added to the
2625/// end of this small vector.
2626///
2627/// \param CTAK Describes how we arrived at this particular template argument:
2628/// explicitly written, deduced, etc.
2629///
2630/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002631bool Sema::CheckTemplateArgument(NamedDecl *Param,
2632 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002633 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002634 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002635 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002636 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002637 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002638 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002639 // Check template type parameters.
2640 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002641 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002642
Douglas Gregord9e15302009-11-11 19:41:09 +00002643 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002644 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002645 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002646 // with the template arguments we've seen thus far. But if the
2647 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002648 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002649 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2650 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002651
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002652 if (NTTPType->isDependentType() &&
2653 !isa<TemplateTemplateParmDecl>(Template) &&
2654 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002655 // Do substitution on the type of the non-type template parameter.
2656 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002657 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002658 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002659
2660 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002661 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002662 NTTPType = SubstType(NTTPType,
2663 MultiLevelTemplateArgumentList(TemplateArgs),
2664 NTTP->getLocation(),
2665 NTTP->getDeclName());
2666 // If that worked, check the non-type template parameter type
2667 // for validity.
2668 if (!NTTPType.isNull())
2669 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2670 NTTP->getLocation());
2671 if (NTTPType.isNull())
2672 return true;
2673 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002674
Douglas Gregore7526412009-11-11 19:31:23 +00002675 switch (Arg.getArgument().getKind()) {
2676 case TemplateArgument::Null:
2677 assert(false && "Should never see a NULL template argument here");
2678 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002679
Douglas Gregore7526412009-11-11 19:31:23 +00002680 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002681 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002682 ExprResult Res =
2683 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2684 Result, CTAK);
2685 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002686 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002687
Douglas Gregor910f8002010-11-07 23:05:16 +00002688 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002689 break;
2690 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002691
Douglas Gregore7526412009-11-11 19:31:23 +00002692 case TemplateArgument::Declaration:
2693 case TemplateArgument::Integral:
2694 // We've already checked this template argument, so just copy
2695 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002696 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002697 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002698
Douglas Gregore7526412009-11-11 19:31:23 +00002699 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002700 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002701 // We were given a template template argument. It may not be ill-formed;
2702 // see below.
2703 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002704 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2705 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002706 // We have a template argument such as \c T::template X, which we
2707 // parsed as a template template argument. However, since we now
2708 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002709 // template name into an expression.
2710
2711 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2712 Arg.getTemplateNameLoc());
2713
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002714 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002715 SS.Adopt(Arg.getTemplateQualifierLoc());
John Wiegley429bb272011-04-08 18:41:53 +00002716 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002717 SS.getWithLocInContext(Context),
John Wiegley429bb272011-04-08 18:41:53 +00002718 NameInfo));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002719
Douglas Gregora7fc9012011-01-05 18:58:31 +00002720 // If we parsed the template argument as a pack expansion, create a
2721 // pack expansion expression.
2722 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002723 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2724 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002725 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002726 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727
Douglas Gregore7526412009-11-11 19:31:23 +00002728 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002729 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2730 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002731 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732
Douglas Gregor910f8002010-11-07 23:05:16 +00002733 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002734 break;
2735 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002736
Douglas Gregore7526412009-11-11 19:31:23 +00002737 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002738 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002739 // therefore cannot be a non-type template argument.
2740 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2741 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002742
Douglas Gregore7526412009-11-11 19:31:23 +00002743 Diag(Param->getLocation(), diag::note_template_param_here);
2744 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002745
Douglas Gregore7526412009-11-11 19:31:23 +00002746 case TemplateArgument::Type: {
2747 // We have a non-type template parameter but the template
2748 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002749
Douglas Gregore7526412009-11-11 19:31:23 +00002750 // C++ [temp.arg]p2:
2751 // In a template-argument, an ambiguity between a type-id and
2752 // an expression is resolved to a type-id, regardless of the
2753 // form of the corresponding template-parameter.
2754 //
2755 // We warn specifically about this case, since it can be rather
2756 // confusing for users.
2757 QualType T = Arg.getArgument().getAsType();
2758 SourceRange SR = Arg.getSourceRange();
2759 if (T->isFunctionType())
2760 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2761 else
2762 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2763 Diag(Param->getLocation(), diag::note_template_param_here);
2764 return true;
2765 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002766
Douglas Gregore7526412009-11-11 19:31:23 +00002767 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002768 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002769 break;
2770 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002771
Douglas Gregore7526412009-11-11 19:31:23 +00002772 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002773 }
2774
2775
Douglas Gregore7526412009-11-11 19:31:23 +00002776 // Check template template parameters.
2777 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002778
Douglas Gregore7526412009-11-11 19:31:23 +00002779 // Substitute into the template parameter list of the template
2780 // template parameter, since previously-supplied template arguments
2781 // may appear within the template template parameter.
2782 {
2783 // Set up a template instantiation context.
2784 LocalInstantiationScope Scope(*this);
2785 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002786 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002787 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002788
2789 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002790 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002791 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002792 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002793 MultiLevelTemplateArgumentList(TemplateArgs)));
2794 if (!TempParm)
2795 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002796 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002797
Douglas Gregore7526412009-11-11 19:31:23 +00002798 switch (Arg.getArgument().getKind()) {
2799 case TemplateArgument::Null:
2800 assert(false && "Should never see a NULL template argument here");
2801 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002802
Douglas Gregore7526412009-11-11 19:31:23 +00002803 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002804 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002805 if (CheckTemplateArgument(TempParm, Arg))
2806 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002807
Douglas Gregor910f8002010-11-07 23:05:16 +00002808 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002809 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002810
Douglas Gregore7526412009-11-11 19:31:23 +00002811 case TemplateArgument::Expression:
2812 case TemplateArgument::Type:
2813 // We have a template template parameter but the template
2814 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002815 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2816 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002817 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002818
Douglas Gregore7526412009-11-11 19:31:23 +00002819 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002820 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002821 "Declaration argument with template template parameter");
2822 break;
2823 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002824 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002825 "Integral argument with template template parameter");
2826 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002827
Douglas Gregore7526412009-11-11 19:31:23 +00002828 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002829 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002830 break;
2831 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002832
Douglas Gregore7526412009-11-11 19:31:23 +00002833 return false;
2834}
2835
Douglas Gregorc15cb382009-02-09 23:23:08 +00002836/// \brief Check that the given template argument list is well-formed
2837/// for specializing the given template.
2838bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2839 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002840 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002841 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002842 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002843 TemplateParameterList *Params = Template->getTemplateParameters();
2844 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002845 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002846 bool Invalid = false;
2847
John McCalld5532b62009-11-23 01:53:49 +00002848 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2849
Mike Stump1eb44332009-09-09 15:08:12 +00002850 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002851 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002852
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002853 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002854 (NumArgs < Params->getMinRequiredArguments() &&
2855 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002856 // FIXME: point at either the first arg beyond what we can handle,
2857 // or the '>', depending on whether we have too many or too few
2858 // arguments.
2859 SourceRange Range;
2860 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002861 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002862 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2863 << (NumArgs > NumParams)
2864 << (isa<ClassTemplateDecl>(Template)? 0 :
2865 isa<FunctionTemplateDecl>(Template)? 1 :
2866 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2867 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002868 Diag(Template->getLocation(), diag::note_template_decl_here)
2869 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002870 Invalid = true;
2871 }
Mike Stump1eb44332009-09-09 15:08:12 +00002872
2873 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002874 // [...] The type and form of each template-argument specified in
2875 // a template-id shall match the type and form specified for the
2876 // corresponding parameter declared by the template in its
2877 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002878 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002879 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002880 TemplateParameterList::iterator Param = Params->begin(),
2881 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002882 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002883 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002884 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002885 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002886 // If we have an expanded parameter pack, make sure we don't have too
2887 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002888 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002889 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002890 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002891 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2892 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2893 << true
2894 << (isa<ClassTemplateDecl>(Template)? 0 :
2895 isa<FunctionTemplateDecl>(Template)? 1 :
2896 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2897 << Template;
2898 Diag(Template->getLocation(), diag::note_template_decl_here)
2899 << Params->getSourceRange();
2900 return true;
2901 }
2902 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002903
Douglas Gregorf35f8282009-11-11 21:54:23 +00002904 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002905 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2906 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002907 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002908 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002909
Douglas Gregor14be16b2010-12-20 16:57:52 +00002910 if ((*Param)->isTemplateParameterPack()) {
2911 // The template parameter was a template parameter pack, so take the
2912 // deduced argument and place it on the argument pack. Note that we
2913 // stay on the same template parameter so that we can deduce more
2914 // arguments.
2915 ArgumentPack.push_back(Converted.back());
2916 Converted.pop_back();
2917 } else {
2918 // Move to the next template parameter.
2919 ++Param;
2920 }
2921 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002922 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002923 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002924
Douglas Gregor8735b292011-06-03 02:59:40 +00002925 // If we're checking a partial template argument list, we're done.
2926 if (PartialTemplateArgs) {
2927 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2928 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2929 ArgumentPack.data(),
2930 ArgumentPack.size()));
2931
2932 return Invalid;
2933 }
2934
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002935 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002936 // arguments, just break out now and we'll fill in the argument pack below.
2937 if ((*Param)->isTemplateParameterPack())
2938 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002939
Douglas Gregorf35f8282009-11-11 21:54:23 +00002940 // We have a default template argument that we will use.
2941 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002942
Douglas Gregorf35f8282009-11-11 21:54:23 +00002943 // Retrieve the default template argument from the template
2944 // parameter. For each kind of template parameter, we substitute the
2945 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002946 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002947 // the default argument.
2948 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2949 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002950 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002951 break;
2952 }
2953
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002954 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002955 Template,
2956 TemplateLoc,
2957 RAngleLoc,
2958 TTP,
2959 Converted);
2960 if (!ArgType)
2961 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002962
Douglas Gregorf35f8282009-11-11 21:54:23 +00002963 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2964 ArgType);
2965 } else if (NonTypeTemplateParmDecl *NTTP
2966 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2967 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002968 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002969 break;
2970 }
2971
John McCall60d7b3a2010-08-24 06:29:42 +00002972 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002973 TemplateLoc,
2974 RAngleLoc,
2975 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002976 Converted);
2977 if (E.isInvalid())
2978 return true;
2979
2980 Expr *Ex = E.takeAs<Expr>();
2981 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2982 } else {
2983 TemplateTemplateParmDecl *TempParm
2984 = cast<TemplateTemplateParmDecl>(*Param);
2985
2986 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002987 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002988 break;
2989 }
2990
Douglas Gregor1d752d72011-03-02 18:46:51 +00002991 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002992 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002993 TemplateLoc,
2994 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002995 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002996 Converted,
2997 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002998 if (Name.isNull())
2999 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003000
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003001 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3002 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003003 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003004
Douglas Gregorf35f8282009-11-11 21:54:23 +00003005 // Introduce an instantiation record that describes where we are using
3006 // the default template argument.
3007 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003008 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003009 SourceRange(TemplateLoc, RAngleLoc));
3010
Douglas Gregorf35f8282009-11-11 21:54:23 +00003011 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003012 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003013 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003014 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003015
Douglas Gregor67714232011-03-03 02:41:12 +00003016 // Core issue 150 (assumed resolution): if this is a template template
3017 // parameter, keep track of the default template arguments from the
3018 // template definition.
3019 if (isTemplateTemplateParameter)
3020 TemplateArgs.addArgument(Arg);
3021
Douglas Gregor14be16b2010-12-20 16:57:52 +00003022 // Move to the next template parameter and argument.
3023 ++Param;
3024 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003025 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003026
Douglas Gregor14be16b2010-12-20 16:57:52 +00003027 // Form argument packs for each of the parameter packs remaining.
3028 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003029 // If we're checking a partial list of template arguments, don't fill
3030 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031
3032 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003033 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003034 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003035 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003036 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3037 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003038 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003039 ArgumentPack.clear();
3040 }
3041 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003042
Douglas Gregor14be16b2010-12-20 16:57:52 +00003043 ++Param;
3044 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003045
Douglas Gregorc15cb382009-02-09 23:23:08 +00003046 return Invalid;
3047}
3048
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003049namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003050 class UnnamedLocalNoLinkageFinder
3051 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003052 {
3053 Sema &S;
3054 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003055
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003056 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003057
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003058 public:
3059 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3060
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003061 bool Visit(QualType T) {
3062 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003063 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003064
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003065#define TYPE(Class, Parent) \
3066 bool Visit##Class##Type(const Class##Type *);
3067#define ABSTRACT_TYPE(Class, Parent) \
3068 bool Visit##Class##Type(const Class##Type *) { return false; }
3069#define NON_CANONICAL_TYPE(Class, Parent) \
3070 bool Visit##Class##Type(const Class##Type *) { return false; }
3071#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003072
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003073 bool VisitTagDecl(const TagDecl *Tag);
3074 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3075 };
3076}
3077
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003078bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003079 return false;
3080}
3081
3082bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3083 return Visit(T->getElementType());
3084}
3085
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003086bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003087 return Visit(T->getPointeeType());
3088}
3089
3090bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003091 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003092 return Visit(T->getPointeeType());
3093}
3094
3095bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003096 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003097 return Visit(T->getPointeeType());
3098}
3099
3100bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003101 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003102 return Visit(T->getPointeeType());
3103}
3104
3105bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003106 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003107 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3108}
3109
3110bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003111 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003112 return Visit(T->getElementType());
3113}
3114
3115bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003116 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003117 return Visit(T->getElementType());
3118}
3119
3120bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003121 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003122 return Visit(T->getElementType());
3123}
3124
3125bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003126 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003127 return Visit(T->getElementType());
3128}
3129
3130bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003131 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003132 return Visit(T->getElementType());
3133}
3134
3135bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3136 return Visit(T->getElementType());
3137}
3138
3139bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3140 return Visit(T->getElementType());
3141}
3142
3143bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3144 const FunctionProtoType* T) {
3145 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003146 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003147 A != AEnd; ++A) {
3148 if (Visit(*A))
3149 return true;
3150 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003151
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003152 return Visit(T->getResultType());
3153}
3154
3155bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3156 const FunctionNoProtoType* T) {
3157 return Visit(T->getResultType());
3158}
3159
3160bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3161 const UnresolvedUsingType*) {
3162 return false;
3163}
3164
3165bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3166 return false;
3167}
3168
3169bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3170 return Visit(T->getUnderlyingType());
3171}
3172
3173bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3174 return false;
3175}
3176
Sean Huntca63c202011-05-24 22:41:36 +00003177bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3178 const UnaryTransformType*) {
3179 return false;
3180}
3181
Richard Smith34b41d92011-02-20 03:19:35 +00003182bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3183 return Visit(T->getDeducedType());
3184}
3185
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003186bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3187 return VisitTagDecl(T->getDecl());
3188}
3189
3190bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3191 return VisitTagDecl(T->getDecl());
3192}
3193
3194bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3195 const TemplateTypeParmType*) {
3196 return false;
3197}
3198
Douglas Gregorc3069d62011-01-14 02:55:32 +00003199bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3200 const SubstTemplateTypeParmPackType *) {
3201 return false;
3202}
3203
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003204bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3205 const TemplateSpecializationType*) {
3206 return false;
3207}
3208
3209bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3210 const InjectedClassNameType* T) {
3211 return VisitTagDecl(T->getDecl());
3212}
3213
3214bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3215 const DependentNameType* T) {
3216 return VisitNestedNameSpecifier(T->getQualifier());
3217}
3218
3219bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3220 const DependentTemplateSpecializationType* T) {
3221 return VisitNestedNameSpecifier(T->getQualifier());
3222}
3223
Douglas Gregor7536dd52010-12-20 02:24:11 +00003224bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3225 const PackExpansionType* T) {
3226 return Visit(T->getPattern());
3227}
3228
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003229bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3230 return false;
3231}
3232
3233bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3234 const ObjCInterfaceType *) {
3235 return false;
3236}
3237
3238bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3239 const ObjCObjectPointerType *) {
3240 return false;
3241}
3242
3243bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3244 if (Tag->getDeclContext()->isFunctionOrMethod()) {
3245 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
3246 << S.Context.getTypeDeclType(Tag) << SR;
3247 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003248 }
3249
Richard Smith162e1c12011-04-15 14:24:37 +00003250 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003251 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
3252 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3253 return true;
3254 }
3255
3256 return false;
3257}
3258
3259bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3260 NestedNameSpecifier *NNS) {
3261 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3262 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003263
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003264 switch (NNS->getKind()) {
3265 case NestedNameSpecifier::Identifier:
3266 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003267 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003268 case NestedNameSpecifier::Global:
3269 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003270
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003271 case NestedNameSpecifier::TypeSpec:
3272 case NestedNameSpecifier::TypeSpecWithTemplate:
3273 return Visit(QualType(NNS->getAsType(), 0));
3274 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003275 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003276}
3277
3278
Douglas Gregorc15cb382009-02-09 23:23:08 +00003279/// \brief Check a template argument against its corresponding
3280/// template type parameter.
3281///
3282/// This routine implements the semantics of C++ [temp.arg.type]. It
3283/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003284bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003285 TypeSourceInfo *ArgInfo) {
3286 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003287 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003288 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003289
3290 if (Arg->isVariablyModifiedType()) {
3291 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003292 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003293 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003294 }
3295
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003296 // C++03 [temp.arg.type]p2:
3297 // A local type, a type with no linkage, an unnamed type or a type
3298 // compounded from any of these types shall not be used as a
3299 // template-argument for a template type-parameter.
3300 //
3301 // C++0x allows these, and even in C++03 we allow them as an extension with
3302 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003303 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003304 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3305 (void)Finder.Visit(Context.getCanonicalType(Arg));
3306 }
3307
Douglas Gregorc15cb382009-02-09 23:23:08 +00003308 return false;
3309}
3310
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003311/// \brief Checks whether the given template argument is the address
3312/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003313static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003314CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3315 NonTypeTemplateParmDecl *Param,
3316 QualType ParamType,
3317 Expr *ArgIn,
3318 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003319 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003320 Expr *Arg = ArgIn;
3321 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003322
3323 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003324 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003325
3326 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003327 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003328 // A template-argument for a non-type, non-template
3329 // template-parameter shall be one of: [...]
3330 //
3331 // -- the address of an object or function with external
3332 // linkage, including function templates and function
3333 // template-ids but excluding non-static class members,
3334 // expressed as & id-expression where the & is optional if
3335 // the name refers to a function or array, or if the
3336 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003337
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003338 // In C++98/03 mode, give an extension warning on any extra parentheses.
3339 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3340 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003341 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003342 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003343 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003344 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003345 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003346 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003347 }
3348
3349 Arg = Parens->getSubExpr();
3350 }
3351
John McCall91a57552011-07-15 05:09:51 +00003352 while (SubstNonTypeTemplateParmExpr *subst =
3353 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3354 Arg = subst->getReplacement()->IgnoreImpCasts();
3355
Douglas Gregorb7a09262010-04-01 18:32:35 +00003356 bool AddressTaken = false;
3357 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003358 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003359 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003360 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003361 AddressTaken = true;
3362 AddrOpLoc = UnOp->getOperatorLoc();
3363 }
Francois Picheta343a412011-04-29 09:08:14 +00003364 }
John McCall91a57552011-07-15 05:09:51 +00003365
3366 if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) {
3367 Converted = TemplateArgument(ArgIn);
3368 return false;
3369 }
3370
3371 while (SubstNonTypeTemplateParmExpr *subst =
3372 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3373 Arg = subst->getReplacement()->IgnoreImpCasts();
3374
3375 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003376 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003377 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3378 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003379 S.Diag(Param->getLocation(), diag::note_template_param_here);
3380 return true;
3381 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003382
3383 // Stop checking the precise nature of the argument if it is value dependent,
3384 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003385 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003386 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003387 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003388 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003389
Douglas Gregorb7a09262010-04-01 18:32:35 +00003390 if (!isa<ValueDecl>(DRE->getDecl())) {
3391 S.Diag(Arg->getSourceRange().getBegin(),
3392 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003393 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003394 S.Diag(Param->getLocation(), diag::note_template_param_here);
3395 return true;
3396 }
3397
3398 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003399
3400 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003401 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3402 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003403 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003404 S.Diag(Param->getLocation(), diag::note_template_param_here);
3405 return true;
3406 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003407
3408 // Cannot refer to non-static member functions
3409 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003410 if (!Method->isStatic()) {
3411 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003412 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003413 S.Diag(Param->getLocation(), diag::note_template_param_here);
3414 return true;
3415 }
Mike Stump1eb44332009-09-09 15:08:12 +00003416
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003417 // Functions must have external linkage.
3418 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003419 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003420 S.Diag(Arg->getSourceRange().getBegin(),
3421 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003422 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003423 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003424 << true;
3425 return true;
3426 }
3427
3428 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003429 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003430
Douglas Gregorb7a09262010-04-01 18:32:35 +00003431 // If the template parameter has pointer type, the function decays.
3432 if (ParamType->isPointerType() && !AddressTaken)
3433 ArgType = S.Context.getPointerType(Func->getType());
3434 else if (AddressTaken && ParamType->isReferenceType()) {
3435 // If we originally had an address-of operator, but the
3436 // parameter has reference type, complain and (if things look
3437 // like they will work) drop the address-of operator.
3438 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3439 ParamType.getNonReferenceType())) {
3440 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3441 << ParamType;
3442 S.Diag(Param->getLocation(), diag::note_template_param_here);
3443 return true;
3444 }
3445
3446 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3447 << ParamType
3448 << FixItHint::CreateRemoval(AddrOpLoc);
3449 S.Diag(Param->getLocation(), diag::note_template_param_here);
3450
3451 ArgType = Func->getType();
3452 }
3453 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003454 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003455 S.Diag(Arg->getSourceRange().getBegin(),
3456 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003457 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003458 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003459 << true;
3460 return true;
3461 }
3462
Douglas Gregorb7a09262010-04-01 18:32:35 +00003463 // A value of reference type is not an object.
3464 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003465 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003466 diag::err_template_arg_reference_var)
3467 << Var->getType() << Arg->getSourceRange();
3468 S.Diag(Param->getLocation(), diag::note_template_param_here);
3469 return true;
3470 }
3471
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003472 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003473 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003474
3475 // If the template parameter has pointer type, we must have taken
3476 // the address of this object.
3477 if (ParamType->isReferenceType()) {
3478 if (AddressTaken) {
3479 // If we originally had an address-of operator, but the
3480 // parameter has reference type, complain and (if things look
3481 // like they will work) drop the address-of operator.
3482 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3483 ParamType.getNonReferenceType())) {
3484 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3485 << ParamType;
3486 S.Diag(Param->getLocation(), diag::note_template_param_here);
3487 return true;
3488 }
3489
3490 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3491 << ParamType
3492 << FixItHint::CreateRemoval(AddrOpLoc);
3493 S.Diag(Param->getLocation(), diag::note_template_param_here);
3494
3495 ArgType = Var->getType();
3496 }
3497 } else if (!AddressTaken && ParamType->isPointerType()) {
3498 if (Var->getType()->isArrayType()) {
3499 // Array-to-pointer decay.
3500 ArgType = S.Context.getArrayDecayedType(Var->getType());
3501 } else {
3502 // If the template parameter has pointer type but the address of
3503 // this object was not taken, complain and (possibly) recover by
3504 // taking the address of the entity.
3505 ArgType = S.Context.getPointerType(Var->getType());
3506 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3507 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3508 << ParamType;
3509 S.Diag(Param->getLocation(), diag::note_template_param_here);
3510 return true;
3511 }
3512
3513 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3514 << ParamType
3515 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3516
3517 S.Diag(Param->getLocation(), diag::note_template_param_here);
3518 }
3519 }
3520 } else {
3521 // We found something else, but we don't know specifically what it is.
3522 S.Diag(Arg->getSourceRange().getBegin(),
3523 diag::err_template_arg_not_object_or_func)
3524 << Arg->getSourceRange();
3525 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3526 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003527 }
Mike Stump1eb44332009-09-09 15:08:12 +00003528
John McCallf85e1932011-06-15 23:02:42 +00003529 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003530 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003531 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003532 S.IsQualificationConversion(ArgType, ParamType, false,
3533 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003534 // For pointer-to-object types, qualification conversions are
3535 // permitted.
3536 } else {
3537 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3538 if (!ParamRef->getPointeeType()->isFunctionType()) {
3539 // C++ [temp.arg.nontype]p5b3:
3540 // For a non-type template-parameter of type reference to
3541 // object, no conversions apply. The type referred to by the
3542 // reference may be more cv-qualified than the (otherwise
3543 // identical) type of the template- argument. The
3544 // template-parameter is bound directly to the
3545 // template-argument, which shall be an lvalue.
3546
3547 // FIXME: Other qualifiers?
3548 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3549 unsigned ArgQuals = ArgType.getCVRQualifiers();
3550
3551 if ((ParamQuals | ArgQuals) != ParamQuals) {
3552 S.Diag(Arg->getSourceRange().getBegin(),
3553 diag::err_template_arg_ref_bind_ignores_quals)
3554 << ParamType << Arg->getType()
3555 << Arg->getSourceRange();
3556 S.Diag(Param->getLocation(), diag::note_template_param_here);
3557 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003558 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003559 }
3560 }
3561
3562 // At this point, the template argument refers to an object or
3563 // function with external linkage. We now need to check whether the
3564 // argument and parameter types are compatible.
3565 if (!S.Context.hasSameUnqualifiedType(ArgType,
3566 ParamType.getNonReferenceType())) {
3567 // We can't perform this conversion or binding.
3568 if (ParamType->isReferenceType())
3569 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003570 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003571 else
3572 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003573 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003574 S.Diag(Param->getLocation(), diag::note_template_param_here);
3575 return true;
3576 }
3577 }
3578
3579 // Create the template argument.
3580 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003581 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003582 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003583}
3584
3585/// \brief Checks whether the given template argument is a pointer to
3586/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003587bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003588 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003589 bool Invalid = false;
3590
3591 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003592 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003593 Arg = Cast->getSubExpr();
3594
3595 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003596 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003597 // A template-argument for a non-type, non-template
3598 // template-parameter shall be one of: [...]
3599 //
3600 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003601 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003602
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003603 // In C++98/03 mode, give an extension warning on any extra parentheses.
3604 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3605 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003606 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003607 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003608 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003609 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003610 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003611 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003612 }
3613
3614 Arg = Parens->getSubExpr();
3615 }
3616
John McCall91a57552011-07-15 05:09:51 +00003617 while (SubstNonTypeTemplateParmExpr *subst =
3618 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3619 Arg = subst->getReplacement()->IgnoreImpCasts();
3620
Douglas Gregorcaddba02009-11-12 18:38:13 +00003621 // A pointer-to-member constant written &Class::member.
3622 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003623 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003624 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3625 if (DRE && !DRE->getQualifier())
3626 DRE = 0;
3627 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003628 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003629 // A constant of pointer-to-member type.
3630 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3631 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3632 if (VD->getType()->isMemberPointerType()) {
3633 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003634 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003635 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3636 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003637 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003638 else
3639 Converted = TemplateArgument(VD->getCanonicalDecl());
3640 return Invalid;
3641 }
3642 }
3643 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003644
Douglas Gregorcaddba02009-11-12 18:38:13 +00003645 DRE = 0;
3646 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003647
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003648 if (!DRE)
3649 return Diag(Arg->getSourceRange().getBegin(),
3650 diag::err_template_arg_not_pointer_to_member_form)
3651 << Arg->getSourceRange();
3652
3653 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3654 assert((isa<FieldDecl>(DRE->getDecl()) ||
3655 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3656 "Only non-static member pointers can make it here");
3657
3658 // Okay: this is the address of a non-static member, and therefore
3659 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003660 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003661 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003662 else
3663 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003664 return Invalid;
3665 }
3666
3667 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003668 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003669 diag::err_template_arg_not_pointer_to_member_form)
3670 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003671 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003672 diag::note_template_arg_refers_here);
3673 return true;
3674}
3675
Douglas Gregorc15cb382009-02-09 23:23:08 +00003676/// \brief Check a template argument against its corresponding
3677/// non-type template parameter.
3678///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003679/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003680/// If an error occurred, it returns ExprError(); otherwise, it
3681/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003682/// InstantiatedParamType is the type of the non-type template
3683/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003684ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3685 QualType InstantiatedParamType, Expr *Arg,
3686 TemplateArgument &Converted,
3687 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003688 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3689
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003690 // If either the parameter has a dependent type or the argument is
3691 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003692 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3693 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003694 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003695 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003696 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003697
3698 // C++ [temp.arg.nontype]p5:
3699 // The following conversions are performed on each expression used
3700 // as a non-type template-argument. If a non-type
3701 // template-argument cannot be converted to the type of the
3702 // corresponding template-parameter then the program is
3703 // ill-formed.
3704 //
3705 // -- for a non-type template-parameter of integral or
3706 // enumeration type, integral promotions (4.5) and integral
3707 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003708 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003709 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003710 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003711 // C++ [temp.arg.nontype]p1:
3712 // A template-argument for a non-type, non-template
3713 // template-parameter shall be one of:
3714 //
3715 // -- an integral constant-expression of integral or enumeration
3716 // type; or
3717 // -- the name of a non-type template-parameter; or
3718 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003719 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003720 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003721 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003722 diag::err_template_arg_not_integral_or_enumeral)
3723 << ArgType << Arg->getSourceRange();
3724 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003725 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003726 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003727 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003728 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3729 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003730 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003731 }
3732
Douglas Gregor02024a92010-03-28 02:42:43 +00003733 // From here on out, all we care about are the unqualified forms
3734 // of the parameter and argument types.
3735 ParamType = ParamType.getUnqualifiedType();
3736 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003737
3738 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003739 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003740 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003741 } else if (CTAK == CTAK_Deduced) {
3742 // C++ [temp.deduct.type]p17:
3743 // If, in the declaration of a function template with a non-type
3744 // template-parameter, the non-type template- parameter is used
3745 // in an expression in the function parameter-list and, if the
3746 // corresponding template-argument is deduced, the
3747 // template-argument type shall match the type of the
3748 // template-parameter exactly, except that a template-argument
3749 // deduced from an array bound may be of any integral type.
3750 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3751 << ArgType << ParamType;
3752 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003753 return ExprError();
John McCalldaa8e4e2010-11-15 09:13:47 +00003754 } else if (ParamType->isBooleanType()) {
3755 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003756 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003757 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3758 !ParamType->isEnumeralType()) {
3759 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003760 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003761 } else {
3762 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003763 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003764 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003765 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003766 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003767 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003768 }
3769
Douglas Gregorc7469372011-05-04 21:55:00 +00003770 // Add the value of this argument to the list of converted
3771 // arguments. We use the bitwidth and signedness of the template
3772 // parameter.
3773 if (Arg->isValueDependent()) {
3774 // The argument is value-dependent. Create a new
3775 // TemplateArgument with the converted expression.
3776 Converted = TemplateArgument(Arg);
3777 return Owned(Arg);
3778 }
3779
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003780 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003781 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003782 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003783
Douglas Gregorc7469372011-05-04 21:55:00 +00003784 if (ParamType->isBooleanType()) {
3785 // Value must be zero or one.
3786 Value = Value != 0;
3787 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3788 if (Value.getBitWidth() != AllowedBits)
3789 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003790 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003791 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003792 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003793
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003794 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003795 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003796 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003797 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003798 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003799 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003800
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003801 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003802 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003803 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003804 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3805 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3806 << Arg->getSourceRange();
3807 Diag(Param->getLocation(), diag::note_template_param_here);
3808 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003809
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003810 // Complain if we overflowed the template parameter's type.
3811 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003812 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003813 RequiredBits = OldValue.getActiveBits();
3814 else if (OldValue.isUnsigned())
3815 RequiredBits = OldValue.getActiveBits() + 1;
3816 else
3817 RequiredBits = OldValue.getMinSignedBits();
3818 if (RequiredBits > AllowedBits) {
3819 Diag(Arg->getSourceRange().getBegin(),
3820 diag::warn_template_arg_too_large)
3821 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3822 << Arg->getSourceRange();
3823 Diag(Param->getLocation(), diag::note_template_param_here);
3824 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003825 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003826
John McCall833ca992009-10-29 08:12:44 +00003827 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003828 ParamType->isEnumeralType()
3829 ? Context.getCanonicalType(ParamType)
3830 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003831 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003832 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003833
John McCall6bb80172010-03-30 21:47:33 +00003834 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3835
Douglas Gregorb7a09262010-04-01 18:32:35 +00003836 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3837 // from a template argument of type std::nullptr_t to a non-type
3838 // template parameter of type pointer to object, pointer to
3839 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003840 if (ArgType->isNullPtrType()) {
3841 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3842 Converted = TemplateArgument((NamedDecl *)0);
3843 return Owned(Arg);
3844 }
3845
3846 if (ParamType->isNullPtrType()) {
3847 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3848 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3849 return Owned(Arg);
3850 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003851 }
3852
Douglas Gregorb86b0572009-02-11 01:18:59 +00003853 // Handle pointer-to-function, reference-to-function, and
3854 // pointer-to-member-function all in (roughly) the same way.
3855 if (// -- For a non-type template-parameter of type pointer to
3856 // function, only the function-to-pointer conversion (4.3) is
3857 // applied. If the template-argument represents a set of
3858 // overloaded functions (or a pointer to such), the matching
3859 // function is selected from the set (13.4).
3860 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003861 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003862 // -- For a non-type template-parameter of type reference to
3863 // function, no conversions apply. If the template-argument
3864 // represents a set of overloaded functions, the matching
3865 // function is selected from the set (13.4).
3866 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003867 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003868 // -- For a non-type template-parameter of type pointer to
3869 // member function, no conversions apply. If the
3870 // template-argument represents a set of overloaded member
3871 // functions, the matching member function is selected from
3872 // the set (13.4).
3873 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003874 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003875 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003876
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003877 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003878 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003879 true,
3880 FoundResult)) {
3881 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003882 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003883
3884 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3885 ArgType = Arg->getType();
3886 } else
John Wiegley429bb272011-04-08 18:41:53 +00003887 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003888 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003889
John Wiegley429bb272011-04-08 18:41:53 +00003890 if (!ParamType->isMemberPointerType()) {
3891 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3892 ParamType,
3893 Arg, Converted))
3894 return ExprError();
3895 return Owned(Arg);
3896 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003897
John McCallf85e1932011-06-15 23:02:42 +00003898 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003899 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00003900 false, ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003901 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003902 } else if (!Context.hasSameUnqualifiedType(ArgType,
3903 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003904 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003905 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003906 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003907 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003908 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003909 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003910 }
Mike Stump1eb44332009-09-09 15:08:12 +00003911
John Wiegley429bb272011-04-08 18:41:53 +00003912 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3913 return ExprError();
3914 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00003915 }
3916
Chris Lattnerfe90de72009-02-20 21:37:53 +00003917 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003918 // -- for a non-type template-parameter of type pointer to
3919 // object, qualification conversions (4.4) and the
3920 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003921 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003922 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003923 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003924
John Wiegley429bb272011-04-08 18:41:53 +00003925 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3926 ParamType,
3927 Arg, Converted))
3928 return ExprError();
3929 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003930 }
Mike Stump1eb44332009-09-09 15:08:12 +00003931
Ted Kremenek6217b802009-07-29 21:53:49 +00003932 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003933 // -- For a non-type template-parameter of type reference to
3934 // object, no conversions apply. The type referred to by the
3935 // reference may be more cv-qualified than the (otherwise
3936 // identical) type of the template-argument. The
3937 // template-parameter is bound directly to the
3938 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003939 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003940 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003941
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003942 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003943 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3944 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003945 true,
3946 FoundResult)) {
3947 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003948 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003949
3950 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3951 ArgType = Arg->getType();
3952 } else
John Wiegley429bb272011-04-08 18:41:53 +00003953 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00003954 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003955
John Wiegley429bb272011-04-08 18:41:53 +00003956 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3957 ParamType,
3958 Arg, Converted))
3959 return ExprError();
3960 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003961 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003962
3963 // -- For a non-type template-parameter of type pointer to data
3964 // member, qualification conversions (4.4) are applied.
3965 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3966
John McCallf85e1932011-06-15 23:02:42 +00003967 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003968 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003969 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00003970 } else if (IsQualificationConversion(ArgType, ParamType, false,
3971 ObjCLifetimeConversion)) {
John Wiegley429bb272011-04-08 18:41:53 +00003972 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003973 } else {
3974 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003975 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003976 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003977 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003978 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003979 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003980 }
3981
John Wiegley429bb272011-04-08 18:41:53 +00003982 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
3983 return ExprError();
3984 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003985}
3986
3987/// \brief Check a template argument against its corresponding
3988/// template template parameter.
3989///
3990/// This routine implements the semantics of C++ [temp.arg.template].
3991/// It returns true if an error occurred, and false otherwise.
3992bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003993 const TemplateArgumentLoc &Arg) {
3994 TemplateName Name = Arg.getArgument().getAsTemplate();
3995 TemplateDecl *Template = Name.getAsTemplateDecl();
3996 if (!Template) {
3997 // Any dependent template name is fine.
3998 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3999 return false;
4000 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004001
Richard Smith3e4c6c42011-05-05 21:57:07 +00004002 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004003 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004004 // the name of a class template or an alias template, expressed as an
4005 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004006 // primary class templates are considered when matching the
4007 // template template argument with the corresponding parameter;
4008 // partial specializations are not considered even if their
4009 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004010 //
4011 // Note that we also allow template template parameters here, which
4012 // will happen when we are dealing with, e.g., class template
4013 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004014 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004015 !isa<TemplateTemplateParmDecl>(Template) &&
4016 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004017 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004018 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004019 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004020 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004021 << Template;
4022 }
4023
4024 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4025 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004026 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004027 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004028 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004029}
4030
Douglas Gregor02024a92010-03-28 02:42:43 +00004031/// \brief Given a non-type template argument that refers to a
4032/// declaration and the type of its corresponding non-type template
4033/// parameter, produce an expression that properly refers to that
4034/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004035ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004036Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4037 QualType ParamType,
4038 SourceLocation Loc) {
4039 assert(Arg.getKind() == TemplateArgument::Declaration &&
4040 "Only declaration template arguments permitted here");
4041 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4042
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004043 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004044 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4045 // If the value is a class member, we might have a pointer-to-member.
4046 // Determine whether the non-type template template parameter is of
4047 // pointer-to-member type. If so, we need to build an appropriate
4048 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4049 // would refer to the member itself.
4050 if (ParamType->isMemberPointerType()) {
4051 QualType ClassType
4052 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4053 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004054 = NestedNameSpecifier::Create(Context, 0, false,
4055 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004056 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004057 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004058
4059 // The actual value-ness of this is unimportant, but for
4060 // internal consistency's sake, references to instance methods
4061 // are r-values.
4062 ExprValueKind VK = VK_LValue;
4063 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4064 VK = VK_RValue;
4065
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004066 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004067 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004068 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004069 Loc,
4070 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004071 if (RefExpr.isInvalid())
4072 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004073
John McCall2de56d12010-08-25 11:45:40 +00004074 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004075
Douglas Gregorc0c83002010-04-30 21:46:38 +00004076 // We might need to perform a trailing qualification conversion, since
4077 // the element type on the parameter could be more qualified than the
4078 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004079 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004080 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004081 ParamType.getUnqualifiedType(), false,
4082 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004083 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004084
Douglas Gregor02024a92010-03-28 02:42:43 +00004085 assert(!RefExpr.isInvalid() &&
4086 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004087 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004088 return move(RefExpr);
4089 }
4090 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004091
Douglas Gregor02024a92010-03-28 02:42:43 +00004092 QualType T = VD->getType().getNonReferenceType();
4093 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004094 // When the non-type template parameter is a pointer, take the
4095 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004096 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004097 if (RefExpr.isInvalid())
4098 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004099
4100 if (T->isFunctionType() || T->isArrayType()) {
4101 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004102 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4103 if (RefExpr.isInvalid())
4104 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004105
4106 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004107 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004108
Douglas Gregorb7a09262010-04-01 18:32:35 +00004109 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004110 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004111 }
4112
John McCallf89e55a2010-11-18 06:31:45 +00004113 ExprValueKind VK = VK_RValue;
4114
Douglas Gregor02024a92010-03-28 02:42:43 +00004115 // If the non-type template parameter has reference type, qualify the
4116 // resulting declaration reference with the extra qualifiers on the
4117 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004118 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4119 VK = VK_LValue;
4120 T = Context.getQualifiedType(T,
4121 TargetRef->getPointeeType().getQualifiers());
4122 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004123
John McCallf89e55a2010-11-18 06:31:45 +00004124 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004125}
4126
4127/// \brief Construct a new expression that refers to the given
4128/// integral template argument with the given source-location
4129/// information.
4130///
4131/// This routine takes care of the mapping from an integral template
4132/// argument (which may have any integral type) to the appropriate
4133/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004134ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004135Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4136 SourceLocation Loc) {
4137 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004138 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004139 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004140 if (T->isAnyCharacterType()) {
4141 CharacterLiteral::CharacterKind Kind;
4142 if (T->isWideCharType())
4143 Kind = CharacterLiteral::Wide;
4144 else if (T->isChar16Type())
4145 Kind = CharacterLiteral::UTF16;
4146 else if (T->isChar32Type())
4147 Kind = CharacterLiteral::UTF32;
4148 else
4149 Kind = CharacterLiteral::Ascii;
4150
Douglas Gregor02024a92010-03-28 02:42:43 +00004151 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004152 Arg.getAsIntegral()->getZExtValue(),
4153 Kind, T, Loc));
4154 }
4155
Douglas Gregor02024a92010-03-28 02:42:43 +00004156 if (T->isBooleanType())
4157 return Owned(new (Context) CXXBoolLiteralExpr(
4158 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004159 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004160
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004161 if (T->isNullPtrType())
4162 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4163
Chris Lattner223de242011-04-25 20:37:58 +00004164 // If this is an enum type that we're instantiating, we need to use an integer
4165 // type the same size as the enumerator. We don't want to build an
4166 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004167 QualType BT;
4168 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004169 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004170 else
4171 BT = T;
4172
John McCall4e9272d2011-07-15 07:47:58 +00004173 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4174 if (T->isEnumeralType()) {
4175 // FIXME: This is a hack. We need a better way to handle substituted
4176 // non-type template parameters.
4177 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4178 Context.getTrivialTypeSourceInfo(T, Loc),
4179 Loc, Loc);
4180 }
4181
4182 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004183}
4184
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004185/// \brief Match two template parameters within template parameter lists.
4186static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4187 bool Complain,
4188 Sema::TemplateParameterListEqualKind Kind,
4189 SourceLocation TemplateArgLoc) {
4190 // Check the actual kind (type, non-type, template).
4191 if (Old->getKind() != New->getKind()) {
4192 if (Complain) {
4193 unsigned NextDiag = diag::err_template_param_different_kind;
4194 if (TemplateArgLoc.isValid()) {
4195 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4196 NextDiag = diag::note_template_param_different_kind;
4197 }
4198 S.Diag(New->getLocation(), NextDiag)
4199 << (Kind != Sema::TPL_TemplateMatch);
4200 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4201 << (Kind != Sema::TPL_TemplateMatch);
4202 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004203
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004204 return false;
4205 }
4206
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004207 // Check that both are parameter packs are neither are parameter packs.
4208 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004209 // template template parameter, the template template parameter can have
4210 // a parameter pack where the template template argument does not.
4211 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4212 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4213 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004214 if (Complain) {
4215 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4216 if (TemplateArgLoc.isValid()) {
4217 S.Diag(TemplateArgLoc,
4218 diag::err_template_arg_template_params_mismatch);
4219 NextDiag = diag::note_template_parameter_pack_non_pack;
4220 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004221
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004222 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4223 : isa<NonTypeTemplateParmDecl>(New)? 1
4224 : 2;
4225 S.Diag(New->getLocation(), NextDiag)
4226 << ParamKind << New->isParameterPack();
4227 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4228 << ParamKind << Old->isParameterPack();
4229 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004230
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004231 return false;
4232 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004233
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004234 // For non-type template parameters, check the type of the parameter.
4235 if (NonTypeTemplateParmDecl *OldNTTP
4236 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4237 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004238
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004239 // If we are matching a template template argument to a template
4240 // template parameter and one of the non-type template parameter types
4241 // is dependent, then we must wait until template instantiation time
4242 // to actually compare the arguments.
4243 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4244 (OldNTTP->getType()->isDependentType() ||
4245 NewNTTP->getType()->isDependentType()))
4246 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004247
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004248 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4249 if (Complain) {
4250 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4251 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004252 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004253 diag::err_template_arg_template_params_mismatch);
4254 NextDiag = diag::note_template_nontype_parm_different_type;
4255 }
4256 S.Diag(NewNTTP->getLocation(), NextDiag)
4257 << NewNTTP->getType()
4258 << (Kind != Sema::TPL_TemplateMatch);
4259 S.Diag(OldNTTP->getLocation(),
4260 diag::note_template_nontype_parm_prev_declaration)
4261 << OldNTTP->getType();
4262 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004263
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004264 return false;
4265 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004266
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004267 return true;
4268 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004269
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004270 // For template template parameters, check the template parameter types.
4271 // The template parameter lists of template template
4272 // parameters must agree.
4273 if (TemplateTemplateParmDecl *OldTTP
4274 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004275 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004276 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4277 OldTTP->getTemplateParameters(),
4278 Complain,
4279 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004280 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004281 : Kind),
4282 TemplateArgLoc);
4283 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004284
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004285 return true;
4286}
Douglas Gregor02024a92010-03-28 02:42:43 +00004287
Douglas Gregora0347822011-01-13 00:08:50 +00004288/// \brief Diagnose a known arity mismatch when comparing template argument
4289/// lists.
4290static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004291void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004292 TemplateParameterList *New,
4293 TemplateParameterList *Old,
4294 Sema::TemplateParameterListEqualKind Kind,
4295 SourceLocation TemplateArgLoc) {
4296 unsigned NextDiag = diag::err_template_param_list_different_arity;
4297 if (TemplateArgLoc.isValid()) {
4298 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4299 NextDiag = diag::note_template_param_list_different_arity;
4300 }
4301 S.Diag(New->getTemplateLoc(), NextDiag)
4302 << (New->size() > Old->size())
4303 << (Kind != Sema::TPL_TemplateMatch)
4304 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4305 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4306 << (Kind != Sema::TPL_TemplateMatch)
4307 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4308}
4309
Douglas Gregorddc29e12009-02-06 22:42:48 +00004310/// \brief Determine whether the given template parameter lists are
4311/// equivalent.
4312///
Mike Stump1eb44332009-09-09 15:08:12 +00004313/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004314/// source code as part of a new template declaration.
4315///
4316/// \param Old The old template parameter list, typically found via
4317/// name lookup of the template declared with this template parameter
4318/// list.
4319///
4320/// \param Complain If true, this routine will produce a diagnostic if
4321/// the template parameter lists are not equivalent.
4322///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004323/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004324///
4325/// \param TemplateArgLoc If this source location is valid, then we
4326/// are actually checking the template parameter list of a template
4327/// argument (New) against the template parameter list of its
4328/// corresponding template template parameter (Old). We produce
4329/// slightly different diagnostics in this scenario.
4330///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004331/// \returns True if the template parameter lists are equal, false
4332/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004333bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004334Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4335 TemplateParameterList *Old,
4336 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004337 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004338 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004339 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4340 if (Complain)
4341 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4342 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004343
4344 return false;
4345 }
4346
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004347 // C++0x [temp.arg.template]p3:
4348 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004349 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004350 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004351 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004352 // template-parameter-list of P. [...]
4353 TemplateParameterList::iterator NewParm = New->begin();
4354 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004355 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004356 OldParmEnd = Old->end();
4357 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004358 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4359 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004360 if (NewParm == NewParmEnd) {
4361 if (Complain)
4362 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4363 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004364
Douglas Gregora0347822011-01-13 00:08:50 +00004365 return false;
4366 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004367
Douglas Gregora0347822011-01-13 00:08:50 +00004368 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4369 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004370 return false;
4371
Douglas Gregora0347822011-01-13 00:08:50 +00004372 ++NewParm;
4373 continue;
4374 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004375
Douglas Gregora0347822011-01-13 00:08:50 +00004376 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004377 // [...] When P's template- parameter-list contains a template parameter
4378 // pack (14.5.3), the template parameter pack will match zero or more
4379 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004380 // template-parameter-list of A with the same type and form as the
4381 // template parameter pack in P (ignoring whether those template
4382 // parameters are template parameter packs).
4383 for (; NewParm != NewParmEnd; ++NewParm) {
4384 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4385 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004386 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004387 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004388 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004389
Douglas Gregora0347822011-01-13 00:08:50 +00004390 // Make sure we exhausted all of the arguments.
4391 if (NewParm != NewParmEnd) {
4392 if (Complain)
4393 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4394 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004395
Douglas Gregora0347822011-01-13 00:08:50 +00004396 return false;
4397 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004398
Douglas Gregorddc29e12009-02-06 22:42:48 +00004399 return true;
4400}
4401
4402/// \brief Check whether a template can be declared within this scope.
4403///
4404/// If the template declaration is valid in this scope, returns
4405/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004406bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004407Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004408 // Find the nearest enclosing declaration scope.
4409 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4410 (S->getFlags() & Scope::TemplateParamScope) != 0)
4411 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004412
Douglas Gregorddc29e12009-02-06 22:42:48 +00004413 // C++ [temp]p2:
4414 // A template-declaration can appear only as a namespace scope or
4415 // class scope declaration.
4416 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004417 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4418 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004419 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004420 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004421
Eli Friedman1503f772009-07-31 01:43:05 +00004422 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004423 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004424
4425 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4426 return false;
4427
Mike Stump1eb44332009-09-09 15:08:12 +00004428 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004429 diag::err_template_outside_namespace_or_class_scope)
4430 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004431}
Douglas Gregorcc636682009-02-17 23:15:12 +00004432
Douglas Gregord5cb8762009-10-07 00:13:32 +00004433/// \brief Determine what kind of template specialization the given declaration
4434/// is.
4435static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4436 if (!D)
4437 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004438
Douglas Gregorf6b11852009-10-08 15:14:33 +00004439 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4440 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004441 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4442 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004443 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4444 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004445
Douglas Gregord5cb8762009-10-07 00:13:32 +00004446 return TSK_Undeclared;
4447}
4448
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004449/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004450/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004451///
Douglas Gregor9302da62009-10-14 23:50:59 +00004452/// This routine determines whether a template specialization can be declared
4453/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004454///
4455/// \param S the semantic analysis object for which this check is being
4456/// performed.
4457///
4458/// \param Specialized the entity being specialized or instantiated, which
4459/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004460/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004461/// member class).
4462///
4463/// \param PrevDecl the previous declaration of this entity, if any.
4464///
4465/// \param Loc the location of the explicit specialization or instantiation of
4466/// this entity.
4467///
4468/// \param IsPartialSpecialization whether this is a partial specialization of
4469/// a class template.
4470///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004471/// \returns true if there was an error that we cannot recover from, false
4472/// otherwise.
4473static bool CheckTemplateSpecializationScope(Sema &S,
4474 NamedDecl *Specialized,
4475 NamedDecl *PrevDecl,
4476 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004477 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004478 // Keep these "kind" numbers in sync with the %select statements in the
4479 // various diagnostics emitted by this routine.
4480 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004481 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004482 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004483 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004484 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004485 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004486 EntityKind = 3;
4487 else if (isa<VarDecl>(Specialized))
4488 EntityKind = 4;
4489 else if (isa<RecordDecl>(Specialized))
4490 EntityKind = 5;
4491 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004492 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4493 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004494 return true;
4495 }
4496
Douglas Gregor88b70942009-02-25 22:02:03 +00004497 // C++ [temp.expl.spec]p2:
4498 // An explicit specialization shall be declared in the namespace
4499 // of which the template is a member, or, for member templates, in
4500 // the namespace of which the enclosing class or enclosing class
4501 // template is a member. An explicit specialization of a member
4502 // function, member class or static data member of a class
4503 // template shall be declared in the namespace of which the class
4504 // template is a member. Such a declaration may also be a
4505 // definition. If the declaration is not a definition, the
4506 // specialization may be defined later in the name- space in which
4507 // the explicit specialization was declared, or in a namespace
4508 // that encloses the one in which the explicit specialization was
4509 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004510 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004511 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004512 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004513 return true;
4514 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004515
Douglas Gregor0a407472009-10-07 17:30:37 +00004516 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004517 if (S.getLangOptions().Microsoft) {
4518 // Do not warn for class scope explicit specialization during
4519 // instantiation, warning was already emitted during pattern
4520 // semantic analysis.
4521 if (!S.ActiveTemplateInstantiations.size())
4522 S.Diag(Loc, diag::ext_function_specialization_in_class)
4523 << Specialized;
4524 } else {
4525 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4526 << Specialized;
4527 return true;
4528 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004529 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004530
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004531 // C++ [temp.class.spec]p6:
4532 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004533 // in any namespace scope in which its definition may be defined (14.5.1
4534 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004535 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004536 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004537 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004538 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004539 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004540 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4541 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004542 // C++ [temp.exp.spec]p2:
4543 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004544 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004545 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004546 // An explicit specialization of a member function, member class or
4547 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004548 // namespace of which the class template is a member.
4549 //
4550 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004551 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004552 // the specialized template.
4553 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4554 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004555 bool IsCPlusPlus0xExtension
4556 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004557 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004558 S.Diag(Loc, IsCPlusPlus0xExtension
4559 ? diag::ext_template_spec_decl_out_of_scope_global
4560 : diag::err_template_spec_decl_out_of_scope_global)
4561 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004562 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004563 S.Diag(Loc, IsCPlusPlus0xExtension
4564 ? diag::ext_template_spec_decl_out_of_scope
4565 : diag::err_template_spec_decl_out_of_scope)
4566 << EntityKind << Specialized
4567 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004568
Douglas Gregor9302da62009-10-14 23:50:59 +00004569 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4570 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004571 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004572 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004573
4574 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004575 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004576 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004577 // specializations of function templates, static data members, and member
4578 // functions, so we skip the check here for those kinds of entities.
4579 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004580 // Should we refactor that check, so that it occurs later?
4581 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004582 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4583 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004584 if (isa<TranslationUnitDecl>(SpecializedContext))
4585 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4586 << EntityKind << Specialized;
4587 else if (isa<NamespaceDecl>(SpecializedContext))
4588 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4589 << EntityKind << Specialized
4590 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004591
Douglas Gregor9302da62009-10-14 23:50:59 +00004592 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004593 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004594
Douglas Gregord5cb8762009-10-07 00:13:32 +00004595 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004596
Douglas Gregor88b70942009-02-25 22:02:03 +00004597 return false;
4598}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004599
Douglas Gregorbacb9492011-01-03 21:13:47 +00004600/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4601/// that checks non-type template partial specialization arguments.
4602static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4603 NonTypeTemplateParmDecl *Param,
4604 const TemplateArgument *Args,
4605 unsigned NumArgs) {
4606 for (unsigned I = 0; I != NumArgs; ++I) {
4607 if (Args[I].getKind() == TemplateArgument::Pack) {
4608 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004609 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004610 Args[I].pack_size()))
4611 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004612
Douglas Gregore94866f2009-06-12 21:21:02 +00004613 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004614 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004615
Douglas Gregorbacb9492011-01-03 21:13:47 +00004616 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004617 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004618 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004619 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004620
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004621 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004622 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4623 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004624
4625 // Strip off any implicit casts we added as part of type checking.
4626 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4627 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004628
Douglas Gregore94866f2009-06-12 21:21:02 +00004629 // C++ [temp.class.spec]p8:
4630 // A non-type argument is non-specialized if it is the name of a
4631 // non-type parameter. All other non-type arguments are
4632 // specialized.
4633 //
4634 // Below, we check the two conditions that only apply to
4635 // specialized non-type arguments, so skip any non-specialized
4636 // arguments.
4637 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004638 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004639 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004640
Douglas Gregore94866f2009-06-12 21:21:02 +00004641 // C++ [temp.class.spec]p9:
4642 // Within the argument list of a class template partial
4643 // specialization, the following restrictions apply:
4644 // -- A partially specialized non-type argument expression
4645 // shall not involve a template parameter of the partial
4646 // specialization except when the argument expression is a
4647 // simple identifier.
4648 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004649 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004650 diag::err_dependent_non_type_arg_in_partial_spec)
4651 << ArgExpr->getSourceRange();
4652 return true;
4653 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004654
Douglas Gregore94866f2009-06-12 21:21:02 +00004655 // -- The type of a template parameter corresponding to a
4656 // specialized non-type argument shall not be dependent on a
4657 // parameter of the specialization.
4658 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004659 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004660 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4661 << Param->getType()
4662 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004663 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004664 return true;
4665 }
4666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004667
Douglas Gregorbacb9492011-01-03 21:13:47 +00004668 return false;
4669}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004670
Douglas Gregorbacb9492011-01-03 21:13:47 +00004671/// \brief Check the non-type template arguments of a class template
4672/// partial specialization according to C++ [temp.class.spec]p9.
4673///
4674/// \param TemplateParams the template parameters of the primary class
4675/// template.
4676///
4677/// \param TemplateArg the template arguments of the class template
4678/// partial specialization.
4679///
4680/// \returns true if there was an error, false otherwise.
4681static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4682 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004683 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004684 const TemplateArgument *ArgList = TemplateArgs.data();
4685
4686 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4687 NonTypeTemplateParmDecl *Param
4688 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4689 if (!Param)
4690 continue;
4691
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004692 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004693 &ArgList[I], 1))
4694 return true;
4695 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004696
4697 return false;
4698}
4699
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004700/// \brief Retrieve the previous declaration of the given declaration.
4701static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4702 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4703 return VD->getPreviousDeclaration();
4704 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4705 return FD->getPreviousDeclaration();
4706 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4707 return TD->getPreviousDeclaration();
Richard Smith162e1c12011-04-15 14:24:37 +00004708 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004709 return TD->getPreviousDeclaration();
4710 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4711 return FTD->getPreviousDeclaration();
4712 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4713 return CTD->getPreviousDeclaration();
4714 return 0;
4715}
4716
John McCalld226f652010-08-21 09:40:31 +00004717DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004718Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4719 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004720 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004721 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004722 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004723 SourceLocation TemplateNameLoc,
4724 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004725 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004726 SourceLocation RAngleLoc,
4727 AttributeList *Attr,
4728 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004729 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004730
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004731 // NOTE: KWLoc is the location of the tag keyword. This will instead
4732 // store the location of the outermost template keyword in the declaration.
4733 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4734 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4735
Douglas Gregorcc636682009-02-17 23:15:12 +00004736 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004737 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004738 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004739 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4740
4741 if (!ClassTemplate) {
4742 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004743 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004744 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4745 return true;
4746 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004747
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004748 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004749 bool isPartialSpecialization = false;
4750
Douglas Gregor88b70942009-02-25 22:02:03 +00004751 // Check the validity of the template headers that introduce this
4752 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004753 // FIXME: We probably shouldn't complain about these headers for
4754 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004755 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004756 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004757 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4758 TemplateNameLoc,
4759 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004760 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004761 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004762 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004763 isExplicitSpecialization,
4764 Invalid);
4765 if (Invalid)
4766 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004767
Douglas Gregor05396e22009-08-25 17:23:04 +00004768 if (TemplateParams && TemplateParams->size() > 0) {
4769 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004770
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004771 if (TUK == TUK_Friend) {
4772 Diag(KWLoc, diag::err_partial_specialization_friend)
4773 << SourceRange(LAngleLoc, RAngleLoc);
4774 return true;
4775 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004776
Douglas Gregor05396e22009-08-25 17:23:04 +00004777 // C++ [temp.class.spec]p10:
4778 // The template parameter list of a specialization shall not
4779 // contain default template argument values.
4780 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4781 Decl *Param = TemplateParams->getParam(I);
4782 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4783 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004784 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004785 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004786 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004787 }
4788 } else if (NonTypeTemplateParmDecl *NTTP
4789 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4790 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004791 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004792 diag::err_default_arg_in_partial_spec)
4793 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004794 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004795 }
4796 } else {
4797 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004798 if (TTP->hasDefaultArgument()) {
4799 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004800 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004801 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004802 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004803 }
4804 }
4805 }
Douglas Gregora735b202009-10-13 14:39:41 +00004806 } else if (TemplateParams) {
4807 if (TUK == TUK_Friend)
4808 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004809 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004810 SourceRange(TemplateParams->getTemplateLoc(),
4811 TemplateParams->getRAngleLoc()))
4812 << SourceRange(LAngleLoc, RAngleLoc);
4813 else
4814 isExplicitSpecialization = true;
4815 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004816 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004817 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004818 isExplicitSpecialization = true;
4819 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004820
Douglas Gregorcc636682009-02-17 23:15:12 +00004821 // Check that the specialization uses the same tag kind as the
4822 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004823 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4824 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004825 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004826 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004827 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004828 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004829 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004830 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004831 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004832 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004833 diag::note_previous_use);
4834 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4835 }
4836
Douglas Gregor40808ce2009-03-09 23:48:35 +00004837 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004838 TemplateArgumentListInfo TemplateArgs;
4839 TemplateArgs.setLAngleLoc(LAngleLoc);
4840 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004841 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004842
Douglas Gregor925910d2011-01-03 20:35:03 +00004843 // Check for unexpanded parameter packs in any of the template arguments.
4844 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004845 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004846 UPPC_PartialSpecialization))
4847 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004848
Douglas Gregorcc636682009-02-17 23:15:12 +00004849 // Check that the template argument list is well-formed for this
4850 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004851 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004852 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4853 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004854 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004855
Douglas Gregor910f8002010-11-07 23:05:16 +00004856 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004857 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004858
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004859 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004860 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004861 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004862 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004863 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004864 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004865 return true;
4866
Douglas Gregor561f8122011-07-01 01:22:09 +00004867 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004868 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004869 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004870 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004871 TemplateArgs.size(),
4872 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004873 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4874 << ClassTemplate->getDeclName();
4875 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004876 }
4877 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004878
Douglas Gregorcc636682009-02-17 23:15:12 +00004879 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004880 ClassTemplateSpecializationDecl *PrevDecl = 0;
4881
4882 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004883 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004884 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004885 = ClassTemplate->findPartialSpecialization(Converted.data(),
4886 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004887 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004888 else
4889 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004890 = ClassTemplate->findSpecialization(Converted.data(),
4891 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004892
4893 ClassTemplateSpecializationDecl *Specialization = 0;
4894
Douglas Gregor88b70942009-02-25 22:02:03 +00004895 // Check whether we can declare a class template specialization in
4896 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004897 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004898 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4899 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004900 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004901 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004902
Douglas Gregorb88e8882009-07-30 17:40:51 +00004903 // The canonical type
4904 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004905 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004906 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004907 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004908 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004909 // arguments was referenced but not declared, or we're only
4910 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004911 // declaration node as our own, updating its source location and
4912 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004913 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004914 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004915 if (TemplateParameterLists.size() > 0) {
4916 Specialization->setTemplateParameterListsInfo(Context,
4917 TemplateParameterLists.size(),
4918 (TemplateParameterList**) TemplateParameterLists.release());
4919 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004920 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004921 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004922 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004923 // Build the canonical type that describes the converted template
4924 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004925 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4926 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004927 Converted.data(),
4928 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004929
4930 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004931 ClassTemplate->getInjectedClassNameSpecialization())) {
4932 // C++ [temp.class.spec]p9b3:
4933 //
4934 // -- The argument list of the specialization shall not be identical
4935 // to the implicit argument list of the primary template.
4936 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00004937 << (TUK == TUK_Definition)
4938 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00004939 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4940 ClassTemplate->getIdentifier(),
4941 TemplateNameLoc,
4942 Attr,
4943 TemplateParams,
Douglas Gregor8d267c52011-09-09 02:06:17 +00004944 AS_none, /*IsModulePrivate=*/false,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004945 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004946 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004947 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004948
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004949 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004950 ClassTemplatePartialSpecializationDecl *PrevPartial
4951 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004952 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004953 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004954 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004955 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004956 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004957 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004958 TemplateParams,
4959 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004960 Converted.data(),
4961 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004962 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004963 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004964 PrevPartial,
4965 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004966 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004967 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004968 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004969 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004970 (TemplateParameterList**) TemplateParameterLists.release());
4971 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004972
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004973 if (!PrevPartial)
4974 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004975 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004976
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004977 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004978 // template specialization, make a note of that.
4979 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4980 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004981
Douglas Gregor031a5882009-06-13 00:26:55 +00004982 // Check that all of the template parameters of the class template
4983 // partial specialization are deducible from the template
4984 // arguments. If not, this class template partial specialization
4985 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004986 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00004987 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004988 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004989 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004990 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004991 unsigned NumNonDeducible = 0;
4992 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4993 if (!DeducibleParams[I])
4994 ++NumNonDeducible;
4995
4996 if (NumNonDeducible) {
4997 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4998 << (NumNonDeducible > 1)
4999 << SourceRange(TemplateNameLoc, RAngleLoc);
5000 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5001 if (!DeducibleParams[I]) {
5002 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5003 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005004 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005005 diag::note_partial_spec_unused_parameter)
5006 << Param->getDeclName();
5007 else
Mike Stump1eb44332009-09-09 15:08:12 +00005008 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005009 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005010 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005011 }
5012 }
5013 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005014 } else {
5015 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005016 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005017 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005018 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005019 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005020 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005021 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005022 Converted.data(),
5023 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005024 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005025 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005026 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005027 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005028 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005029 (TemplateParameterList**) TemplateParameterLists.release());
5030 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005031
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005032 if (!PrevDecl)
5033 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005034
5035 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005036 }
5037
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005038 // C++ [temp.expl.spec]p6:
5039 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005040 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005041 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005042 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005043 // use occurs; no diagnostic is required.
5044 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005045 bool Okay = false;
5046 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5047 // Is there any previous explicit specialization declaration?
5048 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5049 Okay = true;
5050 break;
5051 }
5052 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005053
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005054 if (!Okay) {
5055 SourceRange Range(TemplateNameLoc, RAngleLoc);
5056 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5057 << Context.getTypeDeclType(Specialization) << Range;
5058
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005059 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005060 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005061 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005062 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005063 return true;
5064 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005065 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005066
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005067 // If this is not a friend, note that this is an explicit specialization.
5068 if (TUK != TUK_Friend)
5069 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005070
5071 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005072 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005073 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005074 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005075 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005076 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005077 Diag(Def->getLocation(), diag::note_previous_definition);
5078 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005079 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005080 }
5081 }
5082
John McCall7f1b9872010-12-18 03:30:47 +00005083 if (Attr)
5084 ProcessDeclAttributeList(S, Specialization, Attr);
5085
Douglas Gregorfc705b82009-02-26 22:19:44 +00005086 // Build the fully-sugared type for this class template
5087 // specialization as the user wrote in the specialization
5088 // itself. This means that we'll pretty-print the type retrieved
5089 // from the specialization's declaration the way that the user
5090 // actually wrote the specialization, rather than formatting the
5091 // name based on the "canonical" representation used to store the
5092 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005093 TypeSourceInfo *WrittenTy
5094 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5095 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005096 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005097 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005098 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005099 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005100 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005101
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005102 // C++ [temp.expl.spec]p9:
5103 // A template explicit specialization is in the scope of the
5104 // namespace in which the template was defined.
5105 //
5106 // We actually implement this paragraph where we set the semantic
5107 // context (in the creation of the ClassTemplateSpecializationDecl),
5108 // but we also maintain the lexical context where the actual
5109 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005110 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005111
Douglas Gregorcc636682009-02-17 23:15:12 +00005112 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005113 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005114 Specialization->startDefinition();
5115
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005116 if (TUK == TUK_Friend) {
5117 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5118 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005119 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005120 /*FIXME:*/KWLoc);
5121 Friend->setAccess(AS_public);
5122 CurContext->addDecl(Friend);
5123 } else {
5124 // Add the specialization into its lexical context, so that it can
5125 // be seen when iterating through the list of declarations in that
5126 // context. However, specializations are not found by name lookup.
5127 CurContext->addDecl(Specialization);
5128 }
John McCalld226f652010-08-21 09:40:31 +00005129 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005130}
Douglas Gregord57959a2009-03-27 23:10:48 +00005131
John McCalld226f652010-08-21 09:40:31 +00005132Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005133 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005134 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00005135 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
5136}
5137
John McCalld226f652010-08-21 09:40:31 +00005138Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005139 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005140 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005141 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005142 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005143
Douglas Gregor52591bf2009-06-24 00:54:41 +00005144 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005145 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005146 }
Mike Stump1eb44332009-09-09 15:08:12 +00005147
Douglas Gregor52591bf2009-06-24 00:54:41 +00005148 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005149
John McCalld226f652010-08-21 09:40:31 +00005150 Decl *DP = HandleDeclarator(ParentScope, D,
5151 move(TemplateParameterLists),
5152 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00005153 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005154 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005155 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005156 FunctionTemplate->getTemplatedDecl());
5157 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5158 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5159 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005160}
5161
John McCall75042392010-02-11 01:33:53 +00005162/// \brief Strips various properties off an implicit instantiation
5163/// that has just been explicitly specialized.
5164static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005165 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005166
5167 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5168 FD->setInlineSpecified(false);
5169 }
5170}
5171
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005172/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005173/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005174/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005175/// new specialization/instantiation will have any effect.
5176///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005177/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005178/// instantiation.
5179///
5180/// \param NewTSK the kind of the new explicit specialization or instantiation.
5181///
5182/// \param PrevDecl the previous declaration of the entity.
5183///
5184/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5185///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005186/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005187/// declaration was instantiated (either implicitly or explicitly).
5188///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005189/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005190/// specialization or instantiation has no effect and should be ignored.
5191///
5192/// \returns true if there was an error that should prevent the introduction of
5193/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005194bool
5195Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5196 TemplateSpecializationKind NewTSK,
5197 NamedDecl *PrevDecl,
5198 TemplateSpecializationKind PrevTSK,
5199 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005200 bool &HasNoEffect) {
5201 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005202
Douglas Gregor454885e2009-10-15 15:54:05 +00005203 switch (NewTSK) {
5204 case TSK_Undeclared:
5205 case TSK_ImplicitInstantiation:
5206 assert(false && "Don't check implicit instantiations here");
5207 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005208
Douglas Gregor454885e2009-10-15 15:54:05 +00005209 case TSK_ExplicitSpecialization:
5210 switch (PrevTSK) {
5211 case TSK_Undeclared:
5212 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005213 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005214 // explicitly specialized or has merely been mentioned without any
5215 // instantiation.
5216 return false;
5217
5218 case TSK_ImplicitInstantiation:
5219 if (PrevPointOfInstantiation.isInvalid()) {
5220 // The declaration itself has not actually been instantiated, so it is
5221 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005222 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005223 return false;
5224 }
5225 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005226
Douglas Gregor454885e2009-10-15 15:54:05 +00005227 case TSK_ExplicitInstantiationDeclaration:
5228 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005229 assert((PrevTSK == TSK_ImplicitInstantiation ||
5230 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005231 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005232
Douglas Gregor454885e2009-10-15 15:54:05 +00005233 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005234 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005235 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005236 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005237 // implicit instantiation to take place, in every translation unit in
5238 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005239 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
5240 // Is there any previous explicit specialization declaration?
5241 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5242 return false;
5243 }
5244
Douglas Gregor0d035142009-10-27 18:42:08 +00005245 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005246 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005247 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005248 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005249
Douglas Gregor454885e2009-10-15 15:54:05 +00005250 return true;
5251 }
5252 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005253
Douglas Gregor454885e2009-10-15 15:54:05 +00005254 case TSK_ExplicitInstantiationDeclaration:
5255 switch (PrevTSK) {
5256 case TSK_ExplicitInstantiationDeclaration:
5257 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005258 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005259 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005260
Douglas Gregor454885e2009-10-15 15:54:05 +00005261 case TSK_Undeclared:
5262 case TSK_ImplicitInstantiation:
5263 // We're explicitly instantiating something that may have already been
5264 // implicitly instantiated; that's fine.
5265 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005266
Douglas Gregor454885e2009-10-15 15:54:05 +00005267 case TSK_ExplicitSpecialization:
5268 // C++0x [temp.explicit]p4:
5269 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005270 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005271 // specialization for that template, the explicit instantiation has no
5272 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005273 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005274 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005275
Douglas Gregor454885e2009-10-15 15:54:05 +00005276 case TSK_ExplicitInstantiationDefinition:
5277 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005278 // If an entity is the subject of both an explicit instantiation
5279 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005280 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005281 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005282 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005283 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005284 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00005285 assert(PrevPointOfInstantiation.isValid() &&
5286 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005287 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005288 return false;
5289 }
5290 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005291
Douglas Gregor454885e2009-10-15 15:54:05 +00005292 case TSK_ExplicitInstantiationDefinition:
5293 switch (PrevTSK) {
5294 case TSK_Undeclared:
5295 case TSK_ImplicitInstantiation:
5296 // We're explicitly instantiating something that may have already been
5297 // implicitly instantiated; that's fine.
5298 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005299
Douglas Gregor454885e2009-10-15 15:54:05 +00005300 case TSK_ExplicitSpecialization:
5301 // C++ DR 259, C++0x [temp.explicit]p4:
5302 // For a given set of template parameters, if an explicit
5303 // instantiation of a template appears after a declaration of
5304 // an explicit specialization for that template, the explicit
5305 // instantiation has no effect.
5306 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005307 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005308 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005309 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00005310 if (!getLangOptions().CPlusPlus0x) {
5311 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00005312 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005313 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00005314 diag::note_previous_template_specialization);
5315 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005316 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005317 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005318
Douglas Gregor454885e2009-10-15 15:54:05 +00005319 case TSK_ExplicitInstantiationDeclaration:
5320 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005321 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00005322 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005323
Douglas Gregor454885e2009-10-15 15:54:05 +00005324 case TSK_ExplicitInstantiationDefinition:
5325 // C++0x [temp.spec]p5:
5326 // For a given template and a given set of template-arguments,
5327 // - an explicit instantiation definition shall appear at most once
5328 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005329 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005330 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005331 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00005332 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005333 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005334 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005335 }
5336 break;
5337 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005338
Douglas Gregor454885e2009-10-15 15:54:05 +00005339 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005340
Douglas Gregor454885e2009-10-15 15:54:05 +00005341 return false;
5342}
5343
John McCallaf2094e2010-04-08 09:05:18 +00005344/// \brief Perform semantic analysis for the given dependent function
5345/// template specialization. The only possible way to get a dependent
5346/// function template specialization is with a friend declaration,
5347/// like so:
5348///
5349/// template <class T> void foo(T);
5350/// template <class T> class A {
5351/// friend void foo<>(T);
5352/// };
5353///
5354/// There really isn't any useful analysis we can do here, so we
5355/// just store the information.
5356bool
5357Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5358 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5359 LookupResult &Previous) {
5360 // Remove anything from Previous that isn't a function template in
5361 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005362 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005363 LookupResult::Filter F = Previous.makeFilter();
5364 while (F.hasNext()) {
5365 NamedDecl *D = F.next()->getUnderlyingDecl();
5366 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005367 !FDLookupContext->InEnclosingNamespaceSetOf(
5368 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005369 F.erase();
5370 }
5371 F.done();
5372
5373 // Should this be diagnosed here?
5374 if (Previous.empty()) return true;
5375
5376 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5377 ExplicitTemplateArgs);
5378 return false;
5379}
5380
Abramo Bagnarae03db982010-05-20 15:32:11 +00005381/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005382/// specialization.
5383///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005384/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005385/// explicit function template specialization. On successful completion,
5386/// the function declaration \p FD will become a function template
5387/// specialization.
5388///
5389/// \param FD the function declaration, which will be updated to become a
5390/// function template specialization.
5391///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005392/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5393/// if any. Note that this may be valid info even when 0 arguments are
5394/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5395/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005396///
Francois Pichet59e7c562011-07-08 06:21:47 +00005397/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005398/// this function specialization.
5399bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005400Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005401 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005402 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005403 // The set of function template specializations that could match this
5404 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005405 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005406
Sebastian Redl7a126a42010-08-31 00:36:30 +00005407 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005408 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5409 I != E; ++I) {
5410 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5411 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005412 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005413 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005414 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5415 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005416 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005417
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005418 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005419 // A trailing template-argument can be left unspecified in the
5420 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005421 // provided it can be deduced from the function argument type.
5422 // Perform template argument deduction to determine whether we may be
5423 // specializing this template.
5424 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005425 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005426 FunctionDecl *Specialization = 0;
5427 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005428 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005429 FD->getType(),
5430 Specialization,
5431 Info)) {
5432 // FIXME: Template argument deduction failed; record why it failed, so
5433 // that we can provide nifty diagnostics.
5434 (void)TDK;
5435 continue;
5436 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005437
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005438 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005439 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005440 }
5441 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005442
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005443 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005444 UnresolvedSetIterator Result
5445 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005446 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005447 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005448 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005449 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005450 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005451 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005452 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005453 return true;
John McCallc373d482010-01-27 01:50:18 +00005454
5455 // Ignore access information; it doesn't figure into redeclaration checking.
5456 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005457
5458 FunctionTemplateSpecializationInfo *SpecInfo
5459 = Specialization->getTemplateSpecializationInfo();
5460 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005461
5462 // Note: do not overwrite location info if previous template
5463 // specialization kind was explicit.
5464 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5465 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5466 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005467
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005468 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005469 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005470
5471 // If this is a friend declaration, then we're not really declaring
5472 // an explicit specialization.
5473 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005474
Douglas Gregord5cb8762009-10-07 00:13:32 +00005475 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005476 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005477 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005478 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005479 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005480 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005481 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005482
5483 // C++ [temp.expl.spec]p6:
5484 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005485 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005486 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005487 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005488 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005489 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005490 if (!isFriend &&
5491 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005492 TSK_ExplicitSpecialization,
5493 Specialization,
5494 SpecInfo->getTemplateSpecializationKind(),
5495 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005496 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005497 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005498
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005499 // Mark the prior declaration as an explicit specialization, so that later
5500 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005501 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005502 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005503 MarkUnusedFileScopedDecl(Specialization);
5504 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005505
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005506 // Turn the given function declaration into a function template
5507 // specialization, with the template arguments from the previous
5508 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005509 // Take copies of (semantic and syntactic) template argument lists.
5510 const TemplateArgumentList* TemplArgs = new (Context)
5511 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5512 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5513 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005514 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005515 TemplArgs, /*InsertPos=*/0,
5516 SpecInfo->getTemplateSpecializationKind(),
5517 TemplArgsAsWritten);
Douglas Gregore885e182011-05-21 18:53:30 +00005518 FD->setStorageClass(Specialization->getStorageClass());
5519
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005520 // The "previous declaration" for this function template specialization is
5521 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005522 Previous.clear();
5523 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005524 return false;
5525}
5526
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005527/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005528/// specialization.
5529///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005530/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005531/// explicit member function specialization. On successful completion,
5532/// the function declaration \p FD will become a member function
5533/// specialization.
5534///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005535/// \param Member the member declaration, which will be updated to become a
5536/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005537///
John McCall68263142009-11-18 22:49:29 +00005538/// \param Previous the set of declarations, one of which may be specialized
5539/// by this function specialization; the set will be modified to contain the
5540/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005541bool
John McCall68263142009-11-18 22:49:29 +00005542Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005543 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005544
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005545 // Try to find the member we are instantiating.
5546 NamedDecl *Instantiation = 0;
5547 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005548 MemberSpecializationInfo *MSInfo = 0;
5549
John McCall68263142009-11-18 22:49:29 +00005550 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005551 // Nowhere to look anyway.
5552 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005553 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5554 I != E; ++I) {
5555 NamedDecl *D = (*I)->getUnderlyingDecl();
5556 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005557 if (Context.hasSameType(Function->getType(), Method->getType())) {
5558 Instantiation = Method;
5559 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005560 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005561 break;
5562 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005563 }
5564 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005565 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005566 VarDecl *PrevVar;
5567 if (Previous.isSingleResult() &&
5568 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005569 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005570 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005571 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005572 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005573 }
5574 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005575 CXXRecordDecl *PrevRecord;
5576 if (Previous.isSingleResult() &&
5577 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5578 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005579 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005580 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005581 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005582 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005583
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005584 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005585 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005586 // specializations are always out-of-line, the caller will complain about
5587 // this mismatch later.
5588 return false;
5589 }
John McCall77e8b112010-04-13 20:37:33 +00005590
5591 // If this is a friend, just bail out here before we start turning
5592 // things into explicit specializations.
5593 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5594 // Preserve instantiation information.
5595 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5596 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5597 cast<CXXMethodDecl>(InstantiatedFrom),
5598 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5599 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5600 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5601 cast<CXXRecordDecl>(InstantiatedFrom),
5602 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5603 }
5604
5605 Previous.clear();
5606 Previous.addDecl(Instantiation);
5607 return false;
5608 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005609
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005610 // Make sure that this is a specialization of a member.
5611 if (!InstantiatedFrom) {
5612 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5613 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005614 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5615 return true;
5616 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005617
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005618 // C++ [temp.expl.spec]p6:
5619 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005620 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005621 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005622 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005623 // use occurs; no diagnostic is required.
5624 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005625
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005626 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005627 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5628 TSK_ExplicitSpecialization,
5629 Instantiation,
5630 MSInfo->getTemplateSpecializationKind(),
5631 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005632 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005633 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005634
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005635 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005636 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005637 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005638 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005639 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005640 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005641
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005642 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005643 // the original declaration to note that it is an explicit specialization
5644 // (if it was previously an implicit instantiation). This latter step
5645 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005646 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005647 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5648 if (InstantiationFunction->getTemplateSpecializationKind() ==
5649 TSK_ImplicitInstantiation) {
5650 InstantiationFunction->setTemplateSpecializationKind(
5651 TSK_ExplicitSpecialization);
5652 InstantiationFunction->setLocation(Member->getLocation());
5653 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005654
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005655 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5656 cast<CXXMethodDecl>(InstantiatedFrom),
5657 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005658 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005659 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005660 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5661 if (InstantiationVar->getTemplateSpecializationKind() ==
5662 TSK_ImplicitInstantiation) {
5663 InstantiationVar->setTemplateSpecializationKind(
5664 TSK_ExplicitSpecialization);
5665 InstantiationVar->setLocation(Member->getLocation());
5666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005667
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005668 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5669 cast<VarDecl>(InstantiatedFrom),
5670 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005671 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005672 } else {
5673 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005674 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5675 if (InstantiationClass->getTemplateSpecializationKind() ==
5676 TSK_ImplicitInstantiation) {
5677 InstantiationClass->setTemplateSpecializationKind(
5678 TSK_ExplicitSpecialization);
5679 InstantiationClass->setLocation(Member->getLocation());
5680 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005681
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005682 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005683 cast<CXXRecordDecl>(InstantiatedFrom),
5684 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005686
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005687 // Save the caller the trouble of having to figure out which declaration
5688 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005689 Previous.clear();
5690 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005691 return false;
5692}
5693
Douglas Gregor558c0322009-10-14 23:41:34 +00005694/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005695///
5696/// \returns true if a serious error occurs, false otherwise.
5697static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005698 SourceLocation InstLoc,
5699 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005700 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5701 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005702
Douglas Gregor669eed82010-07-13 00:10:04 +00005703 if (CurContext->isRecord()) {
5704 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5705 << D;
5706 return true;
5707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005708
Douglas Gregor558c0322009-10-14 23:41:34 +00005709 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005710 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005711 // template.
5712 //
5713 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005714 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005715 !CurContext->Encloses(OrigContext)) {
5716 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005717 S.Diag(InstLoc,
5718 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005719 diag::err_explicit_instantiation_out_of_scope
5720 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005721 << D << NS;
5722 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005723 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005724 S.getLangOptions().CPlusPlus0x?
5725 diag::err_explicit_instantiation_must_be_global
5726 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005727 << D;
5728 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005729 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005730 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005731
Douglas Gregor558c0322009-10-14 23:41:34 +00005732 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005733 // If the name declared in the explicit instantiation is an unqualified
5734 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005735 // its template is declared or, if that namespace is inline (7.3.1), any
5736 // namespace from its enclosing namespace set.
5737 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005738 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005739
5740 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005741 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005742
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005743 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005744 S.getLangOptions().CPlusPlus0x?
5745 diag::err_explicit_instantiation_unqualified_wrong_namespace
5746 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005747 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005748 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005749 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005750}
5751
5752/// \brief Determine whether the given scope specifier has a template-id in it.
5753static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5754 if (!SS.isSet())
5755 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005756
Douglas Gregor558c0322009-10-14 23:41:34 +00005757 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005758 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005759 // or a static data member of a class template specialization, the name of
5760 // the class template specialization in the qualified-id for the member
5761 // name shall be a simple-template-id.
5762 //
5763 // C++98 has the same restriction, just worded differently.
5764 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5765 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005766 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005767 if (isa<TemplateSpecializationType>(T))
5768 return true;
5769
5770 return false;
5771}
5772
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005773// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005774DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005775Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005776 SourceLocation ExternLoc,
5777 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005778 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005779 SourceLocation KWLoc,
5780 const CXXScopeSpec &SS,
5781 TemplateTy TemplateD,
5782 SourceLocation TemplateNameLoc,
5783 SourceLocation LAngleLoc,
5784 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005785 SourceLocation RAngleLoc,
5786 AttributeList *Attr) {
5787 // Find the class template we're specializing
5788 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005789 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005790 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5791
5792 // Check that the specialization uses the same tag kind as the
5793 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005794 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5795 assert(Kind != TTK_Enum &&
5796 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005797 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005798 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005799 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005800 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005801 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005802 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005803 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005804 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005805 diag::note_previous_use);
5806 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5807 }
5808
Douglas Gregor558c0322009-10-14 23:41:34 +00005809 // C++0x [temp.explicit]p2:
5810 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005811 // definition and an explicit instantiation declaration. An explicit
5812 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005813 TemplateSpecializationKind TSK
5814 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5815 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005816
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005817 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005818 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005819 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005820
5821 // Check that the template argument list is well-formed for this
5822 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005823 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005824 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5825 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005826 return true;
5827
Douglas Gregor910f8002010-11-07 23:05:16 +00005828 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005829 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005830
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005831 // Find the class template specialization declaration that
5832 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005833 void *InsertPos = 0;
5834 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005835 = ClassTemplate->findSpecialization(Converted.data(),
5836 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005837
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005838 TemplateSpecializationKind PrevDecl_TSK
5839 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5840
Douglas Gregord5cb8762009-10-07 00:13:32 +00005841 // C++0x [temp.explicit]p2:
5842 // [...] An explicit instantiation shall appear in an enclosing
5843 // namespace of its template. [...]
5844 //
5845 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005846 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5847 SS.isSet()))
5848 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005849
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005850 ClassTemplateSpecializationDecl *Specialization = 0;
5851
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005852 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005853 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005854 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005855 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005856 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005857 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005858 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005859
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005860 // Even though HasNoEffect == true means that this explicit instantiation
5861 // has no effect on semantics, we go on to put its syntax in the AST.
5862
5863 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5864 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005865 // Since the only prior class template specialization with these
5866 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005867 // declaration node as our own, updating the source location
5868 // for the template name to reflect our new declaration.
5869 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005870 Specialization = PrevDecl;
5871 Specialization->setLocation(TemplateNameLoc);
5872 PrevDecl = 0;
5873 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005874 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005875
Douglas Gregor52604ab2009-09-11 21:19:12 +00005876 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005877 // Create a new class template specialization declaration node for
5878 // this explicit specialization.
5879 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005880 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005881 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005882 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005883 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005884 Converted.data(),
5885 Converted.size(),
5886 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005887 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005888
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005889 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005890 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005891 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005892 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005893 }
5894
5895 // Build the fully-sugared type for this explicit instantiation as
5896 // the user wrote in the explicit instantiation itself. This means
5897 // that we'll pretty-print the type retrieved from the
5898 // specialization's declaration the way that the user actually wrote
5899 // the explicit instantiation, rather than formatting the name based
5900 // on the "canonical" representation used to store the template
5901 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005902 TypeSourceInfo *WrittenTy
5903 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5904 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005905 Context.getTypeDeclType(Specialization));
5906 Specialization->setTypeAsWritten(WrittenTy);
5907 TemplateArgsIn.release();
5908
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005909 // Set source locations for keywords.
5910 Specialization->setExternLoc(ExternLoc);
5911 Specialization->setTemplateKeywordLoc(TemplateLoc);
5912
5913 // Add the explicit instantiation into its lexical context. However,
5914 // since explicit instantiations are never found by name lookup, we
5915 // just put it into the declaration context directly.
5916 Specialization->setLexicalDeclContext(CurContext);
5917 CurContext->addDecl(Specialization);
5918
5919 // Syntax is now OK, so return if it has no other effect on semantics.
5920 if (HasNoEffect) {
5921 // Set the template specialization kind.
5922 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005923 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005924 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005925
5926 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005927 // A definition of a class template or class member template
5928 // shall be in scope at the point of the explicit instantiation of
5929 // the class template or class member template.
5930 //
5931 // This check comes when we actually try to perform the
5932 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005933 ClassTemplateSpecializationDecl *Def
5934 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005935 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005936 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005937 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005938 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005939 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005940 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5941 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005942
Douglas Gregor0d035142009-10-27 18:42:08 +00005943 // Instantiate the members of this class template specialization.
5944 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005945 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005946 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005947 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5948
5949 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5950 // TSK_ExplicitInstantiationDefinition
5951 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5952 TSK == TSK_ExplicitInstantiationDefinition)
5953 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005954
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005955 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005956 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005957
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005958 // Set the template specialization kind.
5959 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005960 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005961}
5962
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005963// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005964DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005965Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005966 SourceLocation ExternLoc,
5967 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005968 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005969 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005970 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005971 IdentifierInfo *Name,
5972 SourceLocation NameLoc,
5973 AttributeList *Attr) {
5974
Douglas Gregor402abb52009-05-28 23:31:59 +00005975 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005976 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005977 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005978 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregor8d267c52011-09-09 02:06:17 +00005979 /*IsModulePrivate=*/false,
John McCalld226f652010-08-21 09:40:31 +00005980 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005981 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005982 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005983 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5984
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005985 if (!TagD)
5986 return true;
5987
John McCalld226f652010-08-21 09:40:31 +00005988 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005989 if (Tag->isEnum()) {
5990 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5991 << Context.getTypeDeclType(Tag);
5992 return true;
5993 }
5994
Douglas Gregord0c87372009-05-27 17:30:49 +00005995 if (Tag->isInvalidDecl())
5996 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005997
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005998 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5999 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6000 if (!Pattern) {
6001 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6002 << Context.getTypeDeclType(Record);
6003 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6004 return true;
6005 }
6006
Douglas Gregor558c0322009-10-14 23:41:34 +00006007 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006008 // If the explicit instantiation is for a class or member class, the
6009 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006010 // simple-template-id.
6011 //
6012 // C++98 has the same restriction, just worded differently.
6013 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006014 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006015 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006016
Douglas Gregor558c0322009-10-14 23:41:34 +00006017 // C++0x [temp.explicit]p2:
6018 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006019 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006020 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006021 TemplateSpecializationKind TSK
6022 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6023 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006024
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006025 // C++0x [temp.explicit]p2:
6026 // [...] An explicit instantiation shall appear in an enclosing
6027 // namespace of its template. [...]
6028 //
6029 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006030 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006031
Douglas Gregor454885e2009-10-15 15:54:05 +00006032 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006033 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00006034 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00006035 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006036 PrevDecl = Record;
6037 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006038 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006039 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006040 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006041 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006042 PrevDecl,
6043 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006044 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006045 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006046 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006047 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006048 return TagD;
6049 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006050
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006051 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006052 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006053 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006054 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006055 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006056 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006057 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006058 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006059 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006060 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6061 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006062 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6063 << Pattern;
6064 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006065 } else {
6066 if (InstantiateClass(NameLoc, Record, Def,
6067 getTemplateInstantiationArgs(Record),
6068 TSK))
6069 return true;
6070
Douglas Gregor952b0172010-02-11 01:04:33 +00006071 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006072 if (!RecordDef)
6073 return true;
6074 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006075 }
6076
Douglas Gregor0d035142009-10-27 18:42:08 +00006077 // Instantiate all of the members of the class.
6078 InstantiateClassMembers(NameLoc, RecordDef,
6079 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006080
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006081 if (TSK == TSK_ExplicitInstantiationDefinition)
6082 MarkVTableUsed(NameLoc, RecordDef, true);
6083
Mike Stump390b4cc2009-05-16 07:39:55 +00006084 // FIXME: We don't have any representation for explicit instantiations of
6085 // member classes. Such a representation is not needed for compilation, but it
6086 // should be available for clients that want to see all of the declarations in
6087 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006088 return TagD;
6089}
6090
John McCallf312b1e2010-08-26 23:41:50 +00006091DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6092 SourceLocation ExternLoc,
6093 SourceLocation TemplateLoc,
6094 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006095 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006096 // TODO: check if/when DNInfo should replace Name.
6097 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6098 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006099 if (!Name) {
6100 if (!D.isInvalidType())
6101 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6102 diag::err_explicit_instantiation_requires_name)
6103 << D.getDeclSpec().getSourceRange()
6104 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006105
Douglas Gregord5a423b2009-09-25 18:43:00 +00006106 return true;
6107 }
6108
6109 // The scope passed in may not be a decl scope. Zip up the scope tree until
6110 // we find one that is.
6111 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6112 (S->getFlags() & Scope::TemplateParamScope) != 0)
6113 S = S->getParent();
6114
6115 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006116 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6117 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006118 if (R.isNull())
6119 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006120
Douglas Gregore885e182011-05-21 18:53:30 +00006121 // C++ [dcl.stc]p1:
6122 // A storage-class-specifier shall not be specified in [...] an explicit
6123 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006124 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006125 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6126 << Name;
6127 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006128 } else if (D.getDeclSpec().getStorageClassSpec()
6129 != DeclSpec::SCS_unspecified) {
6130 // Complain about then remove the storage class specifier.
6131 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6132 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6133
6134 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006135 }
6136
Douglas Gregor663b5a02009-10-14 20:14:33 +00006137 // C++0x [temp.explicit]p1:
6138 // [...] An explicit instantiation of a function template shall not use the
6139 // inline or constexpr specifiers.
6140 // Presumably, this also applies to member functions of class templates as
6141 // well.
6142 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006143 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00006144 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00006145 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006146
Douglas Gregor663b5a02009-10-14 20:14:33 +00006147 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006148
Douglas Gregor558c0322009-10-14 23:41:34 +00006149 // C++0x [temp.explicit]p2:
6150 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006151 // definition and an explicit instantiation declaration. An explicit
6152 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006153 TemplateSpecializationKind TSK
6154 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6155 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006156
Abramo Bagnara25777432010-08-11 22:01:17 +00006157 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006158 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006159
6160 if (!R->isFunctionType()) {
6161 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006162 // A [...] static data member of a class template can be explicitly
6163 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006164 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006165 if (Previous.isAmbiguous())
6166 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006167
John McCall1bcee0a2009-12-02 08:25:40 +00006168 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006169 if (!Prev || !Prev->isStaticDataMember()) {
6170 // We expect to see a data data member here.
6171 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6172 << Name;
6173 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6174 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006175 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006176 return true;
6177 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006178
Douglas Gregord5a423b2009-09-25 18:43:00 +00006179 if (!Prev->getInstantiatedFromStaticDataMember()) {
6180 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006181 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006182 diag::err_explicit_instantiation_data_member_not_instantiated)
6183 << Prev;
6184 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6185 // FIXME: Can we provide a note showing where this was declared?
6186 return true;
6187 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006188
Douglas Gregor558c0322009-10-14 23:41:34 +00006189 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006190 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006191 // or a static data member of a class template specialization, the name of
6192 // the class template specialization in the qualified-id for the member
6193 // name shall be a simple-template-id.
6194 //
6195 // C++98 has the same restriction, just worded differently.
6196 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006197 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006198 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006199 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006200
Douglas Gregor558c0322009-10-14 23:41:34 +00006201 // Check the scope of this explicit instantiation.
6202 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006203
Douglas Gregor454885e2009-10-15 15:54:05 +00006204 // Verify that it is okay to explicitly instantiate here.
6205 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6206 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006207 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006208 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006209 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006210 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006211 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006212 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006213 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006214 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006215
Douglas Gregord5a423b2009-09-25 18:43:00 +00006216 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006217 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006218 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006219 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006220
Douglas Gregord5a423b2009-09-25 18:43:00 +00006221 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006222 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006223 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006224
6225 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006226 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006227 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006228 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006229 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6230 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006231 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6232 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006233 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6234 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006235 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006236 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006237 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006238 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006239 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006240
Douglas Gregord5a423b2009-09-25 18:43:00 +00006241 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006242 // A [...] function [...] can be explicitly instantiated from its template.
6243 // A member function [...] of a class template can be explicitly
6244 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006245 // template.
John McCallc373d482010-01-27 01:50:18 +00006246 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006247 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6248 P != PEnd; ++P) {
6249 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006250 if (!HasExplicitTemplateArgs) {
6251 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6252 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6253 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006254
John McCallc373d482010-01-27 01:50:18 +00006255 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006256 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6257 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006258 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006259 }
6260 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006261
Douglas Gregord5a423b2009-09-25 18:43:00 +00006262 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6263 if (!FunTmpl)
6264 continue;
6265
John McCall5769d612010-02-08 23:07:23 +00006266 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006267 FunctionDecl *Specialization = 0;
6268 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006269 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006270 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006271 R, Specialization, Info)) {
6272 // FIXME: Keep track of almost-matches?
6273 (void)TDK;
6274 continue;
6275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006276
John McCallc373d482010-01-27 01:50:18 +00006277 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006278 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006279
Douglas Gregord5a423b2009-09-25 18:43:00 +00006280 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006281 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006282 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006283 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006284 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6285 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6286 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006287
John McCallc373d482010-01-27 01:50:18 +00006288 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006289 return true;
John McCallc373d482010-01-27 01:50:18 +00006290
6291 // Ignore access control bits, we don't need them for redeclaration checking.
6292 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006293
Douglas Gregor0a897e32009-10-15 17:21:20 +00006294 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006295 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006296 diag::err_explicit_instantiation_member_function_not_instantiated)
6297 << Specialization
6298 << (Specialization->getTemplateSpecializationKind() ==
6299 TSK_ExplicitSpecialization);
6300 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6301 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006302 }
6303
Douglas Gregor0a897e32009-10-15 17:21:20 +00006304 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006305 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6306 PrevDecl = Specialization;
6307
Douglas Gregor0a897e32009-10-15 17:21:20 +00006308 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006309 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006310 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006311 PrevDecl,
6312 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006313 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006314 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006315 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006316
Douglas Gregor0a897e32009-10-15 17:21:20 +00006317 // FIXME: We may still want to build some representation of this
6318 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006319 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006320 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006321 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006322
6323 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006324
Douglas Gregor0a897e32009-10-15 17:21:20 +00006325 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006326 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006327
Douglas Gregor558c0322009-10-14 23:41:34 +00006328 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006329 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006330 // or a static data member of a class template specialization, the name of
6331 // the class template specialization in the qualified-id for the member
6332 // name shall be a simple-template-id.
6333 //
6334 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006335 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006336 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006337 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006338 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006339 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006340 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006341 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006342
Douglas Gregor558c0322009-10-14 23:41:34 +00006343 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006344 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006345 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006346 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006347 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006348
Douglas Gregord5a423b2009-09-25 18:43:00 +00006349 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006350 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006351}
6352
John McCallf312b1e2010-08-26 23:41:50 +00006353TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006354Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6355 const CXXScopeSpec &SS, IdentifierInfo *Name,
6356 SourceLocation TagLoc, SourceLocation NameLoc) {
6357 // This has to hold, because SS is expected to be defined.
6358 assert(Name && "Expected a name in a dependent tag");
6359
6360 NestedNameSpecifier *NNS
6361 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6362 if (!NNS)
6363 return true;
6364
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006365 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006366
Douglas Gregor48c89f42010-04-24 16:38:41 +00006367 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6368 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006369 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006370 return true;
6371 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006372
Douglas Gregor059101f2011-03-02 00:47:37 +00006373 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006374 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006375 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6376
6377 // Create type-source location information for this type.
6378 TypeLocBuilder TLB;
6379 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6380 TL.setKeywordLoc(TagLoc);
6381 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6382 TL.setNameLoc(NameLoc);
6383 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006384}
6385
John McCallf312b1e2010-08-26 23:41:50 +00006386TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006387Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6388 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006389 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006390 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006391 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006392
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006393 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6394 !getLangOptions().CPlusPlus0x)
6395 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006396 << FixItHint::CreateRemoval(TypenameLoc);
6397
Douglas Gregor2494dd02011-03-01 01:34:45 +00006398 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006399 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6400 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006401 if (T.isNull())
6402 return true;
John McCall63b43852010-04-29 23:50:39 +00006403
6404 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6405 if (isa<DependentNameType>(T)) {
6406 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006407 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006408 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006409 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006410 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006411 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006412 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006413 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006414 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006415 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006416
John McCallb3d87482010-08-24 05:47:05 +00006417 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006418}
6419
John McCallf312b1e2010-08-26 23:41:50 +00006420TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006421Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6422 const CXXScopeSpec &SS,
6423 SourceLocation TemplateLoc,
6424 TemplateTy TemplateIn,
6425 SourceLocation TemplateNameLoc,
6426 SourceLocation LAngleLoc,
6427 ASTTemplateArgsPtr TemplateArgsIn,
6428 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006429 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6430 !getLangOptions().CPlusPlus0x)
6431 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006432 << FixItHint::CreateRemoval(TypenameLoc);
6433
6434 // Translate the parser's template argument list in our AST format.
6435 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6436 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6437
6438 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006439 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6440 // Construct a dependent template specialization type.
6441 assert(DTN && "dependent template has non-dependent name?");
6442 assert(DTN->getQualifier()
6443 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6444 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6445 DTN->getQualifier(),
6446 DTN->getIdentifier(),
6447 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006448
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006449 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006450 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006451 DependentTemplateSpecializationTypeLoc SpecTL
6452 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006453 SpecTL.setLAngleLoc(LAngleLoc);
6454 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006455 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006456 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006457 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006458 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6459 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006460 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006461 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006462
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006463 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6464 if (T.isNull())
6465 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006466
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006467 // Provide source-location information for the template specialization
6468 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006469 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006470 TemplateSpecializationTypeLoc SpecTL
6471 = Builder.push<TemplateSpecializationTypeLoc>(T);
6472
6473 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006474 SpecTL.setLAngleLoc(LAngleLoc);
6475 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006476 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006477 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6478 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6479
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006480 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6481 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6482 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006483 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6484
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006485 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6486 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006487}
6488
Douglas Gregora02411e2011-02-27 22:46:49 +00006489
Douglas Gregord57959a2009-03-27 23:10:48 +00006490/// \brief Build the type that describes a C++ typename specifier,
6491/// e.g., "typename T::type".
6492QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006493Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6494 SourceLocation KeywordLoc,
6495 NestedNameSpecifierLoc QualifierLoc,
6496 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006497 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006498 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006499 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006500
John McCall77bb1aa2010-05-01 00:40:08 +00006501 DeclContext *Ctx = computeDeclContext(SS);
6502 if (!Ctx) {
6503 // If the nested-name-specifier is dependent and couldn't be
6504 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006505 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6506 return Context.getDependentNameType(Keyword,
6507 QualifierLoc.getNestedNameSpecifier(),
6508 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006509 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006510
John McCall77bb1aa2010-05-01 00:40:08 +00006511 // If the nested-name-specifier refers to the current instantiation,
6512 // the "typename" keyword itself is superfluous. In C++03, the
6513 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6514 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006515 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006516
John McCall77bb1aa2010-05-01 00:40:08 +00006517 if (RequireCompleteDeclContext(SS, Ctx))
6518 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006519
6520 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006521 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006522 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006523 unsigned DiagID = 0;
6524 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006525 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006526 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006527 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006528 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006529
6530 case LookupResult::FoundUnresolvedValue: {
6531 // We found a using declaration that is a value. Most likely, the using
6532 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006533 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006534 IILoc);
6535 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6536 << Name << Ctx << FullRange;
6537 if (UnresolvedUsingValueDecl *Using
6538 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006539 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006540 Diag(Loc, diag::note_using_value_decl_missing_typename)
6541 << FixItHint::CreateInsertion(Loc, "typename ");
6542 }
6543 }
6544 // Fall through to create a dependent typename type, from which we can recover
6545 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006546
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006547 case LookupResult::NotFoundInCurrentInstantiation:
6548 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006549 return Context.getDependentNameType(Keyword,
6550 QualifierLoc.getNestedNameSpecifier(),
6551 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006552
6553 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006554 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006555 // We found a type. Build an ElaboratedType, since the
6556 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006557 return Context.getElaboratedType(ETK_Typename,
6558 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006559 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006560 }
6561
6562 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006563 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006564 break;
6565
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006566
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006567 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006568 return QualType();
6569
Douglas Gregord57959a2009-03-27 23:10:48 +00006570 case LookupResult::FoundOverloaded:
6571 DiagID = diag::err_typename_nested_not_type;
6572 Referenced = *Result.begin();
6573 break;
6574
John McCall6e247262009-10-10 05:48:19 +00006575 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006576 return QualType();
6577 }
6578
6579 // If we get here, it's because name lookup did not find a
6580 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006581 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006582 IILoc);
6583 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006584 if (Referenced)
6585 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6586 << Name;
6587 return QualType();
6588}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006589
6590namespace {
6591 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006592 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006593 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006594 SourceLocation Loc;
6595 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006596
Douglas Gregor4a959d82009-08-06 16:20:37 +00006597 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006598 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006599
Mike Stump1eb44332009-09-09 15:08:12 +00006600 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006601 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006602 DeclarationName Entity)
6603 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006604 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006605
6606 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006607 /// transformed.
6608 ///
6609 /// For the purposes of type reconstruction, a type has already been
6610 /// transformed if it is NULL or if it is not dependent.
6611 bool AlreadyTransformed(QualType T) {
6612 return T.isNull() || !T->isDependentType();
6613 }
Mike Stump1eb44332009-09-09 15:08:12 +00006614
6615 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006616 /// rebuilt.
6617 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006618
Douglas Gregor4a959d82009-08-06 16:20:37 +00006619 /// \brief Returns the name of the entity whose type is being rebuilt.
6620 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006621
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006622 /// \brief Sets the "base" location and entity when that
6623 /// information is known based on another transformation.
6624 void setBase(SourceLocation Loc, DeclarationName Entity) {
6625 this->Loc = Loc;
6626 this->Entity = Entity;
6627 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006628 };
6629}
6630
Douglas Gregor4a959d82009-08-06 16:20:37 +00006631/// \brief Rebuilds a type within the context of the current instantiation.
6632///
Mike Stump1eb44332009-09-09 15:08:12 +00006633/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006634/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006635/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006636/// partial specialization thereof). This routine will rebuild that type now
6637/// that we have entered the declarator's scope, which may produce different
6638/// canonical types, e.g.,
6639///
6640/// \code
6641/// template<typename T>
6642/// struct X {
6643/// typedef T* pointer;
6644/// pointer data();
6645/// };
6646///
6647/// template<typename T>
6648/// typename X<T>::pointer X<T>::data() { ... }
6649/// \endcode
6650///
Douglas Gregor4714c122010-03-31 17:34:00 +00006651/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006652/// since we do not know that we can look into X<T> when we parsed the type.
6653/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006654/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006655/// as the canonical type of T*, allowing the return types of the out-of-line
6656/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006657TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6658 SourceLocation Loc,
6659 DeclarationName Name) {
6660 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006661 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006662
Douglas Gregor4a959d82009-08-06 16:20:37 +00006663 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6664 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006665}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006666
John McCall60d7b3a2010-08-24 06:29:42 +00006667ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006668 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6669 DeclarationName());
6670 return Rebuilder.TransformExpr(E);
6671}
6672
John McCall63b43852010-04-29 23:50:39 +00006673bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006674 if (SS.isInvalid())
6675 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006676
Douglas Gregor7e384942011-02-25 16:07:42 +00006677 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006678 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6679 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006680 NestedNameSpecifierLoc Rebuilt
6681 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6682 if (!Rebuilt)
6683 return true;
John McCall63b43852010-04-29 23:50:39 +00006684
Douglas Gregor7e384942011-02-25 16:07:42 +00006685 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006686 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006687}
6688
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006689/// \brief Produces a formatted string that describes the binding of
6690/// template parameters to template arguments.
6691std::string
6692Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6693 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006694 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006695}
6696
6697std::string
6698Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6699 const TemplateArgument *Args,
6700 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006701 llvm::SmallString<128> Str;
6702 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006703
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006704 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006705 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006706
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006707 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006708 if (I >= NumArgs)
6709 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006710
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006711 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006712 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006713 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006714 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006715
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006716 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006717 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006718 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006719 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006720 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006721
Douglas Gregor87dd6972010-12-20 16:52:59 +00006722 Out << " = ";
6723 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006724 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006725
6726 Out << ']';
6727 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006728}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006729
6730void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6731 if (!FD)
6732 return;
6733 FD->setLateTemplateParsed(Flag);
6734}
6735
6736bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6737 DeclContext *DC = CurContext;
6738
6739 while (DC) {
6740 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6741 const FunctionDecl *FD = RD->isLocalClass();
6742 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6743 } else if (DC->isTranslationUnit() || DC->isNamespace())
6744 return false;
6745
6746 DC = DC->getParent();
6747 }
6748 return false;
6749}