blob: cd5573b2f1af0668930a221e2748eb9974f5c3ad [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
John McCallf7a1a742009-11-24 19:00:30 +000079static void FilterAcceptableTemplateNames(ASTContext &C, 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();
John McCallad00b772010-06-16 08:42:20 +000085 NamedDecl *Repl = isAcceptableTemplateName(C, 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
95 // is followed by a template-argument-list, the reference refers to the
96 // class template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
98 //
99 // FIXME: Will we eventually have to do the same for alias templates?
100 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
101 if (!ClassTemplates.insert(ClassTmpl)) {
102 filter.erase();
103 continue;
104 }
John McCall8ba66912010-08-13 07:02:08 +0000105
106 // FIXME: we promote access to public here as a workaround to
107 // the fact that LookupResult doesn't let us remember that we
108 // found this template through a particular injected class name,
109 // which means we end up doing nasty things to the invariants.
110 // Pretending that access is public is *much* safer.
111 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000112 }
John McCallf7a1a742009-11-24 19:00:30 +0000113 }
114 filter.done();
115}
116
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000117TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000118 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000119 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000120 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000121 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000122 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000123 TemplateTy &TemplateResult,
124 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000125 assert(getLangOptions().CPlusPlus && "No template names in C!");
126
Douglas Gregor014e88d2009-11-03 23:16:33 +0000127 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000128 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000129
Douglas Gregor014e88d2009-11-03 23:16:33 +0000130 switch (Name.getKind()) {
131 case UnqualifiedId::IK_Identifier:
132 TName = DeclarationName(Name.Identifier);
133 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 case UnqualifiedId::IK_OperatorFunctionId:
136 TName = Context.DeclarationNames.getCXXOperatorName(
137 Name.OperatorFunctionId.Operator);
138 break;
139
Sean Hunte6252d12009-11-28 08:58:14 +0000140 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000141 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
142 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000143
Douglas Gregor014e88d2009-11-03 23:16:33 +0000144 default:
145 return TNK_Non_template;
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
John McCallb3d87482010-08-24 05:47:05 +0000148 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000150 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000151 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000152 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
153 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000154 if (R.empty()) return TNK_Non_template;
155 if (R.isAmbiguous()) {
156 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000157 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000158
159 // FIXME: we might have ambiguous templates, in which case we
160 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000161 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000162 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000163
John McCall0bd6feb2009-12-02 08:04:21 +0000164 TemplateName Template;
165 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
John McCall0bd6feb2009-12-02 08:04:21 +0000167 unsigned ResultCount = R.end() - R.begin();
168 if (ResultCount > 1) {
169 // We assume that we'll preserve the qualifier from a function
170 // template name in other ways.
171 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
172 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000173
174 // We'll do this lookup again later.
175 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000177 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
178
179 if (SS.isSet() && !SS.isInvalid()) {
180 NestedNameSpecifier *Qualifier
181 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000182 Template = Context.getQualifiedTemplateName(Qualifier,
183 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000184 } else {
185 Template = TemplateName(TD);
186 }
187
John McCallb8592062010-08-13 02:23:42 +0000188 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000189 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000190
191 // We'll do this lookup again later.
192 R.suppressDiagnostics();
193 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000194 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
195 TemplateKind = TNK_Type_template;
196 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
John McCall0bd6feb2009-12-02 08:04:21 +0000199 TemplateResult = TemplateTy::make(Template);
200 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000201}
202
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000203bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000204 SourceLocation IILoc,
205 Scope *S,
206 const CXXScopeSpec *SS,
207 TemplateTy &SuggestedTemplate,
208 TemplateNameKind &SuggestedKind) {
209 // We can't recover unless there's a dependent scope specifier preceding the
210 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000211 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000212 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
213 computeDeclContext(*SS))
214 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000215
Douglas Gregor84d0a192010-01-12 21:28:44 +0000216 // The code is missing a 'template' keyword prior to the dependent template
217 // name.
218 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
219 Diag(IILoc, diag::err_template_kw_missing)
220 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000221 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
224 SuggestedKind = TNK_Dependent_template_name;
225 return true;
226}
227
John McCallf7a1a742009-11-24 19:00:30 +0000228void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000229 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000230 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000231 bool EnteringContext,
232 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000233 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000234 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000235 DeclContext *LookupCtx = 0;
236 bool isDependent = false;
237 if (!ObjectType.isNull()) {
238 // This nested-name-specifier occurs in a member access expression, e.g.,
239 // x->B::f, and we are looking into the type of the object.
240 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
241 LookupCtx = computeDeclContext(ObjectType);
242 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000243 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000244 "Caller should have completed object type");
245 } else if (SS.isSet()) {
246 // This nested-name-specifier occurs after another nested-name-specifier,
247 // so long into the context associated with the prior nested-name-specifier.
248 LookupCtx = computeDeclContext(SS, EnteringContext);
249 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250
John McCallf7a1a742009-11-24 19:00:30 +0000251 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000252 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000253 return;
254 }
255
256 bool ObjectTypeSearchedInScope = false;
257 if (LookupCtx) {
258 // Perform "qualified" name lookup into the declaration context we
259 // computed, which is either the type of the base of a member access
260 // expression or the declaration context associated with a prior
261 // nested-name-specifier.
262 LookupQualifiedName(Found, LookupCtx);
263
264 if (!ObjectType.isNull() && Found.empty()) {
265 // C++ [basic.lookup.classref]p1:
266 // In a class member access expression (5.2.5), if the . or -> token is
267 // immediately followed by an identifier followed by a <, the
268 // identifier must be looked up to determine whether the < is the
269 // beginning of a template argument list (14.2) or a less-than operator.
270 // The identifier is first looked up in the class of the object
271 // expression. If the identifier is not found, it is then looked up in
272 // the context of the entire postfix-expression and shall name a class
273 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000274 if (S) LookupName(Found, S);
275 ObjectTypeSearchedInScope = true;
276 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000277 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000278 // We cannot look into a dependent object type or nested nme
279 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000280 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000281 return;
282 } else {
283 // Perform unqualified name lookup in the current scope.
284 LookupName(Found, S);
285 }
286
Douglas Gregor2e933882010-01-12 17:06:20 +0000287 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000288 // If we did not find any names, attempt to correct any typos.
289 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000290 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000291 false, CTC_CXXCasts)) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000292 FilterAcceptableTemplateNames(Context, Found);
John McCallad00b772010-06-16 08:42:20 +0000293 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000294 if (LookupCtx)
295 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
296 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000297 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000298 Found.getLookupName().getAsString());
299 else
300 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
301 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000302 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000303 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000304 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
305 Diag(Template->getLocation(), diag::note_previous_decl)
306 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000307 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000308 } else {
309 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000310 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000311 }
312 }
313
John McCallf7a1a742009-11-24 19:00:30 +0000314 FilterAcceptableTemplateNames(Context, Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000315 if (Found.empty()) {
316 if (isDependent)
317 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000318 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000319 }
John McCallf7a1a742009-11-24 19:00:30 +0000320
321 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
322 // C++ [basic.lookup.classref]p1:
323 // [...] If the lookup in the class of the object expression finds a
324 // template, the name is also looked up in the context of the entire
325 // postfix-expression and [...]
326 //
327 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
328 LookupOrdinaryName);
329 LookupName(FoundOuter, S);
330 FilterAcceptableTemplateNames(Context, FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000331
John McCallf7a1a742009-11-24 19:00:30 +0000332 if (FoundOuter.empty()) {
333 // - if the name is not found, the name found in the class of the
334 // object expression is used, otherwise
335 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
336 // - if the name is found in the context of the entire
337 // postfix-expression and does not name a class template, the name
338 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000339 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000340 // - if the name found is a class template, it must refer to the same
341 // entity as the one found in the class of the object expression,
342 // otherwise the program is ill-formed.
343 if (!Found.isSingleResult() ||
344 Found.getFoundDecl()->getCanonicalDecl()
345 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000346 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000347 diag::ext_nested_name_member_ref_lookup_ambiguous)
348 << Found.getLookupName()
349 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000350 Diag(Found.getRepresentativeDecl()->getLocation(),
351 diag::note_ambig_member_ref_object_type)
352 << ObjectType;
353 Diag(FoundOuter.getFoundDecl()->getLocation(),
354 diag::note_ambig_member_ref_scope);
355
356 // Recover by taking the template that we found in the object
357 // expression's type.
358 }
359 }
360 }
361}
362
John McCall2f841ba2009-12-02 03:53:29 +0000363/// ActOnDependentIdExpression - Handle a dependent id-expression that
364/// was just parsed. This is only possible with an explicit scope
365/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000366ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000367Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000369 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000371 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000372
John McCall2f841ba2009-12-02 03:53:29 +0000373 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000374 isa<CXXMethodDecl>(DC) &&
375 cast<CXXMethodDecl>(DC)->isInstance()) {
376 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000377
John McCallf7a1a742009-11-24 19:00:30 +0000378 // Since the 'this' expression is synthesized, we don't need to
379 // perform the double-lookup check.
380 NamedDecl *FirstQualifierInScope = 0;
381
John McCallaa81e162009-12-01 22:10:20 +0000382 return Owned(CXXDependentScopeMemberExpr::Create(Context,
383 /*This*/ 0, ThisType,
384 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000385 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000386 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000387 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000388 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000389 TemplateArgs));
390 }
391
Abramo Bagnara25777432010-08-11 22:01:17 +0000392 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000393}
394
John McCall60d7b3a2010-08-24 06:29:42 +0000395ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000396Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000397 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000398 const TemplateArgumentListInfo *TemplateArgs) {
399 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000400 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000401 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000402 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000403}
404
Douglas Gregor72c3f312008-12-05 18:15:24 +0000405/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
406/// that the template parameter 'PrevDecl' is being shadowed by a new
407/// declaration at location Loc. Returns true to indicate that this is
408/// an error, and false otherwise.
409bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000410 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000411
412 // Microsoft Visual C++ permits template parameters to be shadowed.
413 if (getLangOptions().Microsoft)
414 return false;
415
416 // C++ [temp.local]p4:
417 // A template-parameter shall not be redeclared within its
418 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000419 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000420 << cast<NamedDecl>(PrevDecl)->getDeclName();
421 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
422 return true;
423}
424
Douglas Gregor2943aed2009-03-03 04:44:36 +0000425/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000426/// the parameter D to reference the templated declaration and return a pointer
427/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000428TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
429 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
430 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000431 return Temp;
432 }
433 return 0;
434}
435
Douglas Gregorba68eca2011-01-05 17:40:24 +0000436ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
437 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000438 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000439 "Only template template arguments can be pack expansions here");
440 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
441 "Template template argument pack expansion without packs");
442 ParsedTemplateArgument Result(*this);
443 Result.EllipsisLoc = EllipsisLoc;
444 return Result;
445}
446
Douglas Gregor788cd062009-11-11 01:00:40 +0000447static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
448 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000449
Douglas Gregor788cd062009-11-11 01:00:40 +0000450 switch (Arg.getKind()) {
451 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000452 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000453 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000454 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000455 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000456 return TemplateArgumentLoc(TemplateArgument(T), DI);
457 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000458
Douglas Gregor788cd062009-11-11 01:00:40 +0000459 case ParsedTemplateArgument::NonType: {
460 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
461 return TemplateArgumentLoc(TemplateArgument(E), E);
462 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000465 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000466 TemplateArgument TArg;
467 if (Arg.getEllipsisLoc().isValid())
468 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
469 else
470 TArg = Template;
471 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000472 Arg.getScopeSpec().getWithLocInContext(
473 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000474 Arg.getLocation(),
475 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000476 }
477 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000478
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000479 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000480 return TemplateArgumentLoc();
481}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000482
Douglas Gregor788cd062009-11-11 01:00:40 +0000483/// \brief Translates template arguments as provided by the parser
484/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000485void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
486 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000487 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000488 TemplateArgs.addArgument(translateTemplateArgument(*this,
489 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000490}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000491
Douglas Gregor72c3f312008-12-05 18:15:24 +0000492/// ActOnTypeParameter - Called when a C++ template type parameter
493/// (e.g., "typename T") has been parsed. Typename specifies whether
494/// the keyword "typename" was used to declare the type parameter
495/// (otherwise, "class" was used), and KeyLoc is the location of the
496/// "class" or "typename" keyword. ParamName is the name of the
497/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000498/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000499/// If the type parameter has a default argument, it will be added
500/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000501Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
502 SourceLocation EllipsisLoc,
503 SourceLocation KeyLoc,
504 IdentifierInfo *ParamName,
505 SourceLocation ParamNameLoc,
506 unsigned Depth, unsigned Position,
507 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000508 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000509 assert(S->isTemplateParamScope() &&
510 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000511 bool Invalid = false;
512
513 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000514 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000515 LookupOrdinaryName,
516 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000517 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000518 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000519 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000520 }
521
Douglas Gregorddc29e12009-02-06 22:42:48 +0000522 SourceLocation Loc = ParamNameLoc;
523 if (!ParamName)
524 Loc = KeyLoc;
525
Douglas Gregor72c3f312008-12-05 18:15:24 +0000526 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000527 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000528 KeyLoc, Loc, Depth, Position, ParamName,
529 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000530 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000531 if (Invalid)
532 Param->setInvalidDecl();
533
534 if (ParamName) {
535 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000536 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000537 IdResolver.AddDecl(Param);
538 }
539
Douglas Gregor61c4d282011-01-05 15:48:55 +0000540 // C++0x [temp.param]p9:
541 // A default template-argument may be specified for any kind of
542 // template-parameter that is not a template parameter pack.
543 if (DefaultArg && Ellipsis) {
544 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
545 DefaultArg = ParsedType();
546 }
547
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000548 // Handle the default argument, if provided.
549 if (DefaultArg) {
550 TypeSourceInfo *DefaultTInfo;
551 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000552
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000553 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000554
Douglas Gregor6f526752010-12-16 08:48:57 +0000555 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000556 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000557 UPPC_DefaultArgument))
558 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000559
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000560 // Check the template argument itself.
561 if (CheckTemplateArgument(Param, DefaultTInfo)) {
562 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000563 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000564 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000565
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000566 Param->setDefaultArgument(DefaultTInfo, false);
567 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000568
John McCalld226f652010-08-21 09:40:31 +0000569 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000570}
571
Douglas Gregor2943aed2009-03-03 04:44:36 +0000572/// \brief Check that the type of a non-type template parameter is
573/// well-formed.
574///
575/// \returns the (possibly-promoted) parameter type if valid;
576/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000577QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000578Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000579 // We don't allow variably-modified types as the type of non-type template
580 // parameters.
581 if (T->isVariablyModifiedType()) {
582 Diag(Loc, diag::err_variably_modified_nontype_template_param)
583 << T;
584 return QualType();
585 }
586
Douglas Gregor2943aed2009-03-03 04:44:36 +0000587 // C++ [temp.param]p4:
588 //
589 // A non-type template-parameter shall have one of the following
590 // (optionally cv-qualified) types:
591 //
592 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000593 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000594 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000595 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000596 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000597 T->isReferenceType() ||
598 // -- pointer to member.
599 T->isMemberPointerType() ||
600 // If T is a dependent type, we can't do the check now, so we
601 // assume that it is well-formed.
602 T->isDependentType())
603 return T;
604 // C++ [temp.param]p8:
605 //
606 // A non-type template-parameter of type "array of T" or
607 // "function returning T" is adjusted to be of type "pointer to
608 // T" or "pointer to function returning T", respectively.
609 else if (T->isArrayType())
610 // FIXME: Keep the type prior to promotion?
611 return Context.getArrayDecayedType(T);
612 else if (T->isFunctionType())
613 // FIXME: Keep the type prior to promotion?
614 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000615
Douglas Gregor2943aed2009-03-03 04:44:36 +0000616 Diag(Loc, diag::err_template_nontype_parm_bad_type)
617 << T;
618
619 return QualType();
620}
621
John McCalld226f652010-08-21 09:40:31 +0000622Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
623 unsigned Depth,
624 unsigned Position,
625 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000626 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000627 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
628 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000629
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000630 assert(S->isTemplateParamScope() &&
631 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000632 bool Invalid = false;
633
634 IdentifierInfo *ParamName = D.getIdentifier();
635 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000636 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000637 LookupOrdinaryName,
638 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000639 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000640 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000641 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000642 }
643
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000644 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
645 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000646 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000647 Invalid = true;
648 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000649
Douglas Gregor10738d32010-12-23 23:51:58 +0000650 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000651 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000652 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000653 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000654 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000655 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000656 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000657 Param->setAccess(AS_public);
658
Douglas Gregor72c3f312008-12-05 18:15:24 +0000659 if (Invalid)
660 Param->setInvalidDecl();
661
662 if (D.getIdentifier()) {
663 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000664 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000665 IdResolver.AddDecl(Param);
666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000667
Douglas Gregor61c4d282011-01-05 15:48:55 +0000668 // C++0x [temp.param]p9:
669 // A default template-argument may be specified for any kind of
670 // template-parameter that is not a template parameter pack.
671 if (Default && IsParameterPack) {
672 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
673 Default = 0;
674 }
675
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000676 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000677 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000678 // Check for unexpanded parameter packs.
679 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
680 return Param;
681
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000682 TemplateArgument Converted;
683 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
684 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000685 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000686 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000687
John McCall9ae2f072010-08-23 23:25:46 +0000688 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000689 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000690
John McCalld226f652010-08-21 09:40:31 +0000691 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000692}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000693
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000694/// ActOnTemplateTemplateParameter - Called when a C++ template template
695/// parameter (e.g. T in template <template <typename> class T> class array)
696/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000697Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
698 SourceLocation TmpLoc,
699 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000700 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000701 IdentifierInfo *Name,
702 SourceLocation NameLoc,
703 unsigned Depth,
704 unsigned Position,
705 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000706 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000707 assert(S->isTemplateParamScope() &&
708 "Template template parameter not in template parameter scope!");
709
710 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000711 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000712 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000713 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000714 NameLoc.isInvalid()? TmpLoc : NameLoc,
715 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000716 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000717 Param->setAccess(AS_public);
718
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000719 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000720 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000721 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000722 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000723 IdResolver.AddDecl(Param);
724 }
725
Douglas Gregor6f526752010-12-16 08:48:57 +0000726 if (Params->size() == 0) {
727 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
728 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
729 Param->setInvalidDecl();
730 }
731
Douglas Gregor61c4d282011-01-05 15:48:55 +0000732 // C++0x [temp.param]p9:
733 // A default template-argument may be specified for any kind of
734 // template-parameter that is not a template parameter pack.
735 if (IsParameterPack && !Default.isInvalid()) {
736 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
737 Default = ParsedTemplateArgument();
738 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000739
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000740 if (!Default.isInvalid()) {
741 // Check only that we have a template template argument. We don't want to
742 // try to check well-formedness now, because our template template parameter
743 // might have dependent types in its template parameters, which we wouldn't
744 // be able to match now.
745 //
746 // If none of the template template parameter's template arguments mention
747 // other template parameters, we could actually perform more checking here.
748 // However, it isn't worth doing.
749 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
750 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
751 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
752 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000753 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000754 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000755
Douglas Gregor6f526752010-12-16 08:48:57 +0000756 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000758 DefaultArg.getArgument().getAsTemplate(),
759 UPPC_DefaultArgument))
760 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000761
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000762 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000763 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000764
John McCalld226f652010-08-21 09:40:31 +0000765 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000766}
767
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000768/// ActOnTemplateParameterList - Builds a TemplateParameterList that
769/// contains the template parameters in Params/NumParams.
770Sema::TemplateParamsTy *
771Sema::ActOnTemplateParameterList(unsigned Depth,
772 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000773 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000774 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000775 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000776 SourceLocation RAngleLoc) {
777 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000778 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000779
Douglas Gregorddc29e12009-02-06 22:42:48 +0000780 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000781 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000782 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000783}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000784
John McCallb6217662010-03-15 10:12:16 +0000785static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
786 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000787 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000788}
789
John McCallf312b1e2010-08-26 23:41:50 +0000790DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000791Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000792 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000793 IdentifierInfo *Name, SourceLocation NameLoc,
794 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000795 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000796 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000797 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000798 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000799 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000800 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000801
802 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000803 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000804 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000805
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000806 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
807 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000808
809 // There is no such thing as an unnamed class template.
810 if (!Name) {
811 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000812 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000813 }
814
815 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000816 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000817 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000818 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000819 if (SS.isNotEmpty() && !SS.isInvalid()) {
820 SemanticContext = computeDeclContext(SS, true);
821 if (!SemanticContext) {
822 // FIXME: Produce a reasonable diagnostic here
823 return true;
824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
John McCall77bb1aa2010-05-01 00:40:08 +0000826 if (RequireCompleteDeclContext(SS, SemanticContext))
827 return true;
828
John McCalla24dc2e2009-11-17 02:14:36 +0000829 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000830 } else {
831 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000832 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor57265e32010-04-12 16:00:01 +0000835 if (Previous.isAmbiguous())
836 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000837
Douglas Gregorddc29e12009-02-06 22:42:48 +0000838 NamedDecl *PrevDecl = 0;
839 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000840 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841
Douglas Gregorddc29e12009-02-06 22:42:48 +0000842 // If there is a previous declaration with the same name, check
843 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000844 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000845 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000846
847 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000848 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000849 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000850 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000851 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
852 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000853 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000854 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
855 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
856 PrevClassTemplate
857 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
858 ->getSpecializedTemplate();
859 }
860 }
861
John McCall65c49462009-12-18 11:25:59 +0000862 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000863 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000864 // [...] When looking for a prior declaration of a class or a function
865 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000866 // function is neither a qualified name nor a template-id, scopes outside
867 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000868 if (!SS.isSet()) {
869 DeclContext *OutermostContext = CurContext;
870 while (!OutermostContext->isFileContext())
871 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000872
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000873 if (PrevDecl &&
874 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
875 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
876 SemanticContext = PrevDecl->getDeclContext();
877 } else {
878 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000879 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000880 // declaration.
881 PrevDecl = PrevClassTemplate = 0;
882 SemanticContext = OutermostContext;
883 }
John McCalle129d442009-12-17 23:21:11 +0000884 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000885
John McCalle129d442009-12-17 23:21:11 +0000886 if (CurContext->isDependentContext()) {
887 // If this is a dependent context, we don't want to link the friend
888 // class template to the template in scope, because that would perform
889 // checking of the template parameter lists that can't be performed
890 // until the outer context is instantiated.
891 PrevDecl = PrevClassTemplate = 0;
892 }
893 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
894 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895
Douglas Gregorddc29e12009-02-06 22:42:48 +0000896 if (PrevClassTemplate) {
897 // Ensure that the template parameter lists are compatible.
898 if (!TemplateParameterListsAreEqual(TemplateParams,
899 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000900 /*Complain=*/true,
901 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000902 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000903
904 // C++ [temp.class]p4:
905 // In a redeclaration, partial specialization, explicit
906 // specialization or explicit instantiation of a class template,
907 // the class-key shall agree in kind with the original class
908 // template declaration (7.1.5.3).
909 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000910 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000911 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000912 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000913 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000915 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 }
917
Douglas Gregorddc29e12009-02-06 22:42:48 +0000918 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000919 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000920 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000921 Diag(NameLoc, diag::err_redefinition) << Name;
922 Diag(Def->getLocation(), diag::note_previous_definition);
923 // FIXME: Would it make sense to try to "forget" the previous
924 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000925 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000926 }
927 }
928 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
929 // Maybe we will complain about the shadowed template parameter.
930 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
931 // Just pretend that we didn't see the previous declaration.
932 PrevDecl = 0;
933 } else if (PrevDecl) {
934 // C++ [temp]p5:
935 // A class template shall not have the same name as any other
936 // template, class, function, object, enumeration, enumerator,
937 // namespace, or type in the same scope (3.3), except as specified
938 // in (14.5.4).
939 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
940 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000941 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000942 }
943
Douglas Gregord684b002009-02-10 19:49:53 +0000944 // Check the template parameter list of this declaration, possibly
945 // merging in the template parameter list from the previous class
946 // template declaration.
947 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000948 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000949 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000950 SemanticContext->isRecord() &&
951 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000952 ? TPC_ClassTemplateMember
953 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000954 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Douglas Gregor57265e32010-04-12 16:00:01 +0000956 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000957 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000958 // template out-of-line.
959 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
960 !(TUK == TUK_Friend && CurContext->isDependentContext()))
961 Diag(NameLoc, diag::err_member_def_does_not_match)
962 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000963 }
964
Mike Stump1eb44332009-09-09 15:08:12 +0000965 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000966 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000967 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000968 PrevClassTemplate->getTemplatedDecl() : 0,
969 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000970 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000971
972 ClassTemplateDecl *NewTemplate
973 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
974 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000975 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000976 NewClass->setDescribedClassTemplate(NewTemplate);
977
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000978 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000979 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000980 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000981 assert(T->isDependentType() && "Class template type is not dependent?");
982 (void)T;
983
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000984 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000985 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000986 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000987 PrevClassTemplate->getInstantiatedFromMemberTemplate())
988 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000989
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000990 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000991 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000992 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Douglas Gregorddc29e12009-02-06 22:42:48 +0000994 // Set the lexical context of these templates
995 NewClass->setLexicalDeclContext(CurContext);
996 NewTemplate->setLexicalDeclContext(CurContext);
997
John McCall0f434ec2009-07-31 02:45:11 +0000998 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000999 NewClass->startDefinition();
1000
1001 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001002 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001003
John McCall05b23ea2009-09-14 21:59:20 +00001004 if (TUK != TUK_Friend)
1005 PushOnScopeChains(NewTemplate, S);
1006 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001007 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001008 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001009 NewClass->setAccess(PrevClassTemplate->getAccess());
1010 }
John McCall05b23ea2009-09-14 21:59:20 +00001011
Douglas Gregord85bea22009-09-26 06:47:28 +00001012 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1013 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001014
John McCall05b23ea2009-09-14 21:59:20 +00001015 // Friend templates are visible in fairly strange ways.
1016 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001017 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001018 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1019 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1020 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001021 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001022 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001023
Douglas Gregord85bea22009-09-26 06:47:28 +00001024 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1025 NewClass->getLocation(),
1026 NewTemplate,
1027 /*FIXME:*/NewClass->getLocation());
1028 Friend->setAccess(AS_public);
1029 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001030 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001031
Douglas Gregord684b002009-02-10 19:49:53 +00001032 if (Invalid) {
1033 NewTemplate->setInvalidDecl();
1034 NewClass->setInvalidDecl();
1035 }
John McCalld226f652010-08-21 09:40:31 +00001036 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001037}
1038
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001039/// \brief Diagnose the presence of a default template argument on a
1040/// template parameter, which is ill-formed in certain contexts.
1041///
1042/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001043static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001044 Sema::TemplateParamListContext TPC,
1045 SourceLocation ParamLoc,
1046 SourceRange DefArgRange) {
1047 switch (TPC) {
1048 case Sema::TPC_ClassTemplate:
1049 return false;
1050
1051 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001052 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001053 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001054 // A default template-argument shall not be specified in a
1055 // function template declaration or a function template
1056 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001057 // If a friend function template declaration specifies a default
1058 // template-argument, that declaration shall be a definition and shall be
1059 // the only declaration of the function template in the translation unit.
1060 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001061 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001062 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001063 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001064 << DefArgRange;
1065 return false;
1066
1067 case Sema::TPC_ClassTemplateMember:
1068 // C++0x [temp.param]p9:
1069 // A default template-argument shall not be specified in the
1070 // template-parameter-lists of the definition of a member of a
1071 // class template that appears outside of the member's class.
1072 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1073 << DefArgRange;
1074 return true;
1075
1076 case Sema::TPC_FriendFunctionTemplate:
1077 // C++ [temp.param]p9:
1078 // A default template-argument shall not be specified in a
1079 // friend template declaration.
1080 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1081 << DefArgRange;
1082 return true;
1083
1084 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1085 // for friend function templates if there is only a single
1086 // declaration (and it is a definition). Strange!
1087 }
1088
1089 return false;
1090}
1091
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001092/// \brief Check for unexpanded parameter packs within the template parameters
1093/// of a template template parameter, recursively.
1094bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1095 TemplateParameterList *Params = TTP->getTemplateParameters();
1096 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1097 NamedDecl *P = Params->getParam(I);
1098 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001099 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001100 NTTP->getTypeSourceInfo(),
1101 Sema::UPPC_NonTypeTemplateParameterType))
1102 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001103
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001104 continue;
1105 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001106
1107 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001108 = dyn_cast<TemplateTemplateParmDecl>(P))
1109 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1110 return true;
1111 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001112
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001113 return false;
1114}
1115
Douglas Gregord684b002009-02-10 19:49:53 +00001116/// \brief Checks the validity of a template parameter list, possibly
1117/// considering the template parameter list from a previous
1118/// declaration.
1119///
1120/// If an "old" template parameter list is provided, it must be
1121/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1122/// template parameter list.
1123///
1124/// \param NewParams Template parameter list for a new template
1125/// declaration. This template parameter list will be updated with any
1126/// default arguments that are carried through from the previous
1127/// template parameter list.
1128///
1129/// \param OldParams If provided, template parameter list from a
1130/// previous declaration of the same template. Default template
1131/// arguments will be merged from the old template parameter list to
1132/// the new template parameter list.
1133///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001134/// \param TPC Describes the context in which we are checking the given
1135/// template parameter list.
1136///
Douglas Gregord684b002009-02-10 19:49:53 +00001137/// \returns true if an error occurred, false otherwise.
1138bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001139 TemplateParameterList *OldParams,
1140 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001141 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Douglas Gregord684b002009-02-10 19:49:53 +00001143 // C++ [temp.param]p10:
1144 // The set of default template-arguments available for use with a
1145 // template declaration or definition is obtained by merging the
1146 // default arguments from the definition (if in scope) and all
1147 // declarations in scope in the same way default function
1148 // arguments are (8.3.6).
1149 bool SawDefaultArgument = false;
1150 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001151
Anders Carlsson49d25572009-06-12 23:20:15 +00001152 bool SawParameterPack = false;
1153 SourceLocation ParameterPackLoc;
1154
Mike Stump1a35fde2009-02-11 23:03:27 +00001155 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001156 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001157 if (OldParams)
1158 OldParam = OldParams->begin();
1159
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001160 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001161 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1162 NewParamEnd = NewParams->end();
1163 NewParam != NewParamEnd; ++NewParam) {
1164 // Variables used to diagnose redundant default arguments
1165 bool RedundantDefaultArg = false;
1166 SourceLocation OldDefaultLoc;
1167 SourceLocation NewDefaultLoc;
1168
1169 // Variables used to diagnose missing default arguments
1170 bool MissingDefaultArg = false;
1171
Anders Carlsson49d25572009-06-12 23:20:15 +00001172 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001173 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001174 // parameter pack, it shall be the last template parameter.
1175 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001176 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001177 diag::err_template_param_pack_must_be_last_template_parameter);
1178 Invalid = true;
1179 }
1180
Douglas Gregord684b002009-02-10 19:49:53 +00001181 if (TemplateTypeParmDecl *NewTypeParm
1182 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001183 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001184 if (NewTypeParm->hasDefaultArgument() &&
1185 DiagnoseDefaultTemplateArgument(*this, TPC,
1186 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001187 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001188 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001189 NewTypeParm->removeDefaultArgument();
1190
1191 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001192 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001193 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Anders Carlsson49d25572009-06-12 23:20:15 +00001195 if (NewTypeParm->isParameterPack()) {
1196 assert(!NewTypeParm->hasDefaultArgument() &&
1197 "Parameter packs can't have a default argument!");
1198 SawParameterPack = true;
1199 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001200 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001201 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001202 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1203 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1204 SawDefaultArgument = true;
1205 RedundantDefaultArg = true;
1206 PreviousDefaultArgLoc = NewDefaultLoc;
1207 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1208 // Merge the default argument from the old declaration to the
1209 // new declaration.
1210 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001211 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001212 true);
1213 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1214 } else if (NewTypeParm->hasDefaultArgument()) {
1215 SawDefaultArgument = true;
1216 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1217 } else if (SawDefaultArgument)
1218 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001219 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001220 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001221 // Check for unexpanded parameter packs.
1222 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001223 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001224 UPPC_NonTypeTemplateParameterType)) {
1225 Invalid = true;
1226 continue;
1227 }
1228
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001229 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001230 if (NewNonTypeParm->hasDefaultArgument() &&
1231 DiagnoseDefaultTemplateArgument(*this, TPC,
1232 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001233 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001234 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001235 }
1236
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001237 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001238 NonTypeTemplateParmDecl *OldNonTypeParm
1239 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001240 if (NewNonTypeParm->isParameterPack()) {
1241 assert(!NewNonTypeParm->hasDefaultArgument() &&
1242 "Parameter packs can't have a default argument!");
1243 SawParameterPack = true;
1244 ParameterPackLoc = NewNonTypeParm->getLocation();
1245 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001246 NewNonTypeParm->hasDefaultArgument()) {
1247 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1248 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1249 SawDefaultArgument = true;
1250 RedundantDefaultArg = true;
1251 PreviousDefaultArgLoc = NewDefaultLoc;
1252 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1253 // Merge the default argument from the old declaration to the
1254 // new declaration.
1255 SawDefaultArgument = true;
1256 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001257 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001258 // parameter.
1259 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001260 OldNonTypeParm->getDefaultArgument(),
1261 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001262 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1263 } else if (NewNonTypeParm->hasDefaultArgument()) {
1264 SawDefaultArgument = true;
1265 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1266 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001267 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001268 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001269 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001270 TemplateTemplateParmDecl *NewTemplateParm
1271 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001272
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001273 // Check for unexpanded parameter packs, recursively.
1274 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1275 Invalid = true;
1276 continue;
1277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001278
1279 if (NewTemplateParm->hasDefaultArgument() &&
1280 DiagnoseDefaultTemplateArgument(*this, TPC,
1281 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001282 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001283 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001284
1285 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001286 TemplateTemplateParmDecl *OldTemplateParm
1287 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001288 if (NewTemplateParm->isParameterPack()) {
1289 assert(!NewTemplateParm->hasDefaultArgument() &&
1290 "Parameter packs can't have a default argument!");
1291 SawParameterPack = true;
1292 ParameterPackLoc = NewTemplateParm->getLocation();
1293 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001294 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001295 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1296 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001297 SawDefaultArgument = true;
1298 RedundantDefaultArg = true;
1299 PreviousDefaultArgLoc = NewDefaultLoc;
1300 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1301 // Merge the default argument from the old declaration to the
1302 // new declaration.
1303 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001304 // FIXME: We need to create a new kind of "default argument" expression
1305 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001306 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001307 OldTemplateParm->getDefaultArgument(),
1308 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001309 PreviousDefaultArgLoc
1310 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001311 } else if (NewTemplateParm->hasDefaultArgument()) {
1312 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001313 PreviousDefaultArgLoc
1314 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001315 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001316 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001317 }
1318
1319 if (RedundantDefaultArg) {
1320 // C++ [temp.param]p12:
1321 // A template-parameter shall not be given default arguments
1322 // by two different declarations in the same scope.
1323 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1324 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1325 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001326 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001327 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001328 // If a template-parameter of a class template has a default
1329 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001330 // have a default template-argument supplied or be a template parameter
1331 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001332 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001333 diag::err_template_param_default_arg_missing);
1334 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1335 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001336 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001337 }
1338
1339 // If we have an old template parameter list that we're merging
1340 // in, move on to the next parameter.
1341 if (OldParams)
1342 ++OldParam;
1343 }
1344
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001345 // We were missing some default arguments at the end of the list, so remove
1346 // all of the default arguments.
1347 if (RemoveDefaultArguments) {
1348 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1349 NewParamEnd = NewParams->end();
1350 NewParam != NewParamEnd; ++NewParam) {
1351 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1352 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001353 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001354 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1355 NTTP->removeDefaultArgument();
1356 else
1357 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1358 }
1359 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001360
Douglas Gregord684b002009-02-10 19:49:53 +00001361 return Invalid;
1362}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001363
John McCall4e2cbb22010-10-20 05:44:58 +00001364namespace {
1365
1366/// A class which looks for a use of a certain level of template
1367/// parameter.
1368struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1369 typedef RecursiveASTVisitor<DependencyChecker> super;
1370
1371 unsigned Depth;
1372 bool Match;
1373
1374 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1375 NamedDecl *ND = Params->getParam(0);
1376 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1377 Depth = PD->getDepth();
1378 } else if (NonTypeTemplateParmDecl *PD =
1379 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1380 Depth = PD->getDepth();
1381 } else {
1382 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1383 }
1384 }
1385
1386 bool Matches(unsigned ParmDepth) {
1387 if (ParmDepth >= Depth) {
1388 Match = true;
1389 return true;
1390 }
1391 return false;
1392 }
1393
1394 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1395 return !Matches(T->getDepth());
1396 }
1397
1398 bool TraverseTemplateName(TemplateName N) {
1399 if (TemplateTemplateParmDecl *PD =
1400 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1401 if (Matches(PD->getDepth())) return false;
1402 return super::TraverseTemplateName(N);
1403 }
1404
1405 bool VisitDeclRefExpr(DeclRefExpr *E) {
1406 if (NonTypeTemplateParmDecl *PD =
1407 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1408 if (PD->getDepth() == Depth) {
1409 Match = true;
1410 return false;
1411 }
1412 }
1413 return super::VisitDeclRefExpr(E);
1414 }
1415};
1416}
1417
1418/// Determines whether a template-id depends on the given parameter
1419/// list.
1420static bool
1421DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1422 TemplateParameterList *Params) {
1423 DependencyChecker Checker(Params);
1424 Checker.TraverseType(QualType(TemplateId, 0));
1425 return Checker.Match;
1426}
1427
Mike Stump1eb44332009-09-09 15:08:12 +00001428/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001429/// specifier, returning the template parameter list that applies to the
1430/// name.
1431///
1432/// \param DeclStartLoc the start of the declaration that has a scope
1433/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001434///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001435/// \param SS the scope specifier that will be matched to the given template
1436/// parameter lists. This scope specifier precedes a qualified name that is
1437/// being declared.
1438///
1439/// \param ParamLists the template parameter lists, from the outermost to the
1440/// innermost template parameter lists.
1441///
1442/// \param NumParamLists the number of template parameter lists in ParamLists.
1443///
John McCall77e8b112010-04-13 20:37:33 +00001444/// \param IsFriend Whether to apply the slightly different rules for
1445/// matching template parameters to scope specifiers in friend
1446/// declarations.
1447///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001448/// \param IsExplicitSpecialization will be set true if the entity being
1449/// declared is an explicit specialization, false otherwise.
1450///
Mike Stump1eb44332009-09-09 15:08:12 +00001451/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001452/// name that is preceded by the scope specifier @p SS. This template
1453/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001454/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001455/// template specialization), or may be NULL (if we were's declaring isn't
1456/// itself a template).
1457TemplateParameterList *
1458Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1459 const CXXScopeSpec &SS,
1460 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001461 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001462 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001463 bool &IsExplicitSpecialization,
1464 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001465 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001466
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001467 // Find the template-ids that occur within the nested-name-specifier. These
1468 // template-ids will match up with the template parameter lists.
1469 llvm::SmallVector<const TemplateSpecializationType *, 4>
1470 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001471 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1472 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001473 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1474 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001475 const Type *T = NNS->getAsType();
1476 if (!T) break;
1477
1478 // C++0x [temp.expl.spec]p17:
1479 // A member or a member template may be nested within many
1480 // enclosing class templates. In an explicit specialization for
1481 // such a member, the member declaration shall be preceded by a
1482 // template<> for each enclosing class template that is
1483 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001484 //
1485 // Following the existing practice of GNU and EDG, we allow a typedef of a
1486 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001487 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1488 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001489
Mike Stump1eb44332009-09-09 15:08:12 +00001490 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001491 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001492 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1493 if (!Template)
1494 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Ted Kremenek6217b802009-07-29 21:53:49 +00001496 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001497 ClassTemplateSpecializationDecl *SpecDecl
1498 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1499 // If the nested name specifier refers to an explicit specialization,
1500 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001501 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1502 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001504 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001505 }
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001507 TemplateIdsInSpecifier.push_back(SpecType);
1508 }
1509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001511 // Reverse the list of template-ids in the scope specifier, so that we can
1512 // more easily match up the template-ids and the template parameter lists.
1513 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001515 SourceLocation FirstTemplateLoc = DeclStartLoc;
1516 if (NumParamLists)
1517 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001519 // Match the template-ids found in the specifier to the template parameter
1520 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001521 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001522 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001523 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1524 const TemplateSpecializationType *TemplateId
1525 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001526 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001527
1528 // In friend declarations we can have template-ids which don't
1529 // depend on the corresponding template parameter lists. But
1530 // assume that empty parameter lists are supposed to match this
1531 // template-id.
1532 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1533 if (!DependentTemplateId ||
1534 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1535 continue;
1536 }
1537
1538 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001539 // We have a template-id without a corresponding template parameter
1540 // list.
John McCall77e8b112010-04-13 20:37:33 +00001541
1542 // ...which is fine if this is a friend declaration.
1543 if (IsFriend) {
1544 IsExplicitSpecialization = true;
1545 break;
1546 }
1547
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001548 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001549 // FIXME: the location information here isn't great.
1550 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001551 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001552 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001553 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001554 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001555 } else {
1556 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1557 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001558 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001559 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001560 }
1561 return 0;
1562 }
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001564 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001565 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001566 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001567
John McCall31f17ec2010-04-27 00:57:59 +00001568 // Are there cases in (e.g.) friends where this won't match?
1569 if (const InjectedClassNameType *Injected
1570 = TemplateId->getAs<InjectedClassNameType>()) {
1571 CXXRecordDecl *Record = Injected->getDecl();
1572 if (ClassTemplatePartialSpecializationDecl *Partial =
1573 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1574 ExpectedTemplateParams = Partial->getTemplateParameters();
1575 else
1576 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1577 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001578 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001579
John McCall31f17ec2010-04-27 00:57:59 +00001580 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001581 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001582 ExpectedTemplateParams,
1583 true, TPL_TemplateMatch);
1584
John McCall4e2cbb22010-10-20 05:44:58 +00001585 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1586 TPC_ClassTemplateMember);
1587 } else if (ParamLists[ParamIdx]->size() > 0)
1588 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001589 diag::err_template_param_list_matches_nontemplate)
1590 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001591 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001592 else
1593 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001594
1595 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001598 // If there were at least as many template-ids as there were template
1599 // parameter lists, then there are no template parameter lists remaining for
1600 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001601 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001602 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001604 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001605 if (ParamIdx != NumParamLists - 1) {
1606 while (ParamIdx < NumParamLists - 1) {
1607 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1608 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001609 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1610 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001611 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1612 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001613
1614 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1615 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1616 diag::note_explicit_template_spec_does_not_need_header)
1617 << ExplicitSpecializationsInSpecifier.back();
1618 ExplicitSpecializationsInSpecifier.pop_back();
1619 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001620
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001621 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001622 // means that the resulting template declaration can't be instantiated
1623 // properly (we'll end up with dependent nodes when we shouldn't).
1624 if (!isExplicitSpecHeader)
1625 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001626
John McCall4e2cbb22010-10-20 05:44:58 +00001627 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001628 }
1629 }
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001631 // Return the last template parameter list, which corresponds to the
1632 // entity being declared.
1633 return ParamLists[NumParamLists - 1];
1634}
1635
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001636void Sema::NoteAllFoundTemplates(TemplateName Name) {
1637 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1638 Diag(Template->getLocation(), diag::note_template_declared_here)
1639 << (isa<FunctionTemplateDecl>(Template)? 0
1640 : isa<ClassTemplateDecl>(Template)? 1
1641 : 2)
1642 << Template->getDeclName();
1643 return;
1644 }
1645
1646 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1647 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1648 IEnd = OST->end();
1649 I != IEnd; ++I)
1650 Diag((*I)->getLocation(), diag::note_template_declared_here)
1651 << 0 << (*I)->getDeclName();
1652
1653 return;
1654 }
1655}
1656
1657
Douglas Gregor7532dc62009-03-30 22:58:21 +00001658QualType Sema::CheckTemplateIdType(TemplateName Name,
1659 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001660 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001661 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001662 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1663 // We might have a substituted template template parameter pack. If so,
1664 // build a template specialization type for it.
1665 if (Name.getAsSubstTemplateTemplateParmPack())
1666 return Context.getTemplateSpecializationType(Name, TemplateArgs);
1667
1668 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1669 << Name;
1670 NoteAllFoundTemplates(Name);
1671 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001672 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001673
Douglas Gregor40808ce2009-03-09 23:48:35 +00001674 // Check that the template argument list is well-formed for this
1675 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001676 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001677 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001678 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001679 return QualType();
1680
Douglas Gregor910f8002010-11-07 23:05:16 +00001681 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001682 "Converted template argument list is too short!");
1683
1684 QualType CanonType;
1685
Douglas Gregorcaddba02009-11-12 18:38:13 +00001686 if (Name.isDependent() ||
1687 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001688 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001689 // This class template specialization is a dependent
1690 // type. Therefore, its canonical type is another class template
1691 // specialization type that contains all of the converted
1692 // arguments in canonical form. This ensures that, e.g., A<T> and
1693 // A<T, T> have identical types when A is declared as:
1694 //
1695 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001696 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001697 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001698 Converted.data(),
1699 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Douglas Gregor1275ae02009-07-28 23:00:59 +00001701 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001702 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001703 // In the future, we need to teach getTemplateSpecializationType to only
1704 // build the canonical type and return that to us.
1705 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001706
1707 // This might work out to be a current instantiation, in which
1708 // case the canonical type needs to be the InjectedClassNameType.
1709 //
1710 // TODO: in theory this could be a simple hashtable lookup; most
1711 // changes to CurContext don't change the set of current
1712 // instantiations.
1713 if (isa<ClassTemplateDecl>(Template)) {
1714 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1715 // If we get out to a namespace, we're done.
1716 if (Ctx->isFileContext()) break;
1717
1718 // If this isn't a record, keep looking.
1719 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1720 if (!Record) continue;
1721
1722 // Look for one of the two cases with InjectedClassNameTypes
1723 // and check whether it's the same template.
1724 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1725 !Record->getDescribedClassTemplate())
1726 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001727
John McCall31f17ec2010-04-27 00:57:59 +00001728 // Fetch the injected class name type and check whether its
1729 // injected type is equal to the type we just built.
1730 QualType ICNT = Context.getTypeDeclType(Record);
1731 QualType Injected = cast<InjectedClassNameType>(ICNT)
1732 ->getInjectedSpecializationType();
1733
1734 if (CanonType != Injected->getCanonicalTypeInternal())
1735 continue;
1736
1737 // If so, the canonical type of this TST is the injected
1738 // class name type of the record we just found.
1739 assert(ICNT.isCanonical());
1740 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001741 break;
1742 }
1743 }
Mike Stump1eb44332009-09-09 15:08:12 +00001744 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001745 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001746 // Find the class template specialization declaration that
1747 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001748 void *InsertPos = 0;
1749 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001750 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001751 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001752 if (!Decl) {
1753 // This is the first time we have referenced this class template
1754 // specialization. Create the canonical declaration and add it to
1755 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001756 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001757 ClassTemplate->getTemplatedDecl()->getTagKind(),
1758 ClassTemplate->getDeclContext(),
1759 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001760 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001761 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001762 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001763 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001764 Decl->setLexicalDeclContext(CurContext);
1765 }
1766
1767 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001768 assert(isa<RecordType>(CanonType) &&
1769 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001770 }
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Douglas Gregor40808ce2009-03-09 23:48:35 +00001772 // Build the fully-sugared type for this class template
1773 // specialization, which refers back to the class template
1774 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001775 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001776}
1777
John McCallf312b1e2010-08-26 23:41:50 +00001778TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001779Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1780 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001781 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001782 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001783 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001784 if (SS.isInvalid())
1785 return true;
1786
Douglas Gregor7532dc62009-03-30 22:58:21 +00001787 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001788
Douglas Gregor40808ce2009-03-09 23:48:35 +00001789 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001790 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001791 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001792
Douglas Gregora88f09f2011-02-28 17:23:35 +00001793 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1794 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1795 DTN->getQualifier(),
1796 DTN->getIdentifier(),
1797 TemplateArgs);
1798
1799 // Build type-source information.
1800 TypeLocBuilder TLB;
1801 DependentTemplateSpecializationTypeLoc SpecTL
1802 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001803 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001804 SpecTL.setNameLoc(TemplateLoc);
1805 SpecTL.setLAngleLoc(LAngleLoc);
1806 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001807 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001808 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1809 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1810 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1811 }
1812
John McCalld5532b62009-11-23 01:53:49 +00001813 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001814 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001815
1816 if (Result.isNull())
1817 return true;
1818
Douglas Gregor059101f2011-03-02 00:47:37 +00001819 // Build type-source information.
1820 TypeLocBuilder TLB;
1821 TemplateSpecializationTypeLoc SpecTL
1822 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1823 SpecTL.setTemplateNameLoc(TemplateLoc);
1824 SpecTL.setLAngleLoc(LAngleLoc);
1825 SpecTL.setRAngleLoc(RAngleLoc);
1826 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1827 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001828
Douglas Gregor059101f2011-03-02 00:47:37 +00001829 if (SS.isNotEmpty()) {
1830 // Create an elaborated-type-specifier containing the nested-name-specifier.
1831 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1832 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1833 ElabTL.setKeywordLoc(SourceLocation());
1834 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1835 }
1836
1837 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001838}
John McCallf1bbbb42009-09-04 01:14:41 +00001839
Douglas Gregor059101f2011-03-02 00:47:37 +00001840TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001841 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001842 SourceLocation TagLoc,
1843 CXXScopeSpec &SS,
1844 TemplateTy TemplateD,
1845 SourceLocation TemplateLoc,
1846 SourceLocation LAngleLoc,
1847 ASTTemplateArgsPtr TemplateArgsIn,
1848 SourceLocation RAngleLoc) {
1849 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1850
1851 // Translate the parser's template argument list in our AST format.
1852 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1853 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1854
1855 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001856 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001857 ElaboratedTypeKeyword Keyword
1858 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Douglas Gregor059101f2011-03-02 00:47:37 +00001860 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1861 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1862 DTN->getQualifier(),
1863 DTN->getIdentifier(),
1864 TemplateArgs);
1865
1866 // Build type-source information.
1867 TypeLocBuilder TLB;
1868 DependentTemplateSpecializationTypeLoc SpecTL
1869 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1870 SpecTL.setKeywordLoc(TagLoc);
1871 SpecTL.setNameLoc(TemplateLoc);
1872 SpecTL.setLAngleLoc(LAngleLoc);
1873 SpecTL.setRAngleLoc(RAngleLoc);
1874 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1875 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1876 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1877 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1878 }
1879
1880 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1881 if (Result.isNull())
1882 return TypeResult();
1883
1884 // Check the tag kind
1885 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001886 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001887
John McCall6b2becf2009-09-08 17:47:29 +00001888 IdentifierInfo *Id = D->getIdentifier();
1889 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001890
John McCall6b2becf2009-09-08 17:47:29 +00001891 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1892 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001893 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001894 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001895 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001896 }
1897 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001898
1899 // Provide source-location information for the template specialization.
1900 TypeLocBuilder TLB;
1901 TemplateSpecializationTypeLoc SpecTL
1902 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1903 SpecTL.setTemplateNameLoc(TemplateLoc);
1904 SpecTL.setLAngleLoc(LAngleLoc);
1905 SpecTL.setRAngleLoc(RAngleLoc);
1906 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1907 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001908
Douglas Gregor059101f2011-03-02 00:47:37 +00001909 // Construct an elaborated type containing the nested-name-specifier (if any)
1910 // and keyword.
1911 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1912 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1913 ElabTL.setKeywordLoc(TagLoc);
1914 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1915 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001916}
1917
John McCall60d7b3a2010-08-24 06:29:42 +00001918ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001919 LookupResult &R,
1920 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001921 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001922 // FIXME: Can we do any checking at this point? I guess we could check the
1923 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001924 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001925 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001926 // foo<int> could identify a single function unambiguously
1927 // This approach does NOT work, since f<int>(1);
1928 // gets resolved prior to resorting to overload resolution
1929 // i.e., template<class T> void f(double);
1930 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001931
1932 // These should be filtered out by our callers.
1933 assert(!R.empty() && "empty lookup results when building templateid");
1934 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1935
John McCallc373d482010-01-27 01:50:18 +00001936 // We don't want lookup warnings at this point.
1937 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001938
John McCallf7a1a742009-11-24 19:00:30 +00001939 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001940 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001941 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001942 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001943 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001944 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001945
1946 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001947}
1948
John McCallf7a1a742009-11-24 19:00:30 +00001949// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001950ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001951Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001952 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001953 const TemplateArgumentListInfo &TemplateArgs) {
1954 DeclContext *DC;
1955 if (!(DC = computeDeclContext(SS, false)) ||
1956 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001957 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001958 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001960 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001961 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001962 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1963 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001964
John McCallf7a1a742009-11-24 19:00:30 +00001965 if (R.isAmbiguous())
1966 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001967
John McCallf7a1a742009-11-24 19:00:30 +00001968 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001969 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1970 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001971 return ExprError();
1972 }
1973
1974 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001975 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1976 << (NestedNameSpecifier*) SS.getScopeRep()
1977 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001978 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1979 return ExprError();
1980 }
1981
1982 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001983}
1984
Douglas Gregorc45c2322009-03-31 00:43:58 +00001985/// \brief Form a dependent template name.
1986///
1987/// This action forms a dependent template name given the template
1988/// name and its (presumably dependent) scope specifier. For
1989/// example, given "MetaFun::template apply", the scope specifier \p
1990/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1991/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001992TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001993 SourceLocation TemplateKWLoc,
1994 CXXScopeSpec &SS,
1995 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001996 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001997 bool EnteringContext,
1998 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001999 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2000 !getLangOptions().CPlusPlus0x)
2001 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002002 << FixItHint::CreateRemoval(TemplateKWLoc);
2003
Douglas Gregor0707bc52010-01-19 16:01:07 +00002004 DeclContext *LookupCtx = 0;
2005 if (SS.isSet())
2006 LookupCtx = computeDeclContext(SS, EnteringContext);
2007 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002008 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002009 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002010 // C++0x [temp.names]p5:
2011 // If a name prefixed by the keyword template is not the name of
2012 // a template, the program is ill-formed. [Note: the keyword
2013 // template may not be applied to non-template members of class
2014 // templates. -end note ] [ Note: as is the case with the
2015 // typename prefix, the template prefix is allowed in cases
2016 // where it is not strictly necessary; i.e., when the
2017 // nested-name-specifier or the expression on the left of the ->
2018 // or . is not dependent on a template-parameter, or the use
2019 // does not appear in the scope of a template. -end note]
2020 //
2021 // Note: C++03 was more strict here, because it banned the use of
2022 // the "template" keyword prior to a template-name that was not a
2023 // dependent name. C++ DR468 relaxed this requirement (the
2024 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002025 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002026 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002027 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2028 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002029 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002030 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2031 isa<CXXRecordDecl>(LookupCtx) &&
2032 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002033 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002034 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002035 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002036 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002037 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002038 << Name.getSourceRange()
2039 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002040 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002041 } else {
2042 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002043 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002044 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002045 }
2046
Mike Stump1eb44332009-09-09 15:08:12 +00002047 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002048 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002049
Douglas Gregor014e88d2009-11-03 23:16:33 +00002050 switch (Name.getKind()) {
2051 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002052 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002053 Name.Identifier));
2054 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002055
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002056 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002057 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002058 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002059 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002060
2061 case UnqualifiedId::IK_LiteralOperatorId:
2062 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2063
Douglas Gregor014e88d2009-11-03 23:16:33 +00002064 default:
2065 break;
2066 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002067
2068 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002069 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002070 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002071 << Name.getSourceRange()
2072 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002073 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002074}
2075
Mike Stump1eb44332009-09-09 15:08:12 +00002076bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002077 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002078 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002079 const TemplateArgument &Arg = AL.getArgument();
2080
Anders Carlsson436b1562009-06-13 00:33:33 +00002081 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002082 switch(Arg.getKind()) {
2083 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002084 // C++ [temp.arg.type]p1:
2085 // A template-argument for a template-parameter which is a
2086 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002087 break;
2088 case TemplateArgument::Template: {
2089 // We have a template type parameter but the template argument
2090 // is a template without any arguments.
2091 SourceRange SR = AL.getSourceRange();
2092 TemplateName Name = Arg.getAsTemplate();
2093 Diag(SR.getBegin(), diag::err_template_missing_args)
2094 << Name << SR;
2095 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2096 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002097
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002098 return true;
2099 }
2100 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002101 // We have a template type parameter but the template argument
2102 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002103 SourceRange SR = AL.getSourceRange();
2104 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002105 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002106
Anders Carlsson436b1562009-06-13 00:33:33 +00002107 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002108 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002109 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002110
John McCalla93c9342009-12-07 02:54:59 +00002111 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002112 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002113
Anders Carlsson436b1562009-06-13 00:33:33 +00002114 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002115 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002116 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002117 return false;
2118}
2119
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002120/// \brief Substitute template arguments into the default template argument for
2121/// the given template type parameter.
2122///
2123/// \param SemaRef the semantic analysis object for which we are performing
2124/// the substitution.
2125///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002126/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002127/// for.
2128///
2129/// \param TemplateLoc the location of the template name that started the
2130/// template-id we are checking.
2131///
2132/// \param RAngleLoc the location of the right angle bracket ('>') that
2133/// terminates the template-id.
2134///
2135/// \param Param the template template parameter whose default we are
2136/// substituting into.
2137///
2138/// \param Converted the list of template arguments provided for template
2139/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002140/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002141static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002142SubstDefaultTemplateArgument(Sema &SemaRef,
2143 TemplateDecl *Template,
2144 SourceLocation TemplateLoc,
2145 SourceLocation RAngleLoc,
2146 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002147 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002148 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002149
2150 // If the argument type is dependent, instantiate it now based
2151 // on the previously-computed template arguments.
2152 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002153 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002154 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002155
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002156 MultiLevelTemplateArgumentList AllTemplateArgs
2157 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2158
2159 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002160 Template, Converted.data(),
2161 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002162 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002163
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002164 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2165 Param->getDefaultArgumentLoc(),
2166 Param->getDeclName());
2167 }
2168
2169 return ArgType;
2170}
2171
2172/// \brief Substitute template arguments into the default template argument for
2173/// the given non-type template parameter.
2174///
2175/// \param SemaRef the semantic analysis object for which we are performing
2176/// the substitution.
2177///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002178/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002179/// for.
2180///
2181/// \param TemplateLoc the location of the template name that started the
2182/// template-id we are checking.
2183///
2184/// \param RAngleLoc the location of the right angle bracket ('>') that
2185/// terminates the template-id.
2186///
Douglas Gregor788cd062009-11-11 01:00:40 +00002187/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002188/// substituting into.
2189///
2190/// \param Converted the list of template arguments provided for template
2191/// parameters that precede \p Param in the template parameter list.
2192///
2193/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002194static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002195SubstDefaultTemplateArgument(Sema &SemaRef,
2196 TemplateDecl *Template,
2197 SourceLocation TemplateLoc,
2198 SourceLocation RAngleLoc,
2199 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002200 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002201 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002202 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002203
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002204 MultiLevelTemplateArgumentList AllTemplateArgs
2205 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002206
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002207 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002208 Template, Converted.data(),
2209 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002210 SourceRange(TemplateLoc, RAngleLoc));
2211
2212 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2213}
2214
Douglas Gregor788cd062009-11-11 01:00:40 +00002215/// \brief Substitute template arguments into the default template argument for
2216/// the given template template parameter.
2217///
2218/// \param SemaRef the semantic analysis object for which we are performing
2219/// the substitution.
2220///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002221/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002222/// for.
2223///
2224/// \param TemplateLoc the location of the template name that started the
2225/// template-id we are checking.
2226///
2227/// \param RAngleLoc the location of the right angle bracket ('>') that
2228/// terminates the template-id.
2229///
2230/// \param Param the template template parameter whose default we are
2231/// substituting into.
2232///
2233/// \param Converted the list of template arguments provided for template
2234/// parameters that precede \p Param in the template parameter list.
2235///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002236/// \param QualifierLoc Will be set to the nested-name-specifier (with
2237/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002238///
Douglas Gregor788cd062009-11-11 01:00:40 +00002239/// \returns the substituted template argument, or NULL if an error occurred.
2240static TemplateName
2241SubstDefaultTemplateArgument(Sema &SemaRef,
2242 TemplateDecl *Template,
2243 SourceLocation TemplateLoc,
2244 SourceLocation RAngleLoc,
2245 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002246 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2247 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002248 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002249 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002250
Douglas Gregor788cd062009-11-11 01:00:40 +00002251 MultiLevelTemplateArgumentList AllTemplateArgs
2252 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002253
Douglas Gregor788cd062009-11-11 01:00:40 +00002254 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002255 Template, Converted.data(),
2256 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002257 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002258
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002259 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002260 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002261 if (QualifierLoc) {
2262 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2263 AllTemplateArgs);
2264 if (!QualifierLoc)
2265 return TemplateName();
2266 }
2267
Douglas Gregor1d752d72011-03-02 18:46:51 +00002268 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002269 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002270 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002271 AllTemplateArgs);
2272}
2273
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002274/// \brief If the given template parameter has a default template
2275/// argument, substitute into that default template argument and
2276/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002277TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002278Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2279 SourceLocation TemplateLoc,
2280 SourceLocation RAngleLoc,
2281 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002282 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2283 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002284 if (!TypeParm->hasDefaultArgument())
2285 return TemplateArgumentLoc();
2286
John McCalla93c9342009-12-07 02:54:59 +00002287 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002288 TemplateLoc,
2289 RAngleLoc,
2290 TypeParm,
2291 Converted);
2292 if (DI)
2293 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2294
2295 return TemplateArgumentLoc();
2296 }
2297
2298 if (NonTypeTemplateParmDecl *NonTypeParm
2299 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2300 if (!NonTypeParm->hasDefaultArgument())
2301 return TemplateArgumentLoc();
2302
John McCall60d7b3a2010-08-24 06:29:42 +00002303 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002304 TemplateLoc,
2305 RAngleLoc,
2306 NonTypeParm,
2307 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002308 if (Arg.isInvalid())
2309 return TemplateArgumentLoc();
2310
2311 Expr *ArgE = Arg.takeAs<Expr>();
2312 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2313 }
2314
2315 TemplateTemplateParmDecl *TempTempParm
2316 = cast<TemplateTemplateParmDecl>(Param);
2317 if (!TempTempParm->hasDefaultArgument())
2318 return TemplateArgumentLoc();
2319
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002320
Douglas Gregor1d752d72011-03-02 18:46:51 +00002321 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002322 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002323 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002324 RAngleLoc,
2325 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002326 Converted,
2327 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002328 if (TName.isNull())
2329 return TemplateArgumentLoc();
2330
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002331 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002332 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002333 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2334}
2335
Douglas Gregore7526412009-11-11 19:31:23 +00002336/// \brief Check that the given template argument corresponds to the given
2337/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002338///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002339/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002340/// checked.
2341///
2342/// \param Arg The template argument.
2343///
2344/// \param Template The template in which the template argument resides.
2345///
2346/// \param TemplateLoc The location of the template name for the template
2347/// whose argument list we're matching.
2348///
2349/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2350/// the template argument list.
2351///
2352/// \param ArgumentPackIndex The index into the argument pack where this
2353/// argument will be placed. Only valid if the parameter is a parameter pack.
2354///
2355/// \param Converted The checked, converted argument will be added to the
2356/// end of this small vector.
2357///
2358/// \param CTAK Describes how we arrived at this particular template argument:
2359/// explicitly written, deduced, etc.
2360///
2361/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002362bool Sema::CheckTemplateArgument(NamedDecl *Param,
2363 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002364 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002365 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002366 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002367 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002368 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002369 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002370 // Check template type parameters.
2371 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002372 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002373
Douglas Gregord9e15302009-11-11 19:41:09 +00002374 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002375 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002376 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002377 // with the template arguments we've seen thus far. But if the
2378 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002379 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002380 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2381 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002382
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002383 if (NTTPType->isDependentType() &&
2384 !isa<TemplateTemplateParmDecl>(Template) &&
2385 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002386 // Do substitution on the type of the non-type template parameter.
2387 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002388 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002389 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002390
2391 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002392 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002393 NTTPType = SubstType(NTTPType,
2394 MultiLevelTemplateArgumentList(TemplateArgs),
2395 NTTP->getLocation(),
2396 NTTP->getDeclName());
2397 // If that worked, check the non-type template parameter type
2398 // for validity.
2399 if (!NTTPType.isNull())
2400 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2401 NTTP->getLocation());
2402 if (NTTPType.isNull())
2403 return true;
2404 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002405
Douglas Gregore7526412009-11-11 19:31:23 +00002406 switch (Arg.getArgument().getKind()) {
2407 case TemplateArgument::Null:
2408 assert(false && "Should never see a NULL template argument here");
2409 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002410
Douglas Gregore7526412009-11-11 19:31:23 +00002411 case TemplateArgument::Expression: {
2412 Expr *E = Arg.getArgument().getAsExpr();
2413 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002414 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002415 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002416
Douglas Gregor910f8002010-11-07 23:05:16 +00002417 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002418 break;
2419 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002420
Douglas Gregore7526412009-11-11 19:31:23 +00002421 case TemplateArgument::Declaration:
2422 case TemplateArgument::Integral:
2423 // We've already checked this template argument, so just copy
2424 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002425 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002426 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002427
Douglas Gregore7526412009-11-11 19:31:23 +00002428 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002429 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002430 // We were given a template template argument. It may not be ill-formed;
2431 // see below.
2432 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002433 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2434 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002435 // We have a template argument such as \c T::template X, which we
2436 // parsed as a template template argument. However, since we now
2437 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002438 // template name into an expression.
2439
2440 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2441 Arg.getTemplateNameLoc());
2442
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002443 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002444 SS.Adopt(Arg.getTemplateQualifierLoc());
John McCallf7a1a742009-11-24 19:00:30 +00002445 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002446 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002447 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002448
Douglas Gregora7fc9012011-01-05 18:58:31 +00002449 // If we parsed the template argument as a pack expansion, create a
2450 // pack expansion expression.
2451 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002452 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002453 Arg.getTemplateEllipsisLoc());
2454 if (Expansion.isInvalid())
2455 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002456
Douglas Gregora7fc9012011-01-05 18:58:31 +00002457 E = Expansion.get();
2458 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002459
Douglas Gregore7526412009-11-11 19:31:23 +00002460 TemplateArgument Result;
2461 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2462 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002463
Douglas Gregor910f8002010-11-07 23:05:16 +00002464 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002465 break;
2466 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002467
Douglas Gregore7526412009-11-11 19:31:23 +00002468 // We have a template argument that actually does refer to a class
2469 // template, template alias, or template template parameter, and
2470 // therefore cannot be a non-type template argument.
2471 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2472 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002473
Douglas Gregore7526412009-11-11 19:31:23 +00002474 Diag(Param->getLocation(), diag::note_template_param_here);
2475 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002476
Douglas Gregore7526412009-11-11 19:31:23 +00002477 case TemplateArgument::Type: {
2478 // We have a non-type template parameter but the template
2479 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002480
Douglas Gregore7526412009-11-11 19:31:23 +00002481 // C++ [temp.arg]p2:
2482 // In a template-argument, an ambiguity between a type-id and
2483 // an expression is resolved to a type-id, regardless of the
2484 // form of the corresponding template-parameter.
2485 //
2486 // We warn specifically about this case, since it can be rather
2487 // confusing for users.
2488 QualType T = Arg.getArgument().getAsType();
2489 SourceRange SR = Arg.getSourceRange();
2490 if (T->isFunctionType())
2491 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2492 else
2493 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2494 Diag(Param->getLocation(), diag::note_template_param_here);
2495 return true;
2496 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002497
Douglas Gregore7526412009-11-11 19:31:23 +00002498 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002499 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002500 break;
2501 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002502
Douglas Gregore7526412009-11-11 19:31:23 +00002503 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002504 }
2505
2506
Douglas Gregore7526412009-11-11 19:31:23 +00002507 // Check template template parameters.
2508 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002509
Douglas Gregore7526412009-11-11 19:31:23 +00002510 // Substitute into the template parameter list of the template
2511 // template parameter, since previously-supplied template arguments
2512 // may appear within the template template parameter.
2513 {
2514 // Set up a template instantiation context.
2515 LocalInstantiationScope Scope(*this);
2516 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002517 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002518 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002519
2520 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002521 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002522 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002523 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002524 MultiLevelTemplateArgumentList(TemplateArgs)));
2525 if (!TempParm)
2526 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002527 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002528
Douglas Gregore7526412009-11-11 19:31:23 +00002529 switch (Arg.getArgument().getKind()) {
2530 case TemplateArgument::Null:
2531 assert(false && "Should never see a NULL template argument here");
2532 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002533
Douglas Gregore7526412009-11-11 19:31:23 +00002534 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002535 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002536 if (CheckTemplateArgument(TempParm, Arg))
2537 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002538
Douglas Gregor910f8002010-11-07 23:05:16 +00002539 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002540 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002541
Douglas Gregore7526412009-11-11 19:31:23 +00002542 case TemplateArgument::Expression:
2543 case TemplateArgument::Type:
2544 // We have a template template parameter but the template
2545 // argument does not refer to a template.
2546 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2547 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002548
Douglas Gregore7526412009-11-11 19:31:23 +00002549 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002550 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002551 "Declaration argument with template template parameter");
2552 break;
2553 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002554 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002555 "Integral argument with template template parameter");
2556 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002557
Douglas Gregore7526412009-11-11 19:31:23 +00002558 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002559 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002560 break;
2561 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002562
Douglas Gregore7526412009-11-11 19:31:23 +00002563 return false;
2564}
2565
Douglas Gregorc15cb382009-02-09 23:23:08 +00002566/// \brief Check that the given template argument list is well-formed
2567/// for specializing the given template.
2568bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2569 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002570 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002571 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002572 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002573 TemplateParameterList *Params = Template->getTemplateParameters();
2574 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002575 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002576 bool Invalid = false;
2577
John McCalld5532b62009-11-23 01:53:49 +00002578 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2579
Mike Stump1eb44332009-09-09 15:08:12 +00002580 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002581 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002582
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002583 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002584 (NumArgs < Params->getMinRequiredArguments() &&
2585 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002586 // FIXME: point at either the first arg beyond what we can handle,
2587 // or the '>', depending on whether we have too many or too few
2588 // arguments.
2589 SourceRange Range;
2590 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002591 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002592 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2593 << (NumArgs > NumParams)
2594 << (isa<ClassTemplateDecl>(Template)? 0 :
2595 isa<FunctionTemplateDecl>(Template)? 1 :
2596 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2597 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002598 Diag(Template->getLocation(), diag::note_template_decl_here)
2599 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002600 Invalid = true;
2601 }
Mike Stump1eb44332009-09-09 15:08:12 +00002602
2603 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002604 // [...] The type and form of each template-argument specified in
2605 // a template-id shall match the type and form specified for the
2606 // corresponding parameter declared by the template in its
2607 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002608 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002609 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2610 TemplateParameterList::iterator Param = Params->begin(),
2611 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002612 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002613 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002614 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002615 if (ArgIdx > NumArgs && PartialTemplateArgs)
2616 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002617
Douglas Gregorf35f8282009-11-11 21:54:23 +00002618 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002619 // If we have an expanded parameter pack, make sure we don't have too
2620 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002621 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002622 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002623 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002624 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2625 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2626 << true
2627 << (isa<ClassTemplateDecl>(Template)? 0 :
2628 isa<FunctionTemplateDecl>(Template)? 1 :
2629 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2630 << Template;
2631 Diag(Template->getLocation(), diag::note_template_decl_here)
2632 << Params->getSourceRange();
2633 return true;
2634 }
2635 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002636
Douglas Gregorf35f8282009-11-11 21:54:23 +00002637 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002638 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2639 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002640 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002641 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002642
Douglas Gregor14be16b2010-12-20 16:57:52 +00002643 if ((*Param)->isTemplateParameterPack()) {
2644 // The template parameter was a template parameter pack, so take the
2645 // deduced argument and place it on the argument pack. Note that we
2646 // stay on the same template parameter so that we can deduce more
2647 // arguments.
2648 ArgumentPack.push_back(Converted.back());
2649 Converted.pop_back();
2650 } else {
2651 // Move to the next template parameter.
2652 ++Param;
2653 }
2654 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002655 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002656 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002657
2658 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002659 // arguments, just break out now and we'll fill in the argument pack below.
2660 if ((*Param)->isTemplateParameterPack())
2661 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002662
Douglas Gregorf35f8282009-11-11 21:54:23 +00002663 // We have a default template argument that we will use.
2664 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002665
Douglas Gregorf35f8282009-11-11 21:54:23 +00002666 // Retrieve the default template argument from the template
2667 // parameter. For each kind of template parameter, we substitute the
2668 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002669 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002670 // the default argument.
2671 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2672 if (!TTP->hasDefaultArgument()) {
2673 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2674 break;
2675 }
2676
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002677 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002678 Template,
2679 TemplateLoc,
2680 RAngleLoc,
2681 TTP,
2682 Converted);
2683 if (!ArgType)
2684 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002685
Douglas Gregorf35f8282009-11-11 21:54:23 +00002686 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2687 ArgType);
2688 } else if (NonTypeTemplateParmDecl *NTTP
2689 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2690 if (!NTTP->hasDefaultArgument()) {
2691 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2692 break;
2693 }
2694
John McCall60d7b3a2010-08-24 06:29:42 +00002695 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696 TemplateLoc,
2697 RAngleLoc,
2698 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002699 Converted);
2700 if (E.isInvalid())
2701 return true;
2702
2703 Expr *Ex = E.takeAs<Expr>();
2704 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2705 } else {
2706 TemplateTemplateParmDecl *TempParm
2707 = cast<TemplateTemplateParmDecl>(*Param);
2708
2709 if (!TempParm->hasDefaultArgument()) {
2710 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2711 break;
2712 }
2713
Douglas Gregor1d752d72011-03-02 18:46:51 +00002714 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002715 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002716 TemplateLoc,
2717 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002718 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002719 Converted,
2720 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002721 if (Name.isNull())
2722 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002723
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002724 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2725 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002726 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727
Douglas Gregorf35f8282009-11-11 21:54:23 +00002728 // Introduce an instantiation record that describes where we are using
2729 // the default template argument.
2730 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002731 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732 SourceRange(TemplateLoc, RAngleLoc));
2733
Douglas Gregorf35f8282009-11-11 21:54:23 +00002734 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002735 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002736 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002737 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002738
Douglas Gregor67714232011-03-03 02:41:12 +00002739 // Core issue 150 (assumed resolution): if this is a template template
2740 // parameter, keep track of the default template arguments from the
2741 // template definition.
2742 if (isTemplateTemplateParameter)
2743 TemplateArgs.addArgument(Arg);
2744
Douglas Gregor14be16b2010-12-20 16:57:52 +00002745 // Move to the next template parameter and argument.
2746 ++Param;
2747 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002748 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002749
Douglas Gregor14be16b2010-12-20 16:57:52 +00002750 // Form argument packs for each of the parameter packs remaining.
2751 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002752 // If we're checking a partial list of template arguments, don't fill
2753 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002754
2755 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002756 if (PartialTemplateArgs && ArgumentPack.empty()) {
2757 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002758 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002759 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002760 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002761 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2762 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002763 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002764 ArgumentPack.clear();
2765 }
2766 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002767
Douglas Gregor14be16b2010-12-20 16:57:52 +00002768 ++Param;
2769 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002770
Douglas Gregorc15cb382009-02-09 23:23:08 +00002771 return Invalid;
2772}
2773
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002774namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002775 class UnnamedLocalNoLinkageFinder
2776 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002777 {
2778 Sema &S;
2779 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002780
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002781 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002782
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002783 public:
2784 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2785
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002786 bool Visit(QualType T) {
2787 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002788 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002790#define TYPE(Class, Parent) \
2791 bool Visit##Class##Type(const Class##Type *);
2792#define ABSTRACT_TYPE(Class, Parent) \
2793 bool Visit##Class##Type(const Class##Type *) { return false; }
2794#define NON_CANONICAL_TYPE(Class, Parent) \
2795 bool Visit##Class##Type(const Class##Type *) { return false; }
2796#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002797
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002798 bool VisitTagDecl(const TagDecl *Tag);
2799 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2800 };
2801}
2802
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002803bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002804 return false;
2805}
2806
2807bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2808 return Visit(T->getElementType());
2809}
2810
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002811bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002812 return Visit(T->getPointeeType());
2813}
2814
2815bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002816 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002817 return Visit(T->getPointeeType());
2818}
2819
2820bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002821 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002822 return Visit(T->getPointeeType());
2823}
2824
2825bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002826 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002827 return Visit(T->getPointeeType());
2828}
2829
2830bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002831 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002832 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2833}
2834
2835bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002836 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002837 return Visit(T->getElementType());
2838}
2839
2840bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002841 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002842 return Visit(T->getElementType());
2843}
2844
2845bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002846 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002847 return Visit(T->getElementType());
2848}
2849
2850bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002851 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002852 return Visit(T->getElementType());
2853}
2854
2855bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002856 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002857 return Visit(T->getElementType());
2858}
2859
2860bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2861 return Visit(T->getElementType());
2862}
2863
2864bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2865 return Visit(T->getElementType());
2866}
2867
2868bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2869 const FunctionProtoType* T) {
2870 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002871 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002872 A != AEnd; ++A) {
2873 if (Visit(*A))
2874 return true;
2875 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002876
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002877 return Visit(T->getResultType());
2878}
2879
2880bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2881 const FunctionNoProtoType* T) {
2882 return Visit(T->getResultType());
2883}
2884
2885bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2886 const UnresolvedUsingType*) {
2887 return false;
2888}
2889
2890bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2891 return false;
2892}
2893
2894bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2895 return Visit(T->getUnderlyingType());
2896}
2897
2898bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2899 return false;
2900}
2901
Richard Smith34b41d92011-02-20 03:19:35 +00002902bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2903 return Visit(T->getDeducedType());
2904}
2905
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002906bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2907 return VisitTagDecl(T->getDecl());
2908}
2909
2910bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2911 return VisitTagDecl(T->getDecl());
2912}
2913
2914bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2915 const TemplateTypeParmType*) {
2916 return false;
2917}
2918
Douglas Gregorc3069d62011-01-14 02:55:32 +00002919bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2920 const SubstTemplateTypeParmPackType *) {
2921 return false;
2922}
2923
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002924bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2925 const TemplateSpecializationType*) {
2926 return false;
2927}
2928
2929bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2930 const InjectedClassNameType* T) {
2931 return VisitTagDecl(T->getDecl());
2932}
2933
2934bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2935 const DependentNameType* T) {
2936 return VisitNestedNameSpecifier(T->getQualifier());
2937}
2938
2939bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2940 const DependentTemplateSpecializationType* T) {
2941 return VisitNestedNameSpecifier(T->getQualifier());
2942}
2943
Douglas Gregor7536dd52010-12-20 02:24:11 +00002944bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2945 const PackExpansionType* T) {
2946 return Visit(T->getPattern());
2947}
2948
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002949bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2950 return false;
2951}
2952
2953bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2954 const ObjCInterfaceType *) {
2955 return false;
2956}
2957
2958bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2959 const ObjCObjectPointerType *) {
2960 return false;
2961}
2962
2963bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2964 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2965 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2966 << S.Context.getTypeDeclType(Tag) << SR;
2967 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002968 }
2969
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002970 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2971 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2972 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2973 return true;
2974 }
2975
2976 return false;
2977}
2978
2979bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2980 NestedNameSpecifier *NNS) {
2981 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2982 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002983
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002984 switch (NNS->getKind()) {
2985 case NestedNameSpecifier::Identifier:
2986 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002987 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002988 case NestedNameSpecifier::Global:
2989 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002990
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002991 case NestedNameSpecifier::TypeSpec:
2992 case NestedNameSpecifier::TypeSpecWithTemplate:
2993 return Visit(QualType(NNS->getAsType(), 0));
2994 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002995 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002996}
2997
2998
Douglas Gregorc15cb382009-02-09 23:23:08 +00002999/// \brief Check a template argument against its corresponding
3000/// template type parameter.
3001///
3002/// This routine implements the semantics of C++ [temp.arg.type]. It
3003/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003004bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003005 TypeSourceInfo *ArgInfo) {
3006 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003007 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003008 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003009
3010 if (Arg->isVariablyModifiedType()) {
3011 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003012 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003013 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003014 }
3015
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003016 // C++03 [temp.arg.type]p2:
3017 // A local type, a type with no linkage, an unnamed type or a type
3018 // compounded from any of these types shall not be used as a
3019 // template-argument for a template type-parameter.
3020 //
3021 // C++0x allows these, and even in C++03 we allow them as an extension with
3022 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003023 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003024 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3025 (void)Finder.Visit(Context.getCanonicalType(Arg));
3026 }
3027
Douglas Gregorc15cb382009-02-09 23:23:08 +00003028 return false;
3029}
3030
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003031/// \brief Checks whether the given template argument is the address
3032/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003033static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003034CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3035 NonTypeTemplateParmDecl *Param,
3036 QualType ParamType,
3037 Expr *ArgIn,
3038 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003039 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003040 Expr *Arg = ArgIn;
3041 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003042
3043 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003044 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003045 Arg = Cast->getSubExpr();
3046
3047 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003048 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003049 // A template-argument for a non-type, non-template
3050 // template-parameter shall be one of: [...]
3051 //
3052 // -- the address of an object or function with external
3053 // linkage, including function templates and function
3054 // template-ids but excluding non-static class members,
3055 // expressed as & id-expression where the & is optional if
3056 // the name refers to a function or array, or if the
3057 // corresponding template-parameter is a reference; or
3058 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003059
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003060 // In C++98/03 mode, give an extension warning on any extra parentheses.
3061 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3062 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003063 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003064 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003065 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003066 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003067 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003068 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003069 }
3070
3071 Arg = Parens->getSubExpr();
3072 }
3073
Douglas Gregorb7a09262010-04-01 18:32:35 +00003074 bool AddressTaken = false;
3075 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003076 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003077 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003078 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003079 AddressTaken = true;
3080 AddrOpLoc = UnOp->getOperatorLoc();
3081 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003082 } else
3083 DRE = dyn_cast<DeclRefExpr>(Arg);
3084
Douglas Gregorb7a09262010-04-01 18:32:35 +00003085 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003086 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3087 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003088 S.Diag(Param->getLocation(), diag::note_template_param_here);
3089 return true;
3090 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003091
3092 // Stop checking the precise nature of the argument if it is value dependent,
3093 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003094 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003095 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003096 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003097 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003098
Douglas Gregorb7a09262010-04-01 18:32:35 +00003099 if (!isa<ValueDecl>(DRE->getDecl())) {
3100 S.Diag(Arg->getSourceRange().getBegin(),
3101 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003102 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003103 S.Diag(Param->getLocation(), diag::note_template_param_here);
3104 return true;
3105 }
3106
3107 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003108
3109 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003110 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3111 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003112 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003113 S.Diag(Param->getLocation(), diag::note_template_param_here);
3114 return true;
3115 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003116
3117 // Cannot refer to non-static member functions
3118 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003119 if (!Method->isStatic()) {
3120 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003121 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003122 S.Diag(Param->getLocation(), diag::note_template_param_here);
3123 return true;
3124 }
Mike Stump1eb44332009-09-09 15:08:12 +00003125
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003126 // Functions must have external linkage.
3127 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003128 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003129 S.Diag(Arg->getSourceRange().getBegin(),
3130 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003131 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003132 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003133 << true;
3134 return true;
3135 }
3136
3137 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003138 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003139
Douglas Gregorb7a09262010-04-01 18:32:35 +00003140 // If the template parameter has pointer type, the function decays.
3141 if (ParamType->isPointerType() && !AddressTaken)
3142 ArgType = S.Context.getPointerType(Func->getType());
3143 else if (AddressTaken && ParamType->isReferenceType()) {
3144 // If we originally had an address-of operator, but the
3145 // parameter has reference type, complain and (if things look
3146 // like they will work) drop the address-of operator.
3147 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3148 ParamType.getNonReferenceType())) {
3149 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3150 << ParamType;
3151 S.Diag(Param->getLocation(), diag::note_template_param_here);
3152 return true;
3153 }
3154
3155 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3156 << ParamType
3157 << FixItHint::CreateRemoval(AddrOpLoc);
3158 S.Diag(Param->getLocation(), diag::note_template_param_here);
3159
3160 ArgType = Func->getType();
3161 }
3162 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003163 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003164 S.Diag(Arg->getSourceRange().getBegin(),
3165 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003166 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003167 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003168 << true;
3169 return true;
3170 }
3171
Douglas Gregorb7a09262010-04-01 18:32:35 +00003172 // A value of reference type is not an object.
3173 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003174 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003175 diag::err_template_arg_reference_var)
3176 << Var->getType() << Arg->getSourceRange();
3177 S.Diag(Param->getLocation(), diag::note_template_param_here);
3178 return true;
3179 }
3180
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003181 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003182 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003183
3184 // If the template parameter has pointer type, we must have taken
3185 // the address of this object.
3186 if (ParamType->isReferenceType()) {
3187 if (AddressTaken) {
3188 // If we originally had an address-of operator, but the
3189 // parameter has reference type, complain and (if things look
3190 // like they will work) drop the address-of operator.
3191 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3192 ParamType.getNonReferenceType())) {
3193 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3194 << ParamType;
3195 S.Diag(Param->getLocation(), diag::note_template_param_here);
3196 return true;
3197 }
3198
3199 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3200 << ParamType
3201 << FixItHint::CreateRemoval(AddrOpLoc);
3202 S.Diag(Param->getLocation(), diag::note_template_param_here);
3203
3204 ArgType = Var->getType();
3205 }
3206 } else if (!AddressTaken && ParamType->isPointerType()) {
3207 if (Var->getType()->isArrayType()) {
3208 // Array-to-pointer decay.
3209 ArgType = S.Context.getArrayDecayedType(Var->getType());
3210 } else {
3211 // If the template parameter has pointer type but the address of
3212 // this object was not taken, complain and (possibly) recover by
3213 // taking the address of the entity.
3214 ArgType = S.Context.getPointerType(Var->getType());
3215 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3216 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3217 << ParamType;
3218 S.Diag(Param->getLocation(), diag::note_template_param_here);
3219 return true;
3220 }
3221
3222 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3223 << ParamType
3224 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3225
3226 S.Diag(Param->getLocation(), diag::note_template_param_here);
3227 }
3228 }
3229 } else {
3230 // We found something else, but we don't know specifically what it is.
3231 S.Diag(Arg->getSourceRange().getBegin(),
3232 diag::err_template_arg_not_object_or_func)
3233 << Arg->getSourceRange();
3234 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3235 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003236 }
Mike Stump1eb44332009-09-09 15:08:12 +00003237
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003238 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003239 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003240 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003241 // For pointer-to-object types, qualification conversions are
3242 // permitted.
3243 } else {
3244 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3245 if (!ParamRef->getPointeeType()->isFunctionType()) {
3246 // C++ [temp.arg.nontype]p5b3:
3247 // For a non-type template-parameter of type reference to
3248 // object, no conversions apply. The type referred to by the
3249 // reference may be more cv-qualified than the (otherwise
3250 // identical) type of the template- argument. The
3251 // template-parameter is bound directly to the
3252 // template-argument, which shall be an lvalue.
3253
3254 // FIXME: Other qualifiers?
3255 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3256 unsigned ArgQuals = ArgType.getCVRQualifiers();
3257
3258 if ((ParamQuals | ArgQuals) != ParamQuals) {
3259 S.Diag(Arg->getSourceRange().getBegin(),
3260 diag::err_template_arg_ref_bind_ignores_quals)
3261 << ParamType << Arg->getType()
3262 << Arg->getSourceRange();
3263 S.Diag(Param->getLocation(), diag::note_template_param_here);
3264 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003265 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003266 }
3267 }
3268
3269 // At this point, the template argument refers to an object or
3270 // function with external linkage. We now need to check whether the
3271 // argument and parameter types are compatible.
3272 if (!S.Context.hasSameUnqualifiedType(ArgType,
3273 ParamType.getNonReferenceType())) {
3274 // We can't perform this conversion or binding.
3275 if (ParamType->isReferenceType())
3276 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3277 << ParamType << Arg->getType() << Arg->getSourceRange();
3278 else
3279 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3280 << Arg->getType() << ParamType << Arg->getSourceRange();
3281 S.Diag(Param->getLocation(), diag::note_template_param_here);
3282 return true;
3283 }
3284 }
3285
3286 // Create the template argument.
3287 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003288 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003289 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003290}
3291
3292/// \brief Checks whether the given template argument is a pointer to
3293/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003294bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003295 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003296 bool Invalid = false;
3297
3298 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003299 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003300 Arg = Cast->getSubExpr();
3301
3302 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003303 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003304 // A template-argument for a non-type, non-template
3305 // template-parameter shall be one of: [...]
3306 //
3307 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003308 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003309
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003310 // In C++98/03 mode, give an extension warning on any extra parentheses.
3311 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3312 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003313 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003314 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003315 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003316 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003317 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003318 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003319 }
3320
3321 Arg = Parens->getSubExpr();
3322 }
3323
Douglas Gregorcaddba02009-11-12 18:38:13 +00003324 // A pointer-to-member constant written &Class::member.
3325 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003326 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003327 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3328 if (DRE && !DRE->getQualifier())
3329 DRE = 0;
3330 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003331 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003332 // A constant of pointer-to-member type.
3333 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3334 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3335 if (VD->getType()->isMemberPointerType()) {
3336 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003337 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003338 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3339 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003340 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003341 else
3342 Converted = TemplateArgument(VD->getCanonicalDecl());
3343 return Invalid;
3344 }
3345 }
3346 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003347
Douglas Gregorcaddba02009-11-12 18:38:13 +00003348 DRE = 0;
3349 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003350
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003351 if (!DRE)
3352 return Diag(Arg->getSourceRange().getBegin(),
3353 diag::err_template_arg_not_pointer_to_member_form)
3354 << Arg->getSourceRange();
3355
3356 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3357 assert((isa<FieldDecl>(DRE->getDecl()) ||
3358 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3359 "Only non-static member pointers can make it here");
3360
3361 // Okay: this is the address of a non-static member, and therefore
3362 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003363 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003364 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003365 else
3366 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003367 return Invalid;
3368 }
3369
3370 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003371 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003372 diag::err_template_arg_not_pointer_to_member_form)
3373 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003374 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003375 diag::note_template_arg_refers_here);
3376 return true;
3377}
3378
Douglas Gregorc15cb382009-02-09 23:23:08 +00003379/// \brief Check a template argument against its corresponding
3380/// non-type template parameter.
3381///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003382/// This routine implements the semantics of C++ [temp.arg.nontype].
3383/// It returns true if an error occurred, and false otherwise. \p
3384/// InstantiatedParamType is the type of the non-type template
3385/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003386///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003387/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003388bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003389 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003390 TemplateArgument &Converted,
3391 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003392 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3393
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003394 // If either the parameter has a dependent type or the argument is
3395 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003396 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3397 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003398 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003399 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003400 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003401
3402 // C++ [temp.arg.nontype]p5:
3403 // The following conversions are performed on each expression used
3404 // as a non-type template-argument. If a non-type
3405 // template-argument cannot be converted to the type of the
3406 // corresponding template-parameter then the program is
3407 // ill-formed.
3408 //
3409 // -- for a non-type template-parameter of integral or
3410 // enumeration type, integral promotions (4.5) and integral
3411 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003412 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003413 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003414 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003415 // C++ [temp.arg.nontype]p1:
3416 // A template-argument for a non-type, non-template
3417 // template-parameter shall be one of:
3418 //
3419 // -- an integral constant-expression of integral or enumeration
3420 // type; or
3421 // -- the name of a non-type template-parameter; or
3422 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003423 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003424 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003425 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003426 diag::err_template_arg_not_integral_or_enumeral)
3427 << ArgType << Arg->getSourceRange();
3428 Diag(Param->getLocation(), diag::note_template_param_here);
3429 return true;
3430 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003431 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003432 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3433 << ArgType << Arg->getSourceRange();
3434 return true;
3435 }
3436
Douglas Gregor02024a92010-03-28 02:42:43 +00003437 // From here on out, all we care about are the unqualified forms
3438 // of the parameter and argument types.
3439 ParamType = ParamType.getUnqualifiedType();
3440 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003441
3442 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003443 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003444 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003445 } else if (CTAK == CTAK_Deduced) {
3446 // C++ [temp.deduct.type]p17:
3447 // If, in the declaration of a function template with a non-type
3448 // template-parameter, the non-type template- parameter is used
3449 // in an expression in the function parameter-list and, if the
3450 // corresponding template-argument is deduced, the
3451 // template-argument type shall match the type of the
3452 // template-parameter exactly, except that a template-argument
3453 // deduced from an array bound may be of any integral type.
3454 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3455 << ArgType << ParamType;
3456 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003457 return true;
3458 } else if (ParamType->isBooleanType()) {
3459 // This is an integral-to-boolean conversion.
3460 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003461 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3462 !ParamType->isEnumeralType()) {
3463 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003464 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003465 } else {
3466 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003467 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003468 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003469 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003470 Diag(Param->getLocation(), diag::note_template_param_here);
3471 return true;
3472 }
3473
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003474 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003475 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003476 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003477
3478 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003479 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003480
3481 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003482 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003483 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003484 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003485 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003486 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003487
3488 // Complain if an unsigned parameter received a negative value.
3489 if (IntegerType->isUnsignedIntegerType()
3490 && (OldValue.isSigned() && OldValue.isNegative())) {
3491 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3492 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3493 << Arg->getSourceRange();
3494 Diag(Param->getLocation(), diag::note_template_param_here);
3495 }
3496
3497 // Complain if we overflowed the template parameter's type.
3498 unsigned RequiredBits;
3499 if (IntegerType->isUnsignedIntegerType())
3500 RequiredBits = OldValue.getActiveBits();
3501 else if (OldValue.isUnsigned())
3502 RequiredBits = OldValue.getActiveBits() + 1;
3503 else
3504 RequiredBits = OldValue.getMinSignedBits();
3505 if (RequiredBits > AllowedBits) {
3506 Diag(Arg->getSourceRange().getBegin(),
3507 diag::warn_template_arg_too_large)
3508 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3509 << Arg->getSourceRange();
3510 Diag(Param->getLocation(), diag::note_template_param_here);
3511 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003512 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003513
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003514 // Add the value of this argument to the list of converted
3515 // arguments. We use the bitwidth and signedness of the template
3516 // parameter.
3517 if (Arg->isValueDependent()) {
3518 // The argument is value-dependent. Create a new
3519 // TemplateArgument with the converted expression.
3520 Converted = TemplateArgument(Arg);
3521 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003522 }
3523
John McCall833ca992009-10-29 08:12:44 +00003524 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003525 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003526 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003527 return false;
3528 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003529
John McCall6bb80172010-03-30 21:47:33 +00003530 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3531
Douglas Gregorb7a09262010-04-01 18:32:35 +00003532 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3533 // from a template argument of type std::nullptr_t to a non-type
3534 // template parameter of type pointer to object, pointer to
3535 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003536 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003537 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3538 Converted = TemplateArgument((NamedDecl *)0);
3539 return false;
3540 }
3541
Douglas Gregorb86b0572009-02-11 01:18:59 +00003542 // Handle pointer-to-function, reference-to-function, and
3543 // pointer-to-member-function all in (roughly) the same way.
3544 if (// -- For a non-type template-parameter of type pointer to
3545 // function, only the function-to-pointer conversion (4.3) is
3546 // applied. If the template-argument represents a set of
3547 // overloaded functions (or a pointer to such), the matching
3548 // function is selected from the set (13.4).
3549 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003550 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003551 // -- For a non-type template-parameter of type reference to
3552 // function, no conversions apply. If the template-argument
3553 // represents a set of overloaded functions, the matching
3554 // function is selected from the set (13.4).
3555 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003556 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003557 // -- For a non-type template-parameter of type pointer to
3558 // member function, no conversions apply. If the
3559 // template-argument represents a set of overloaded member
3560 // functions, the matching member function is selected from
3561 // the set (13.4).
3562 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003563 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003564 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003565
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003566 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003567 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003568 true,
3569 FoundResult)) {
3570 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3571 return true;
3572
3573 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3574 ArgType = Arg->getType();
3575 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003576 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003577 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003578
Douglas Gregorb7a09262010-04-01 18:32:35 +00003579 if (!ParamType->isMemberPointerType())
3580 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003581 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003582 Arg, Converted);
3583
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003584 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3585 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003586 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003587 } else if (!Context.hasSameUnqualifiedType(ArgType,
3588 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003589 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003590 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003591 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003592 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003593 Diag(Param->getLocation(), diag::note_template_param_here);
3594 return true;
3595 }
Mike Stump1eb44332009-09-09 15:08:12 +00003596
Douglas Gregorb7a09262010-04-01 18:32:35 +00003597 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003598 }
3599
Chris Lattnerfe90de72009-02-20 21:37:53 +00003600 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003601 // -- for a non-type template-parameter of type pointer to
3602 // object, qualification conversions (4.4) and the
3603 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003604 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003605 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003606 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003607
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003608 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003609 ParamType,
3610 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003611 }
Mike Stump1eb44332009-09-09 15:08:12 +00003612
Ted Kremenek6217b802009-07-29 21:53:49 +00003613 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003614 // -- For a non-type template-parameter of type reference to
3615 // object, no conversions apply. The type referred to by the
3616 // reference may be more cv-qualified than the (otherwise
3617 // identical) type of the template-argument. The
3618 // template-parameter is bound directly to the
3619 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003620 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003621 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003622
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003623 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003624 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3625 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003626 true,
3627 FoundResult)) {
3628 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3629 return true;
3630
3631 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3632 ArgType = Arg->getType();
3633 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003634 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003635 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003636
3637 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003638 ParamType,
3639 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003640 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003641
3642 // -- For a non-type template-parameter of type pointer to data
3643 // member, qualification conversions (4.4) are applied.
3644 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3645
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003646 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003647 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003648 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003649 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003650 } else {
3651 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003652 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003653 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003654 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003655 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003656 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003657 }
3658
Douglas Gregorcaddba02009-11-12 18:38:13 +00003659 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003660}
3661
3662/// \brief Check a template argument against its corresponding
3663/// template template parameter.
3664///
3665/// This routine implements the semantics of C++ [temp.arg.template].
3666/// It returns true if an error occurred, and false otherwise.
3667bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003668 const TemplateArgumentLoc &Arg) {
3669 TemplateName Name = Arg.getArgument().getAsTemplate();
3670 TemplateDecl *Template = Name.getAsTemplateDecl();
3671 if (!Template) {
3672 // Any dependent template name is fine.
3673 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3674 return false;
3675 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003676
3677 // C++ [temp.arg.template]p1:
3678 // A template-argument for a template template-parameter shall be
3679 // the name of a class template, expressed as id-expression. Only
3680 // primary class templates are considered when matching the
3681 // template template argument with the corresponding parameter;
3682 // partial specializations are not considered even if their
3683 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003684 //
3685 // Note that we also allow template template parameters here, which
3686 // will happen when we are dealing with, e.g., class template
3687 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003688 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003689 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003690 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003691 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003692 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003693 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003694 << Template;
3695 }
3696
3697 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3698 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003699 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003700 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003701 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003702}
3703
Douglas Gregor02024a92010-03-28 02:42:43 +00003704/// \brief Given a non-type template argument that refers to a
3705/// declaration and the type of its corresponding non-type template
3706/// parameter, produce an expression that properly refers to that
3707/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003708ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003709Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3710 QualType ParamType,
3711 SourceLocation Loc) {
3712 assert(Arg.getKind() == TemplateArgument::Declaration &&
3713 "Only declaration template arguments permitted here");
3714 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3715
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003716 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003717 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3718 // If the value is a class member, we might have a pointer-to-member.
3719 // Determine whether the non-type template template parameter is of
3720 // pointer-to-member type. If so, we need to build an appropriate
3721 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3722 // would refer to the member itself.
3723 if (ParamType->isMemberPointerType()) {
3724 QualType ClassType
3725 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3726 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003727 = NestedNameSpecifier::Create(Context, 0, false,
3728 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003729 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003730 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003731
3732 // The actual value-ness of this is unimportant, but for
3733 // internal consistency's sake, references to instance methods
3734 // are r-values.
3735 ExprValueKind VK = VK_LValue;
3736 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3737 VK = VK_RValue;
3738
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003739 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003740 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003741 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003742 Loc,
3743 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003744 if (RefExpr.isInvalid())
3745 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003746
John McCall2de56d12010-08-25 11:45:40 +00003747 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003748
Douglas Gregorc0c83002010-04-30 21:46:38 +00003749 // We might need to perform a trailing qualification conversion, since
3750 // the element type on the parameter could be more qualified than the
3751 // element type in the expression we constructed.
3752 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003753 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003754 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003755 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003756 RefExpr = Owned(RefE);
3757 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003758
Douglas Gregor02024a92010-03-28 02:42:43 +00003759 assert(!RefExpr.isInvalid() &&
3760 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003761 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003762 return move(RefExpr);
3763 }
3764 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003765
Douglas Gregor02024a92010-03-28 02:42:43 +00003766 QualType T = VD->getType().getNonReferenceType();
3767 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003768 // When the non-type template parameter is a pointer, take the
3769 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003770 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003771 if (RefExpr.isInvalid())
3772 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003773
3774 if (T->isFunctionType() || T->isArrayType()) {
3775 // Decay functions and arrays.
3776 Expr *RefE = (Expr *)RefExpr.get();
3777 DefaultFunctionArrayConversion(RefE);
3778 if (RefE != RefExpr.get()) {
3779 RefExpr.release();
3780 RefExpr = Owned(RefE);
3781 }
3782
3783 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003784 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003785
Douglas Gregorb7a09262010-04-01 18:32:35 +00003786 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003787 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003788 }
3789
John McCallf89e55a2010-11-18 06:31:45 +00003790 ExprValueKind VK = VK_RValue;
3791
Douglas Gregor02024a92010-03-28 02:42:43 +00003792 // If the non-type template parameter has reference type, qualify the
3793 // resulting declaration reference with the extra qualifiers on the
3794 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003795 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3796 VK = VK_LValue;
3797 T = Context.getQualifiedType(T,
3798 TargetRef->getPointeeType().getQualifiers());
3799 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003800
John McCallf89e55a2010-11-18 06:31:45 +00003801 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003802}
3803
3804/// \brief Construct a new expression that refers to the given
3805/// integral template argument with the given source-location
3806/// information.
3807///
3808/// This routine takes care of the mapping from an integral template
3809/// argument (which may have any integral type) to the appropriate
3810/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003811ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003812Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3813 SourceLocation Loc) {
3814 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003815 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003816 QualType T = Arg.getIntegralType();
3817 if (T->isCharType() || T->isWideCharType())
3818 return Owned(new (Context) CharacterLiteral(
3819 Arg.getAsIntegral()->getZExtValue(),
3820 T->isWideCharType(),
3821 T,
3822 Loc));
3823 if (T->isBooleanType())
3824 return Owned(new (Context) CXXBoolLiteralExpr(
3825 Arg.getAsIntegral()->getBoolValue(),
3826 T,
3827 Loc));
3828
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003829 QualType BT;
3830 if (const EnumType *ET = T->getAs<EnumType>())
3831 BT = ET->getDecl()->getPromotionType();
3832 else
3833 BT = T;
3834
3835 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003836 if (T->isEnumeralType()) {
3837 // FIXME: This is a hack. We need a better way to handle substituted
3838 // non-type template parameters.
3839 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3840 E, 0,
3841 Context.getTrivialTypeSourceInfo(T, Loc),
3842 Loc, Loc);
3843 }
3844
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003845 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003846}
3847
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003848/// \brief Match two template parameters within template parameter lists.
3849static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3850 bool Complain,
3851 Sema::TemplateParameterListEqualKind Kind,
3852 SourceLocation TemplateArgLoc) {
3853 // Check the actual kind (type, non-type, template).
3854 if (Old->getKind() != New->getKind()) {
3855 if (Complain) {
3856 unsigned NextDiag = diag::err_template_param_different_kind;
3857 if (TemplateArgLoc.isValid()) {
3858 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3859 NextDiag = diag::note_template_param_different_kind;
3860 }
3861 S.Diag(New->getLocation(), NextDiag)
3862 << (Kind != Sema::TPL_TemplateMatch);
3863 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3864 << (Kind != Sema::TPL_TemplateMatch);
3865 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003866
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003867 return false;
3868 }
3869
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003870 // Check that both are parameter packs are neither are parameter packs.
3871 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003872 // template template parameter, the template template parameter can have
3873 // a parameter pack where the template template argument does not.
3874 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3875 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3876 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003877 if (Complain) {
3878 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3879 if (TemplateArgLoc.isValid()) {
3880 S.Diag(TemplateArgLoc,
3881 diag::err_template_arg_template_params_mismatch);
3882 NextDiag = diag::note_template_parameter_pack_non_pack;
3883 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003884
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003885 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3886 : isa<NonTypeTemplateParmDecl>(New)? 1
3887 : 2;
3888 S.Diag(New->getLocation(), NextDiag)
3889 << ParamKind << New->isParameterPack();
3890 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3891 << ParamKind << Old->isParameterPack();
3892 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003893
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003894 return false;
3895 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003896
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003897 // For non-type template parameters, check the type of the parameter.
3898 if (NonTypeTemplateParmDecl *OldNTTP
3899 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3900 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003901
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003902 // If we are matching a template template argument to a template
3903 // template parameter and one of the non-type template parameter types
3904 // is dependent, then we must wait until template instantiation time
3905 // to actually compare the arguments.
3906 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3907 (OldNTTP->getType()->isDependentType() ||
3908 NewNTTP->getType()->isDependentType()))
3909 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003910
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003911 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3912 if (Complain) {
3913 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3914 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003915 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003916 diag::err_template_arg_template_params_mismatch);
3917 NextDiag = diag::note_template_nontype_parm_different_type;
3918 }
3919 S.Diag(NewNTTP->getLocation(), NextDiag)
3920 << NewNTTP->getType()
3921 << (Kind != Sema::TPL_TemplateMatch);
3922 S.Diag(OldNTTP->getLocation(),
3923 diag::note_template_nontype_parm_prev_declaration)
3924 << OldNTTP->getType();
3925 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003926
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003927 return false;
3928 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003929
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003930 return true;
3931 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003932
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003933 // For template template parameters, check the template parameter types.
3934 // The template parameter lists of template template
3935 // parameters must agree.
3936 if (TemplateTemplateParmDecl *OldTTP
3937 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003938 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003939 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3940 OldTTP->getTemplateParameters(),
3941 Complain,
3942 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003943 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003944 : Kind),
3945 TemplateArgLoc);
3946 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003947
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003948 return true;
3949}
Douglas Gregor02024a92010-03-28 02:42:43 +00003950
Douglas Gregora0347822011-01-13 00:08:50 +00003951/// \brief Diagnose a known arity mismatch when comparing template argument
3952/// lists.
3953static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003954void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003955 TemplateParameterList *New,
3956 TemplateParameterList *Old,
3957 Sema::TemplateParameterListEqualKind Kind,
3958 SourceLocation TemplateArgLoc) {
3959 unsigned NextDiag = diag::err_template_param_list_different_arity;
3960 if (TemplateArgLoc.isValid()) {
3961 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3962 NextDiag = diag::note_template_param_list_different_arity;
3963 }
3964 S.Diag(New->getTemplateLoc(), NextDiag)
3965 << (New->size() > Old->size())
3966 << (Kind != Sema::TPL_TemplateMatch)
3967 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3968 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3969 << (Kind != Sema::TPL_TemplateMatch)
3970 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3971}
3972
Douglas Gregorddc29e12009-02-06 22:42:48 +00003973/// \brief Determine whether the given template parameter lists are
3974/// equivalent.
3975///
Mike Stump1eb44332009-09-09 15:08:12 +00003976/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003977/// source code as part of a new template declaration.
3978///
3979/// \param Old The old template parameter list, typically found via
3980/// name lookup of the template declared with this template parameter
3981/// list.
3982///
3983/// \param Complain If true, this routine will produce a diagnostic if
3984/// the template parameter lists are not equivalent.
3985///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003986/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003987///
3988/// \param TemplateArgLoc If this source location is valid, then we
3989/// are actually checking the template parameter list of a template
3990/// argument (New) against the template parameter list of its
3991/// corresponding template template parameter (Old). We produce
3992/// slightly different diagnostics in this scenario.
3993///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003994/// \returns True if the template parameter lists are equal, false
3995/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003996bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003997Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3998 TemplateParameterList *Old,
3999 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004000 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004001 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004002 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4003 if (Complain)
4004 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4005 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004006
4007 return false;
4008 }
4009
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004010 // C++0x [temp.arg.template]p3:
4011 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004012 // when each of the template parameters in the template-parameter-list of
4013 // the template-argument's corresponding class template or template alias
4014 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004015 // template-parameter-list of P. [...]
4016 TemplateParameterList::iterator NewParm = New->begin();
4017 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004018 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004019 OldParmEnd = Old->end();
4020 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004021 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4022 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004023 if (NewParm == NewParmEnd) {
4024 if (Complain)
4025 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4026 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004027
Douglas Gregora0347822011-01-13 00:08:50 +00004028 return false;
4029 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004030
Douglas Gregora0347822011-01-13 00:08:50 +00004031 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4032 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004033 return false;
4034
Douglas Gregora0347822011-01-13 00:08:50 +00004035 ++NewParm;
4036 continue;
4037 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004038
Douglas Gregora0347822011-01-13 00:08:50 +00004039 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004040 // [...] When P's template- parameter-list contains a template parameter
4041 // pack (14.5.3), the template parameter pack will match zero or more
4042 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004043 // template-parameter-list of A with the same type and form as the
4044 // template parameter pack in P (ignoring whether those template
4045 // parameters are template parameter packs).
4046 for (; NewParm != NewParmEnd; ++NewParm) {
4047 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4048 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004049 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004050 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004051 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004052
Douglas Gregora0347822011-01-13 00:08:50 +00004053 // Make sure we exhausted all of the arguments.
4054 if (NewParm != NewParmEnd) {
4055 if (Complain)
4056 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4057 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004058
Douglas Gregora0347822011-01-13 00:08:50 +00004059 return false;
4060 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004061
Douglas Gregorddc29e12009-02-06 22:42:48 +00004062 return true;
4063}
4064
4065/// \brief Check whether a template can be declared within this scope.
4066///
4067/// If the template declaration is valid in this scope, returns
4068/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004069bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004070Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004071 // Find the nearest enclosing declaration scope.
4072 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4073 (S->getFlags() & Scope::TemplateParamScope) != 0)
4074 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004075
Douglas Gregorddc29e12009-02-06 22:42:48 +00004076 // C++ [temp]p2:
4077 // A template-declaration can appear only as a namespace scope or
4078 // class scope declaration.
4079 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004080 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4081 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004082 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004083 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004084
Eli Friedman1503f772009-07-31 01:43:05 +00004085 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004086 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004087
4088 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4089 return false;
4090
Mike Stump1eb44332009-09-09 15:08:12 +00004091 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004092 diag::err_template_outside_namespace_or_class_scope)
4093 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004094}
Douglas Gregorcc636682009-02-17 23:15:12 +00004095
Douglas Gregord5cb8762009-10-07 00:13:32 +00004096/// \brief Determine what kind of template specialization the given declaration
4097/// is.
4098static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4099 if (!D)
4100 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004101
Douglas Gregorf6b11852009-10-08 15:14:33 +00004102 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4103 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004104 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4105 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004106 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4107 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004108
Douglas Gregord5cb8762009-10-07 00:13:32 +00004109 return TSK_Undeclared;
4110}
4111
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004112/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004113/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004114///
Douglas Gregor9302da62009-10-14 23:50:59 +00004115/// This routine determines whether a template specialization can be declared
4116/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004117///
4118/// \param S the semantic analysis object for which this check is being
4119/// performed.
4120///
4121/// \param Specialized the entity being specialized or instantiated, which
4122/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004123/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004124/// member class).
4125///
4126/// \param PrevDecl the previous declaration of this entity, if any.
4127///
4128/// \param Loc the location of the explicit specialization or instantiation of
4129/// this entity.
4130///
4131/// \param IsPartialSpecialization whether this is a partial specialization of
4132/// a class template.
4133///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004134/// \returns true if there was an error that we cannot recover from, false
4135/// otherwise.
4136static bool CheckTemplateSpecializationScope(Sema &S,
4137 NamedDecl *Specialized,
4138 NamedDecl *PrevDecl,
4139 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004140 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004141 // Keep these "kind" numbers in sync with the %select statements in the
4142 // various diagnostics emitted by this routine.
4143 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004144 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004145 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004146 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004147 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004148 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004149 EntityKind = 3;
4150 else if (isa<VarDecl>(Specialized))
4151 EntityKind = 4;
4152 else if (isa<RecordDecl>(Specialized))
4153 EntityKind = 5;
4154 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004155 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4156 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004157 return true;
4158 }
4159
Douglas Gregor88b70942009-02-25 22:02:03 +00004160 // C++ [temp.expl.spec]p2:
4161 // An explicit specialization shall be declared in the namespace
4162 // of which the template is a member, or, for member templates, in
4163 // the namespace of which the enclosing class or enclosing class
4164 // template is a member. An explicit specialization of a member
4165 // function, member class or static data member of a class
4166 // template shall be declared in the namespace of which the class
4167 // template is a member. Such a declaration may also be a
4168 // definition. If the declaration is not a definition, the
4169 // specialization may be defined later in the name- space in which
4170 // the explicit specialization was declared, or in a namespace
4171 // that encloses the one in which the explicit specialization was
4172 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004173 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004174 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004175 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004176 return true;
4177 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004178
Douglas Gregor0a407472009-10-07 17:30:37 +00004179 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4180 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004181 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004182 return true;
4183 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004184
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004185 // C++ [temp.class.spec]p6:
4186 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004187 // in any namespace scope in which its definition may be defined (14.5.1
4188 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004189 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004190 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004191 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004192 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004193 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004194 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4195 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004196 // C++ [temp.exp.spec]p2:
4197 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004198 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004199 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004200 // An explicit specialization of a member function, member class or
4201 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004202 // namespace of which the class template is a member.
4203 //
4204 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004205 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004206 // the specialized template.
4207 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4208 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004209 bool IsCPlusPlus0xExtension
4210 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004211 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004212 S.Diag(Loc, IsCPlusPlus0xExtension
4213 ? diag::ext_template_spec_decl_out_of_scope_global
4214 : diag::err_template_spec_decl_out_of_scope_global)
4215 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004216 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004217 S.Diag(Loc, IsCPlusPlus0xExtension
4218 ? diag::ext_template_spec_decl_out_of_scope
4219 : diag::err_template_spec_decl_out_of_scope)
4220 << EntityKind << Specialized
4221 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004222
Douglas Gregor9302da62009-10-14 23:50:59 +00004223 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4224 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004225 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004226 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004227
4228 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004229 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004230 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004231 // specializations of function templates, static data members, and member
4232 // functions, so we skip the check here for those kinds of entities.
4233 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004234 // Should we refactor that check, so that it occurs later?
4235 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004236 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4237 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004238 if (isa<TranslationUnitDecl>(SpecializedContext))
4239 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4240 << EntityKind << Specialized;
4241 else if (isa<NamespaceDecl>(SpecializedContext))
4242 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4243 << EntityKind << Specialized
4244 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004245
Douglas Gregor9302da62009-10-14 23:50:59 +00004246 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004247 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004248
Douglas Gregord5cb8762009-10-07 00:13:32 +00004249 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004250
Douglas Gregor88b70942009-02-25 22:02:03 +00004251 return false;
4252}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004253
Douglas Gregorbacb9492011-01-03 21:13:47 +00004254/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4255/// that checks non-type template partial specialization arguments.
4256static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4257 NonTypeTemplateParmDecl *Param,
4258 const TemplateArgument *Args,
4259 unsigned NumArgs) {
4260 for (unsigned I = 0; I != NumArgs; ++I) {
4261 if (Args[I].getKind() == TemplateArgument::Pack) {
4262 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004263 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004264 Args[I].pack_size()))
4265 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004266
Douglas Gregore94866f2009-06-12 21:21:02 +00004267 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004268 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004269
Douglas Gregorbacb9492011-01-03 21:13:47 +00004270 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004271 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004272 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004273 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004274
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004275 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004276 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4277 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004278
4279 // Strip off any implicit casts we added as part of type checking.
4280 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4281 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004282
Douglas Gregore94866f2009-06-12 21:21:02 +00004283 // C++ [temp.class.spec]p8:
4284 // A non-type argument is non-specialized if it is the name of a
4285 // non-type parameter. All other non-type arguments are
4286 // specialized.
4287 //
4288 // Below, we check the two conditions that only apply to
4289 // specialized non-type arguments, so skip any non-specialized
4290 // arguments.
4291 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004292 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004293 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004294
Douglas Gregore94866f2009-06-12 21:21:02 +00004295 // C++ [temp.class.spec]p9:
4296 // Within the argument list of a class template partial
4297 // specialization, the following restrictions apply:
4298 // -- A partially specialized non-type argument expression
4299 // shall not involve a template parameter of the partial
4300 // specialization except when the argument expression is a
4301 // simple identifier.
4302 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004303 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004304 diag::err_dependent_non_type_arg_in_partial_spec)
4305 << ArgExpr->getSourceRange();
4306 return true;
4307 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004308
Douglas Gregore94866f2009-06-12 21:21:02 +00004309 // -- The type of a template parameter corresponding to a
4310 // specialized non-type argument shall not be dependent on a
4311 // parameter of the specialization.
4312 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004313 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004314 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4315 << Param->getType()
4316 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004317 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004318 return true;
4319 }
4320 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004321
Douglas Gregorbacb9492011-01-03 21:13:47 +00004322 return false;
4323}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004324
Douglas Gregorbacb9492011-01-03 21:13:47 +00004325/// \brief Check the non-type template arguments of a class template
4326/// partial specialization according to C++ [temp.class.spec]p9.
4327///
4328/// \param TemplateParams the template parameters of the primary class
4329/// template.
4330///
4331/// \param TemplateArg the template arguments of the class template
4332/// partial specialization.
4333///
4334/// \returns true if there was an error, false otherwise.
4335static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4336 TemplateParameterList *TemplateParams,
4337 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4338 const TemplateArgument *ArgList = TemplateArgs.data();
4339
4340 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4341 NonTypeTemplateParmDecl *Param
4342 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4343 if (!Param)
4344 continue;
4345
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004346 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004347 &ArgList[I], 1))
4348 return true;
4349 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004350
4351 return false;
4352}
4353
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004354/// \brief Retrieve the previous declaration of the given declaration.
4355static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4356 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4357 return VD->getPreviousDeclaration();
4358 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4359 return FD->getPreviousDeclaration();
4360 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4361 return TD->getPreviousDeclaration();
4362 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4363 return TD->getPreviousDeclaration();
4364 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4365 return FTD->getPreviousDeclaration();
4366 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4367 return CTD->getPreviousDeclaration();
4368 return 0;
4369}
4370
John McCalld226f652010-08-21 09:40:31 +00004371DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004372Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4373 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004374 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004375 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004376 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004377 SourceLocation TemplateNameLoc,
4378 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004379 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004380 SourceLocation RAngleLoc,
4381 AttributeList *Attr,
4382 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004383 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004384
Douglas Gregorcc636682009-02-17 23:15:12 +00004385 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004386 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004387 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004388 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4389
4390 if (!ClassTemplate) {
4391 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004392 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004393 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4394 return true;
4395 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004396
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004397 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004398 bool isPartialSpecialization = false;
4399
Douglas Gregor88b70942009-02-25 22:02:03 +00004400 // Check the validity of the template headers that introduce this
4401 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004402 // FIXME: We probably shouldn't complain about these headers for
4403 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004404 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004405 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004406 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4407 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004408 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004409 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004410 isExplicitSpecialization,
4411 Invalid);
4412 if (Invalid)
4413 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004414
Abramo Bagnara9b934882010-06-12 08:15:14 +00004415 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4416 if (TemplateParams)
4417 --NumMatchedTemplateParamLists;
4418
Douglas Gregor05396e22009-08-25 17:23:04 +00004419 if (TemplateParams && TemplateParams->size() > 0) {
4420 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004421
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004422 if (TUK == TUK_Friend) {
4423 Diag(KWLoc, diag::err_partial_specialization_friend)
4424 << SourceRange(LAngleLoc, RAngleLoc);
4425 return true;
4426 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004427
Douglas Gregor05396e22009-08-25 17:23:04 +00004428 // C++ [temp.class.spec]p10:
4429 // The template parameter list of a specialization shall not
4430 // contain default template argument values.
4431 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4432 Decl *Param = TemplateParams->getParam(I);
4433 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4434 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004435 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004436 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004437 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004438 }
4439 } else if (NonTypeTemplateParmDecl *NTTP
4440 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4441 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004442 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004443 diag::err_default_arg_in_partial_spec)
4444 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004445 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004446 }
4447 } else {
4448 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004449 if (TTP->hasDefaultArgument()) {
4450 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004451 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004452 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004453 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004454 }
4455 }
4456 }
Douglas Gregora735b202009-10-13 14:39:41 +00004457 } else if (TemplateParams) {
4458 if (TUK == TUK_Friend)
4459 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004460 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004461 SourceRange(TemplateParams->getTemplateLoc(),
4462 TemplateParams->getRAngleLoc()))
4463 << SourceRange(LAngleLoc, RAngleLoc);
4464 else
4465 isExplicitSpecialization = true;
4466 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004467 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004468 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004469 isExplicitSpecialization = true;
4470 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004471
Douglas Gregorcc636682009-02-17 23:15:12 +00004472 // Check that the specialization uses the same tag kind as the
4473 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004474 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4475 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004476 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004477 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004478 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004479 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004480 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004481 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004482 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004483 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004484 diag::note_previous_use);
4485 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4486 }
4487
Douglas Gregor40808ce2009-03-09 23:48:35 +00004488 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004489 TemplateArgumentListInfo TemplateArgs;
4490 TemplateArgs.setLAngleLoc(LAngleLoc);
4491 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004492 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004493
Douglas Gregor925910d2011-01-03 20:35:03 +00004494 // Check for unexpanded parameter packs in any of the template arguments.
4495 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004496 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004497 UPPC_PartialSpecialization))
4498 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004499
Douglas Gregorcc636682009-02-17 23:15:12 +00004500 // Check that the template argument list is well-formed for this
4501 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004502 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004503 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4504 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004505 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004506
Douglas Gregor910f8002010-11-07 23:05:16 +00004507 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004508 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004509
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004510 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004511 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004512 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004513 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004514 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004515 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004516 return true;
4517
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004518 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004519 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004520 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004521 TemplateArgs.size())) {
4522 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4523 << ClassTemplate->getDeclName();
4524 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004525 }
4526 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004527
Douglas Gregorcc636682009-02-17 23:15:12 +00004528 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004529 ClassTemplateSpecializationDecl *PrevDecl = 0;
4530
4531 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004532 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004533 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004534 = ClassTemplate->findPartialSpecialization(Converted.data(),
4535 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004536 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004537 else
4538 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004539 = ClassTemplate->findSpecialization(Converted.data(),
4540 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004541
4542 ClassTemplateSpecializationDecl *Specialization = 0;
4543
Douglas Gregor88b70942009-02-25 22:02:03 +00004544 // Check whether we can declare a class template specialization in
4545 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004546 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004547 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4548 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004549 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004550 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004551
Douglas Gregorb88e8882009-07-30 17:40:51 +00004552 // The canonical type
4553 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004554 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004555 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004556 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004557 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004558 // arguments was referenced but not declared, or we're only
4559 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004560 // declaration node as our own, updating its source location to
4561 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004562 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004563 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004564 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004565 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004566 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004567 // Build the canonical type that describes the converted template
4568 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004569 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4570 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004571 Converted.data(),
4572 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004573
4574 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004575 ClassTemplate->getInjectedClassNameSpecialization())) {
4576 // C++ [temp.class.spec]p9b3:
4577 //
4578 // -- The argument list of the specialization shall not be identical
4579 // to the implicit argument list of the primary template.
4580 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4581 << (TUK == TUK_Definition)
4582 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4583 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4584 ClassTemplate->getIdentifier(),
4585 TemplateNameLoc,
4586 Attr,
4587 TemplateParams,
4588 AS_none);
4589 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004590
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004591 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004592 ClassTemplatePartialSpecializationDecl *PrevPartial
4593 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004594 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004595 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004596 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004597 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004598 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004599 TemplateNameLoc,
4600 TemplateParams,
4601 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004602 Converted.data(),
4603 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004604 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004605 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004606 PrevPartial,
4607 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004608 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004609 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004610 Partial->setTemplateParameterListsInfo(Context,
4611 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004612 (TemplateParameterList**) TemplateParameterLists.release());
4613 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004614
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004615 if (!PrevPartial)
4616 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004617 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004618
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004619 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004620 // template specialization, make a note of that.
4621 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4622 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004623
Douglas Gregor031a5882009-06-13 00:26:55 +00004624 // Check that all of the template parameters of the class template
4625 // partial specialization are deducible from the template
4626 // arguments. If not, this class template partial specialization
4627 // will never be used.
4628 llvm::SmallVector<bool, 8> DeducibleParams;
4629 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004630 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004631 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004632 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004633 unsigned NumNonDeducible = 0;
4634 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4635 if (!DeducibleParams[I])
4636 ++NumNonDeducible;
4637
4638 if (NumNonDeducible) {
4639 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4640 << (NumNonDeducible > 1)
4641 << SourceRange(TemplateNameLoc, RAngleLoc);
4642 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4643 if (!DeducibleParams[I]) {
4644 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4645 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004646 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004647 diag::note_partial_spec_unused_parameter)
4648 << Param->getDeclName();
4649 else
Mike Stump1eb44332009-09-09 15:08:12 +00004650 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004651 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004652 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004653 }
4654 }
4655 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004656 } else {
4657 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004658 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004659 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004660 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004661 ClassTemplate->getDeclContext(),
4662 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004663 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004664 Converted.data(),
4665 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004666 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004667 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004668 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004669 Specialization->setTemplateParameterListsInfo(Context,
4670 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004671 (TemplateParameterList**) TemplateParameterLists.release());
4672 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004673
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004674 if (!PrevDecl)
4675 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004676
4677 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004678 }
4679
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004680 // C++ [temp.expl.spec]p6:
4681 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004682 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004683 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004684 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004685 // use occurs; no diagnostic is required.
4686 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004687 bool Okay = false;
4688 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4689 // Is there any previous explicit specialization declaration?
4690 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4691 Okay = true;
4692 break;
4693 }
4694 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004695
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004696 if (!Okay) {
4697 SourceRange Range(TemplateNameLoc, RAngleLoc);
4698 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4699 << Context.getTypeDeclType(Specialization) << Range;
4700
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004701 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004702 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004703 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004704 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004705 return true;
4706 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004708
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004709 // If this is not a friend, note that this is an explicit specialization.
4710 if (TUK != TUK_Friend)
4711 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004712
4713 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004714 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004715 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004716 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004717 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004718 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004719 Diag(Def->getLocation(), diag::note_previous_definition);
4720 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004721 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004722 }
4723 }
4724
John McCall7f1b9872010-12-18 03:30:47 +00004725 if (Attr)
4726 ProcessDeclAttributeList(S, Specialization, Attr);
4727
Douglas Gregorfc705b82009-02-26 22:19:44 +00004728 // Build the fully-sugared type for this class template
4729 // specialization as the user wrote in the specialization
4730 // itself. This means that we'll pretty-print the type retrieved
4731 // from the specialization's declaration the way that the user
4732 // actually wrote the specialization, rather than formatting the
4733 // name based on the "canonical" representation used to store the
4734 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004735 TypeSourceInfo *WrittenTy
4736 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4737 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004738 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004739 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004740 if (TemplateParams)
4741 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004742 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004743 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004744
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004745 // C++ [temp.expl.spec]p9:
4746 // A template explicit specialization is in the scope of the
4747 // namespace in which the template was defined.
4748 //
4749 // We actually implement this paragraph where we set the semantic
4750 // context (in the creation of the ClassTemplateSpecializationDecl),
4751 // but we also maintain the lexical context where the actual
4752 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004753 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004754
Douglas Gregorcc636682009-02-17 23:15:12 +00004755 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004756 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004757 Specialization->startDefinition();
4758
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004759 if (TUK == TUK_Friend) {
4760 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4761 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004762 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004763 /*FIXME:*/KWLoc);
4764 Friend->setAccess(AS_public);
4765 CurContext->addDecl(Friend);
4766 } else {
4767 // Add the specialization into its lexical context, so that it can
4768 // be seen when iterating through the list of declarations in that
4769 // context. However, specializations are not found by name lookup.
4770 CurContext->addDecl(Specialization);
4771 }
John McCalld226f652010-08-21 09:40:31 +00004772 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004773}
Douglas Gregord57959a2009-03-27 23:10:48 +00004774
John McCalld226f652010-08-21 09:40:31 +00004775Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004776 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004777 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004778 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4779}
4780
John McCalld226f652010-08-21 09:40:31 +00004781Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004782 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004783 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004784 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004785 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004786
Douglas Gregor52591bf2009-06-24 00:54:41 +00004787 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004788 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004789 }
Mike Stump1eb44332009-09-09 15:08:12 +00004790
Douglas Gregor52591bf2009-06-24 00:54:41 +00004791 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004792
John McCalld226f652010-08-21 09:40:31 +00004793 Decl *DP = HandleDeclarator(ParentScope, D,
4794 move(TemplateParameterLists),
4795 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004796 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004797 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004798 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004799 FunctionTemplate->getTemplatedDecl());
4800 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4801 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4802 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004803}
4804
John McCall75042392010-02-11 01:33:53 +00004805/// \brief Strips various properties off an implicit instantiation
4806/// that has just been explicitly specialized.
4807static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004808 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004809
4810 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4811 FD->setInlineSpecified(false);
4812 }
4813}
4814
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004815/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004816/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004817/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004818/// new specialization/instantiation will have any effect.
4819///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004820/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004821/// instantiation.
4822///
4823/// \param NewTSK the kind of the new explicit specialization or instantiation.
4824///
4825/// \param PrevDecl the previous declaration of the entity.
4826///
4827/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4828///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004829/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004830/// declaration was instantiated (either implicitly or explicitly).
4831///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004832/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004833/// specialization or instantiation has no effect and should be ignored.
4834///
4835/// \returns true if there was an error that should prevent the introduction of
4836/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004837bool
4838Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4839 TemplateSpecializationKind NewTSK,
4840 NamedDecl *PrevDecl,
4841 TemplateSpecializationKind PrevTSK,
4842 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004843 bool &HasNoEffect) {
4844 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004845
Douglas Gregor454885e2009-10-15 15:54:05 +00004846 switch (NewTSK) {
4847 case TSK_Undeclared:
4848 case TSK_ImplicitInstantiation:
4849 assert(false && "Don't check implicit instantiations here");
4850 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004851
Douglas Gregor454885e2009-10-15 15:54:05 +00004852 case TSK_ExplicitSpecialization:
4853 switch (PrevTSK) {
4854 case TSK_Undeclared:
4855 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004856 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004857 // explicitly specialized or has merely been mentioned without any
4858 // instantiation.
4859 return false;
4860
4861 case TSK_ImplicitInstantiation:
4862 if (PrevPointOfInstantiation.isInvalid()) {
4863 // The declaration itself has not actually been instantiated, so it is
4864 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004865 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004866 return false;
4867 }
4868 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004869
Douglas Gregor454885e2009-10-15 15:54:05 +00004870 case TSK_ExplicitInstantiationDeclaration:
4871 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004872 assert((PrevTSK == TSK_ImplicitInstantiation ||
4873 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004874 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004875
Douglas Gregor454885e2009-10-15 15:54:05 +00004876 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004877 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004878 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004879 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004880 // implicit instantiation to take place, in every translation unit in
4881 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004882 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4883 // Is there any previous explicit specialization declaration?
4884 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4885 return false;
4886 }
4887
Douglas Gregor0d035142009-10-27 18:42:08 +00004888 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004889 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004890 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004891 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004892
Douglas Gregor454885e2009-10-15 15:54:05 +00004893 return true;
4894 }
4895 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004896
Douglas Gregor454885e2009-10-15 15:54:05 +00004897 case TSK_ExplicitInstantiationDeclaration:
4898 switch (PrevTSK) {
4899 case TSK_ExplicitInstantiationDeclaration:
4900 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004901 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004902 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004903
Douglas Gregor454885e2009-10-15 15:54:05 +00004904 case TSK_Undeclared:
4905 case TSK_ImplicitInstantiation:
4906 // We're explicitly instantiating something that may have already been
4907 // implicitly instantiated; that's fine.
4908 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004909
Douglas Gregor454885e2009-10-15 15:54:05 +00004910 case TSK_ExplicitSpecialization:
4911 // C++0x [temp.explicit]p4:
4912 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004913 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004914 // specialization for that template, the explicit instantiation has no
4915 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004916 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004917 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004918
Douglas Gregor454885e2009-10-15 15:54:05 +00004919 case TSK_ExplicitInstantiationDefinition:
4920 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004921 // If an entity is the subject of both an explicit instantiation
4922 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004923 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004924 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004925 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004926 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004927 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004928 assert(PrevPointOfInstantiation.isValid() &&
4929 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004930 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004931 return false;
4932 }
4933 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004934
Douglas Gregor454885e2009-10-15 15:54:05 +00004935 case TSK_ExplicitInstantiationDefinition:
4936 switch (PrevTSK) {
4937 case TSK_Undeclared:
4938 case TSK_ImplicitInstantiation:
4939 // We're explicitly instantiating something that may have already been
4940 // implicitly instantiated; that's fine.
4941 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004942
Douglas Gregor454885e2009-10-15 15:54:05 +00004943 case TSK_ExplicitSpecialization:
4944 // C++ DR 259, C++0x [temp.explicit]p4:
4945 // For a given set of template parameters, if an explicit
4946 // instantiation of a template appears after a declaration of
4947 // an explicit specialization for that template, the explicit
4948 // instantiation has no effect.
4949 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004950 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004951 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004952 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004953 if (!getLangOptions().CPlusPlus0x) {
4954 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004955 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004956 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004957 diag::note_previous_template_specialization);
4958 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004959 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004960 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004961
Douglas Gregor454885e2009-10-15 15:54:05 +00004962 case TSK_ExplicitInstantiationDeclaration:
4963 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004964 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004965 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004966
Douglas Gregor454885e2009-10-15 15:54:05 +00004967 case TSK_ExplicitInstantiationDefinition:
4968 // C++0x [temp.spec]p5:
4969 // For a given template and a given set of template-arguments,
4970 // - an explicit instantiation definition shall appear at most once
4971 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004972 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004973 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004974 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004975 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004976 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004977 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004978 }
4979 break;
4980 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004981
Douglas Gregor454885e2009-10-15 15:54:05 +00004982 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004983
Douglas Gregor454885e2009-10-15 15:54:05 +00004984 return false;
4985}
4986
John McCallaf2094e2010-04-08 09:05:18 +00004987/// \brief Perform semantic analysis for the given dependent function
4988/// template specialization. The only possible way to get a dependent
4989/// function template specialization is with a friend declaration,
4990/// like so:
4991///
4992/// template <class T> void foo(T);
4993/// template <class T> class A {
4994/// friend void foo<>(T);
4995/// };
4996///
4997/// There really isn't any useful analysis we can do here, so we
4998/// just store the information.
4999bool
5000Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5001 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5002 LookupResult &Previous) {
5003 // Remove anything from Previous that isn't a function template in
5004 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005005 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005006 LookupResult::Filter F = Previous.makeFilter();
5007 while (F.hasNext()) {
5008 NamedDecl *D = F.next()->getUnderlyingDecl();
5009 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005010 !FDLookupContext->InEnclosingNamespaceSetOf(
5011 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005012 F.erase();
5013 }
5014 F.done();
5015
5016 // Should this be diagnosed here?
5017 if (Previous.empty()) return true;
5018
5019 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5020 ExplicitTemplateArgs);
5021 return false;
5022}
5023
Abramo Bagnarae03db982010-05-20 15:32:11 +00005024/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005025/// specialization.
5026///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005027/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005028/// explicit function template specialization. On successful completion,
5029/// the function declaration \p FD will become a function template
5030/// specialization.
5031///
5032/// \param FD the function declaration, which will be updated to become a
5033/// function template specialization.
5034///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005035/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5036/// if any. Note that this may be valid info even when 0 arguments are
5037/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5038/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005039///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005040/// \param PrevDecl the set of declarations that may be specialized by
5041/// this function specialization.
5042bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005043Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005044 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005045 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005046 // The set of function template specializations that could match this
5047 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005048 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005049
Sebastian Redl7a126a42010-08-31 00:36:30 +00005050 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005051 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5052 I != E; ++I) {
5053 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5054 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005055 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005056 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005057 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5058 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005059 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005060
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005061 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005062 // A trailing template-argument can be left unspecified in the
5063 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005064 // provided it can be deduced from the function argument type.
5065 // Perform template argument deduction to determine whether we may be
5066 // specializing this template.
5067 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005068 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005069 FunctionDecl *Specialization = 0;
5070 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005071 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005072 FD->getType(),
5073 Specialization,
5074 Info)) {
5075 // FIXME: Template argument deduction failed; record why it failed, so
5076 // that we can provide nifty diagnostics.
5077 (void)TDK;
5078 continue;
5079 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005080
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005081 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005082 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005083 }
5084 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005085
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005086 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005087 UnresolvedSetIterator Result
5088 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005089 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005090 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005091 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005092 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005093 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005094 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005095 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005096 return true;
John McCallc373d482010-01-27 01:50:18 +00005097
5098 // Ignore access information; it doesn't figure into redeclaration checking.
5099 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005100
5101 FunctionTemplateSpecializationInfo *SpecInfo
5102 = Specialization->getTemplateSpecializationInfo();
5103 assert(SpecInfo && "Function template specialization info missing?");
5104 {
5105 // Note: do not overwrite location info if previous template
5106 // specialization kind was explicit.
5107 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5108 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5109 Specialization->setLocation(FD->getLocation());
5110 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005111
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005112 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005113 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005114
5115 // If this is a friend declaration, then we're not really declaring
5116 // an explicit specialization.
5117 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005118
Douglas Gregord5cb8762009-10-07 00:13:32 +00005119 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005120 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005121 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005122 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005123 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005124 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005125 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005126
5127 // C++ [temp.expl.spec]p6:
5128 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005129 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005130 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005131 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005132 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005133 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005134 if (!isFriend &&
5135 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005136 TSK_ExplicitSpecialization,
5137 Specialization,
5138 SpecInfo->getTemplateSpecializationKind(),
5139 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005140 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005141 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005142
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005143 // Mark the prior declaration as an explicit specialization, so that later
5144 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005145 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005146 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005147 MarkUnusedFileScopedDecl(Specialization);
5148 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005149
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005150 // Turn the given function declaration into a function template
5151 // specialization, with the template arguments from the previous
5152 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005153 // Take copies of (semantic and syntactic) template argument lists.
5154 const TemplateArgumentList* TemplArgs = new (Context)
5155 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5156 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5157 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005158 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005159 TemplArgs, /*InsertPos=*/0,
5160 SpecInfo->getTemplateSpecializationKind(),
5161 TemplArgsAsWritten);
5162
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005163 // The "previous declaration" for this function template specialization is
5164 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005165 Previous.clear();
5166 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005167 return false;
5168}
5169
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005170/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005171/// specialization.
5172///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005173/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005174/// explicit member function specialization. On successful completion,
5175/// the function declaration \p FD will become a member function
5176/// specialization.
5177///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005178/// \param Member the member declaration, which will be updated to become a
5179/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005180///
John McCall68263142009-11-18 22:49:29 +00005181/// \param Previous the set of declarations, one of which may be specialized
5182/// by this function specialization; the set will be modified to contain the
5183/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005184bool
John McCall68263142009-11-18 22:49:29 +00005185Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005186 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005187
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005188 // Try to find the member we are instantiating.
5189 NamedDecl *Instantiation = 0;
5190 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005191 MemberSpecializationInfo *MSInfo = 0;
5192
John McCall68263142009-11-18 22:49:29 +00005193 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005194 // Nowhere to look anyway.
5195 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005196 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5197 I != E; ++I) {
5198 NamedDecl *D = (*I)->getUnderlyingDecl();
5199 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005200 if (Context.hasSameType(Function->getType(), Method->getType())) {
5201 Instantiation = Method;
5202 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005203 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005204 break;
5205 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005206 }
5207 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005208 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005209 VarDecl *PrevVar;
5210 if (Previous.isSingleResult() &&
5211 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005212 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005213 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005214 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005215 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005216 }
5217 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005218 CXXRecordDecl *PrevRecord;
5219 if (Previous.isSingleResult() &&
5220 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5221 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005222 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005223 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005224 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005225 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005226
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005227 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005228 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005229 // specializations are always out-of-line, the caller will complain about
5230 // this mismatch later.
5231 return false;
5232 }
John McCall77e8b112010-04-13 20:37:33 +00005233
5234 // If this is a friend, just bail out here before we start turning
5235 // things into explicit specializations.
5236 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5237 // Preserve instantiation information.
5238 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5239 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5240 cast<CXXMethodDecl>(InstantiatedFrom),
5241 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5242 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5243 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5244 cast<CXXRecordDecl>(InstantiatedFrom),
5245 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5246 }
5247
5248 Previous.clear();
5249 Previous.addDecl(Instantiation);
5250 return false;
5251 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005252
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005253 // Make sure that this is a specialization of a member.
5254 if (!InstantiatedFrom) {
5255 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5256 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005257 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5258 return true;
5259 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005260
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005261 // C++ [temp.expl.spec]p6:
5262 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005263 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005264 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005265 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005266 // use occurs; no diagnostic is required.
5267 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005268
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005269 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005270 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5271 TSK_ExplicitSpecialization,
5272 Instantiation,
5273 MSInfo->getTemplateSpecializationKind(),
5274 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005275 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005276 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005277
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005278 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005279 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005280 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005281 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005282 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005283 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005284
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005285 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005286 // the original declaration to note that it is an explicit specialization
5287 // (if it was previously an implicit instantiation). This latter step
5288 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005289 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005290 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5291 if (InstantiationFunction->getTemplateSpecializationKind() ==
5292 TSK_ImplicitInstantiation) {
5293 InstantiationFunction->setTemplateSpecializationKind(
5294 TSK_ExplicitSpecialization);
5295 InstantiationFunction->setLocation(Member->getLocation());
5296 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005297
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005298 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5299 cast<CXXMethodDecl>(InstantiatedFrom),
5300 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005301 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005302 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005303 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5304 if (InstantiationVar->getTemplateSpecializationKind() ==
5305 TSK_ImplicitInstantiation) {
5306 InstantiationVar->setTemplateSpecializationKind(
5307 TSK_ExplicitSpecialization);
5308 InstantiationVar->setLocation(Member->getLocation());
5309 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005310
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005311 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5312 cast<VarDecl>(InstantiatedFrom),
5313 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005314 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005315 } else {
5316 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005317 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5318 if (InstantiationClass->getTemplateSpecializationKind() ==
5319 TSK_ImplicitInstantiation) {
5320 InstantiationClass->setTemplateSpecializationKind(
5321 TSK_ExplicitSpecialization);
5322 InstantiationClass->setLocation(Member->getLocation());
5323 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005324
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005325 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005326 cast<CXXRecordDecl>(InstantiatedFrom),
5327 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005328 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005329
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005330 // Save the caller the trouble of having to figure out which declaration
5331 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005332 Previous.clear();
5333 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005334 return false;
5335}
5336
Douglas Gregor558c0322009-10-14 23:41:34 +00005337/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005338///
5339/// \returns true if a serious error occurs, false otherwise.
5340static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005341 SourceLocation InstLoc,
5342 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005343 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5344 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005345
Douglas Gregor669eed82010-07-13 00:10:04 +00005346 if (CurContext->isRecord()) {
5347 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5348 << D;
5349 return true;
5350 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005351
Douglas Gregor558c0322009-10-14 23:41:34 +00005352 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005353 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005354 // template.
5355 //
5356 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005357 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005358 !CurContext->Encloses(OrigContext)) {
5359 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005360 S.Diag(InstLoc,
5361 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005362 diag::err_explicit_instantiation_out_of_scope
5363 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005364 << D << NS;
5365 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005366 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005367 S.getLangOptions().CPlusPlus0x?
5368 diag::err_explicit_instantiation_must_be_global
5369 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005370 << D;
5371 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005372 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005373 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005374
Douglas Gregor558c0322009-10-14 23:41:34 +00005375 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005376 // If the name declared in the explicit instantiation is an unqualified
5377 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005378 // its template is declared or, if that namespace is inline (7.3.1), any
5379 // namespace from its enclosing namespace set.
5380 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005381 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005382
5383 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005384 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005385
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005386 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005387 S.getLangOptions().CPlusPlus0x?
5388 diag::err_explicit_instantiation_unqualified_wrong_namespace
5389 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005390 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005391 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005392 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005393}
5394
5395/// \brief Determine whether the given scope specifier has a template-id in it.
5396static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5397 if (!SS.isSet())
5398 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005399
Douglas Gregor558c0322009-10-14 23:41:34 +00005400 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005401 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005402 // or a static data member of a class template specialization, the name of
5403 // the class template specialization in the qualified-id for the member
5404 // name shall be a simple-template-id.
5405 //
5406 // C++98 has the same restriction, just worded differently.
5407 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5408 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005409 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005410 if (isa<TemplateSpecializationType>(T))
5411 return true;
5412
5413 return false;
5414}
5415
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005416// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005417DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005418Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005419 SourceLocation ExternLoc,
5420 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005421 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005422 SourceLocation KWLoc,
5423 const CXXScopeSpec &SS,
5424 TemplateTy TemplateD,
5425 SourceLocation TemplateNameLoc,
5426 SourceLocation LAngleLoc,
5427 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005428 SourceLocation RAngleLoc,
5429 AttributeList *Attr) {
5430 // Find the class template we're specializing
5431 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005432 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005433 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5434
5435 // Check that the specialization uses the same tag kind as the
5436 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005437 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5438 assert(Kind != TTK_Enum &&
5439 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005440 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005441 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005442 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005443 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005444 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005445 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005446 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005447 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005448 diag::note_previous_use);
5449 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5450 }
5451
Douglas Gregor558c0322009-10-14 23:41:34 +00005452 // C++0x [temp.explicit]p2:
5453 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005454 // definition and an explicit instantiation declaration. An explicit
5455 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005456 TemplateSpecializationKind TSK
5457 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5458 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005459
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005460 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005461 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005462 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005463
5464 // Check that the template argument list is well-formed for this
5465 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005466 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005467 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5468 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005469 return true;
5470
Douglas Gregor910f8002010-11-07 23:05:16 +00005471 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005472 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005473
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005474 // Find the class template specialization declaration that
5475 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005476 void *InsertPos = 0;
5477 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005478 = ClassTemplate->findSpecialization(Converted.data(),
5479 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005480
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005481 TemplateSpecializationKind PrevDecl_TSK
5482 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5483
Douglas Gregord5cb8762009-10-07 00:13:32 +00005484 // C++0x [temp.explicit]p2:
5485 // [...] An explicit instantiation shall appear in an enclosing
5486 // namespace of its template. [...]
5487 //
5488 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005489 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5490 SS.isSet()))
5491 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005492
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005493 ClassTemplateSpecializationDecl *Specialization = 0;
5494
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005495 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005496 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005497 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005498 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005499 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005500 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005501 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005502
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005503 // Even though HasNoEffect == true means that this explicit instantiation
5504 // has no effect on semantics, we go on to put its syntax in the AST.
5505
5506 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5507 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005508 // Since the only prior class template specialization with these
5509 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005510 // declaration node as our own, updating the source location
5511 // for the template name to reflect our new declaration.
5512 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005513 Specialization = PrevDecl;
5514 Specialization->setLocation(TemplateNameLoc);
5515 PrevDecl = 0;
5516 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005517 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005518
Douglas Gregor52604ab2009-09-11 21:19:12 +00005519 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005520 // Create a new class template specialization declaration node for
5521 // this explicit specialization.
5522 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005523 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005524 ClassTemplate->getDeclContext(),
5525 TemplateNameLoc,
5526 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005527 Converted.data(),
5528 Converted.size(),
5529 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005530 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005531
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005532 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005533 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005534 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005535 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005536 }
5537
5538 // Build the fully-sugared type for this explicit instantiation as
5539 // the user wrote in the explicit instantiation itself. This means
5540 // that we'll pretty-print the type retrieved from the
5541 // specialization's declaration the way that the user actually wrote
5542 // the explicit instantiation, rather than formatting the name based
5543 // on the "canonical" representation used to store the template
5544 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005545 TypeSourceInfo *WrittenTy
5546 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5547 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005548 Context.getTypeDeclType(Specialization));
5549 Specialization->setTypeAsWritten(WrittenTy);
5550 TemplateArgsIn.release();
5551
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005552 // Set source locations for keywords.
5553 Specialization->setExternLoc(ExternLoc);
5554 Specialization->setTemplateKeywordLoc(TemplateLoc);
5555
5556 // Add the explicit instantiation into its lexical context. However,
5557 // since explicit instantiations are never found by name lookup, we
5558 // just put it into the declaration context directly.
5559 Specialization->setLexicalDeclContext(CurContext);
5560 CurContext->addDecl(Specialization);
5561
5562 // Syntax is now OK, so return if it has no other effect on semantics.
5563 if (HasNoEffect) {
5564 // Set the template specialization kind.
5565 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005566 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005567 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005568
5569 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005570 // A definition of a class template or class member template
5571 // shall be in scope at the point of the explicit instantiation of
5572 // the class template or class member template.
5573 //
5574 // This check comes when we actually try to perform the
5575 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005576 ClassTemplateSpecializationDecl *Def
5577 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005578 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005579 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005580 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005581 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005582 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005583 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5584 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005585
Douglas Gregor0d035142009-10-27 18:42:08 +00005586 // Instantiate the members of this class template specialization.
5587 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005588 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005589 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005590 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5591
5592 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5593 // TSK_ExplicitInstantiationDefinition
5594 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5595 TSK == TSK_ExplicitInstantiationDefinition)
5596 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005597
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005598 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005599 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005600
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005601 // Set the template specialization kind.
5602 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005603 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005604}
5605
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005606// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005607DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005608Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005609 SourceLocation ExternLoc,
5610 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005611 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005612 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005613 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005614 IdentifierInfo *Name,
5615 SourceLocation NameLoc,
5616 AttributeList *Attr) {
5617
Douglas Gregor402abb52009-05-28 23:31:59 +00005618 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005619 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005620 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005621 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5622 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005623 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005624 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005625 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5626
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005627 if (!TagD)
5628 return true;
5629
John McCalld226f652010-08-21 09:40:31 +00005630 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005631 if (Tag->isEnum()) {
5632 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5633 << Context.getTypeDeclType(Tag);
5634 return true;
5635 }
5636
Douglas Gregord0c87372009-05-27 17:30:49 +00005637 if (Tag->isInvalidDecl())
5638 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005639
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005640 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5641 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5642 if (!Pattern) {
5643 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5644 << Context.getTypeDeclType(Record);
5645 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5646 return true;
5647 }
5648
Douglas Gregor558c0322009-10-14 23:41:34 +00005649 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005650 // If the explicit instantiation is for a class or member class, the
5651 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005652 // simple-template-id.
5653 //
5654 // C++98 has the same restriction, just worded differently.
5655 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005656 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005657 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005658
Douglas Gregor558c0322009-10-14 23:41:34 +00005659 // C++0x [temp.explicit]p2:
5660 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005661 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005662 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005663 TemplateSpecializationKind TSK
5664 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5665 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005666
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005667 // C++0x [temp.explicit]p2:
5668 // [...] An explicit instantiation shall appear in an enclosing
5669 // namespace of its template. [...]
5670 //
5671 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005672 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005673
Douglas Gregor454885e2009-10-15 15:54:05 +00005674 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005675 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005676 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005677 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005678 PrevDecl = Record;
5679 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005680 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005681 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005682 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005683 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005684 PrevDecl,
5685 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005686 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005687 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005688 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005689 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005690 return TagD;
5691 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005692
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005693 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005694 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005695 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005696 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005697 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005698 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005699 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005700 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005701 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005702 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5703 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005704 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5705 << Pattern;
5706 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005707 } else {
5708 if (InstantiateClass(NameLoc, Record, Def,
5709 getTemplateInstantiationArgs(Record),
5710 TSK))
5711 return true;
5712
Douglas Gregor952b0172010-02-11 01:04:33 +00005713 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005714 if (!RecordDef)
5715 return true;
5716 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005717 }
5718
Douglas Gregor0d035142009-10-27 18:42:08 +00005719 // Instantiate all of the members of the class.
5720 InstantiateClassMembers(NameLoc, RecordDef,
5721 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005722
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005723 if (TSK == TSK_ExplicitInstantiationDefinition)
5724 MarkVTableUsed(NameLoc, RecordDef, true);
5725
Mike Stump390b4cc2009-05-16 07:39:55 +00005726 // FIXME: We don't have any representation for explicit instantiations of
5727 // member classes. Such a representation is not needed for compilation, but it
5728 // should be available for clients that want to see all of the declarations in
5729 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005730 return TagD;
5731}
5732
John McCallf312b1e2010-08-26 23:41:50 +00005733DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5734 SourceLocation ExternLoc,
5735 SourceLocation TemplateLoc,
5736 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005737 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005738 // TODO: check if/when DNInfo should replace Name.
5739 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5740 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005741 if (!Name) {
5742 if (!D.isInvalidType())
5743 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5744 diag::err_explicit_instantiation_requires_name)
5745 << D.getDeclSpec().getSourceRange()
5746 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005747
Douglas Gregord5a423b2009-09-25 18:43:00 +00005748 return true;
5749 }
5750
5751 // The scope passed in may not be a decl scope. Zip up the scope tree until
5752 // we find one that is.
5753 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5754 (S->getFlags() & Scope::TemplateParamScope) != 0)
5755 S = S->getParent();
5756
5757 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005758 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5759 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005760 if (R.isNull())
5761 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005762
Douglas Gregord5a423b2009-09-25 18:43:00 +00005763 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5764 // Cannot explicitly instantiate a typedef.
5765 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5766 << Name;
5767 return true;
5768 }
5769
Douglas Gregor663b5a02009-10-14 20:14:33 +00005770 // C++0x [temp.explicit]p1:
5771 // [...] An explicit instantiation of a function template shall not use the
5772 // inline or constexpr specifiers.
5773 // Presumably, this also applies to member functions of class templates as
5774 // well.
5775 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005776 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005777 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005778 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005779
Douglas Gregor663b5a02009-10-14 20:14:33 +00005780 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005781
Douglas Gregor558c0322009-10-14 23:41:34 +00005782 // C++0x [temp.explicit]p2:
5783 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005784 // definition and an explicit instantiation declaration. An explicit
5785 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005786 TemplateSpecializationKind TSK
5787 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5788 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005789
Abramo Bagnara25777432010-08-11 22:01:17 +00005790 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005791 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005792
5793 if (!R->isFunctionType()) {
5794 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005795 // A [...] static data member of a class template can be explicitly
5796 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005797 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005798 if (Previous.isAmbiguous())
5799 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005800
John McCall1bcee0a2009-12-02 08:25:40 +00005801 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005802 if (!Prev || !Prev->isStaticDataMember()) {
5803 // We expect to see a data data member here.
5804 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5805 << Name;
5806 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5807 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005808 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005809 return true;
5810 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005811
Douglas Gregord5a423b2009-09-25 18:43:00 +00005812 if (!Prev->getInstantiatedFromStaticDataMember()) {
5813 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005814 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005815 diag::err_explicit_instantiation_data_member_not_instantiated)
5816 << Prev;
5817 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5818 // FIXME: Can we provide a note showing where this was declared?
5819 return true;
5820 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005821
Douglas Gregor558c0322009-10-14 23:41:34 +00005822 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005823 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005824 // or a static data member of a class template specialization, the name of
5825 // the class template specialization in the qualified-id for the member
5826 // name shall be a simple-template-id.
5827 //
5828 // C++98 has the same restriction, just worded differently.
5829 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005830 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005831 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005832 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005833
Douglas Gregor558c0322009-10-14 23:41:34 +00005834 // Check the scope of this explicit instantiation.
5835 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005836
Douglas Gregor454885e2009-10-15 15:54:05 +00005837 // Verify that it is okay to explicitly instantiate here.
5838 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5839 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005840 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005841 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005842 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005843 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005844 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005845 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005846 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005847 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005848
Douglas Gregord5a423b2009-09-25 18:43:00 +00005849 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005850 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005851 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005852 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005853
Douglas Gregord5a423b2009-09-25 18:43:00 +00005854 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005855 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005856 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005857
5858 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005859 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005860 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005861 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005862 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5863 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005864 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5865 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005866 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5867 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005868 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005869 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005870 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005871 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005872 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005873
Douglas Gregord5a423b2009-09-25 18:43:00 +00005874 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005875 // A [...] function [...] can be explicitly instantiated from its template.
5876 // A member function [...] of a class template can be explicitly
5877 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005878 // template.
John McCallc373d482010-01-27 01:50:18 +00005879 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005880 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5881 P != PEnd; ++P) {
5882 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005883 if (!HasExplicitTemplateArgs) {
5884 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5885 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5886 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005887
John McCallc373d482010-01-27 01:50:18 +00005888 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005889 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5890 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005891 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005892 }
5893 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005894
Douglas Gregord5a423b2009-09-25 18:43:00 +00005895 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5896 if (!FunTmpl)
5897 continue;
5898
John McCall5769d612010-02-08 23:07:23 +00005899 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005900 FunctionDecl *Specialization = 0;
5901 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005902 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005903 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005904 R, Specialization, Info)) {
5905 // FIXME: Keep track of almost-matches?
5906 (void)TDK;
5907 continue;
5908 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005909
John McCallc373d482010-01-27 01:50:18 +00005910 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005911 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005912
Douglas Gregord5a423b2009-09-25 18:43:00 +00005913 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005914 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005915 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005916 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005917 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5918 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5919 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005920
John McCallc373d482010-01-27 01:50:18 +00005921 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005922 return true;
John McCallc373d482010-01-27 01:50:18 +00005923
5924 // Ignore access control bits, we don't need them for redeclaration checking.
5925 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005926
Douglas Gregor0a897e32009-10-15 17:21:20 +00005927 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005928 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005929 diag::err_explicit_instantiation_member_function_not_instantiated)
5930 << Specialization
5931 << (Specialization->getTemplateSpecializationKind() ==
5932 TSK_ExplicitSpecialization);
5933 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5934 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005935 }
5936
Douglas Gregor0a897e32009-10-15 17:21:20 +00005937 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005938 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5939 PrevDecl = Specialization;
5940
Douglas Gregor0a897e32009-10-15 17:21:20 +00005941 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005942 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005943 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005944 PrevDecl,
5945 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005946 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005947 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005948 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005949
Douglas Gregor0a897e32009-10-15 17:21:20 +00005950 // FIXME: We may still want to build some representation of this
5951 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005952 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005953 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005954 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005955
5956 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005957
Douglas Gregor0a897e32009-10-15 17:21:20 +00005958 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005959 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005960
Douglas Gregor558c0322009-10-14 23:41:34 +00005961 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005962 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005963 // or a static data member of a class template specialization, the name of
5964 // the class template specialization in the qualified-id for the member
5965 // name shall be a simple-template-id.
5966 //
5967 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005968 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005969 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005970 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005971 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005972 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005973 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005974 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005975
Douglas Gregor558c0322009-10-14 23:41:34 +00005976 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005977 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005978 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005979 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005980 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005981
Douglas Gregord5a423b2009-09-25 18:43:00 +00005982 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005983 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005984}
5985
John McCallf312b1e2010-08-26 23:41:50 +00005986TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005987Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5988 const CXXScopeSpec &SS, IdentifierInfo *Name,
5989 SourceLocation TagLoc, SourceLocation NameLoc) {
5990 // This has to hold, because SS is expected to be defined.
5991 assert(Name && "Expected a name in a dependent tag");
5992
5993 NestedNameSpecifier *NNS
5994 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5995 if (!NNS)
5996 return true;
5997
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005998 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005999
Douglas Gregor48c89f42010-04-24 16:38:41 +00006000 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6001 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006002 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006003 return true;
6004 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006005
Douglas Gregor059101f2011-03-02 00:47:37 +00006006 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006007 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006008 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6009
6010 // Create type-source location information for this type.
6011 TypeLocBuilder TLB;
6012 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6013 TL.setKeywordLoc(TagLoc);
6014 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6015 TL.setNameLoc(NameLoc);
6016 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006017}
6018
John McCallf312b1e2010-08-26 23:41:50 +00006019TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006020Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6021 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006022 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006023 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006024 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006025
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006026 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6027 !getLangOptions().CPlusPlus0x)
6028 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006029 << FixItHint::CreateRemoval(TypenameLoc);
6030
Douglas Gregor2494dd02011-03-01 01:34:45 +00006031 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006032 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6033 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006034 if (T.isNull())
6035 return true;
John McCall63b43852010-04-29 23:50:39 +00006036
6037 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6038 if (isa<DependentNameType>(T)) {
6039 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006040 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006041 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006042 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006043 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006044 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006045 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006046 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006047 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006048 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006049
John McCallb3d87482010-08-24 05:47:05 +00006050 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006051}
6052
John McCallf312b1e2010-08-26 23:41:50 +00006053TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006054Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6055 const CXXScopeSpec &SS,
6056 SourceLocation TemplateLoc,
6057 TemplateTy TemplateIn,
6058 SourceLocation TemplateNameLoc,
6059 SourceLocation LAngleLoc,
6060 ASTTemplateArgsPtr TemplateArgsIn,
6061 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006062 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6063 !getLangOptions().CPlusPlus0x)
6064 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006065 << FixItHint::CreateRemoval(TypenameLoc);
6066
6067 // Translate the parser's template argument list in our AST format.
6068 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6069 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6070
6071 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006072 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6073 // Construct a dependent template specialization type.
6074 assert(DTN && "dependent template has non-dependent name?");
6075 assert(DTN->getQualifier()
6076 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6077 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6078 DTN->getQualifier(),
6079 DTN->getIdentifier(),
6080 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006081
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006082 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006083 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006084 DependentTemplateSpecializationTypeLoc SpecTL
6085 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006086 SpecTL.setLAngleLoc(LAngleLoc);
6087 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006088 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006089 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006090 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006091 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6092 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006093 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006094 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006095
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006096 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6097 if (T.isNull())
6098 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006099
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006100 // Provide source-location information for the template specialization
6101 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006102 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006103 TemplateSpecializationTypeLoc SpecTL
6104 = Builder.push<TemplateSpecializationTypeLoc>(T);
6105
6106 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006107 SpecTL.setLAngleLoc(LAngleLoc);
6108 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006109 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006110 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6111 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6112
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006113 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6114 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6115 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006116 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6117
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006118 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6119 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006120}
6121
Douglas Gregora02411e2011-02-27 22:46:49 +00006122
Douglas Gregord57959a2009-03-27 23:10:48 +00006123/// \brief Build the type that describes a C++ typename specifier,
6124/// e.g., "typename T::type".
6125QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006126Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6127 SourceLocation KeywordLoc,
6128 NestedNameSpecifierLoc QualifierLoc,
6129 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006130 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006131 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006132 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006133
John McCall77bb1aa2010-05-01 00:40:08 +00006134 DeclContext *Ctx = computeDeclContext(SS);
6135 if (!Ctx) {
6136 // If the nested-name-specifier is dependent and couldn't be
6137 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006138 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6139 return Context.getDependentNameType(Keyword,
6140 QualifierLoc.getNestedNameSpecifier(),
6141 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006142 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006143
John McCall77bb1aa2010-05-01 00:40:08 +00006144 // If the nested-name-specifier refers to the current instantiation,
6145 // the "typename" keyword itself is superfluous. In C++03, the
6146 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6147 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006148 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006149
John McCall77bb1aa2010-05-01 00:40:08 +00006150 if (RequireCompleteDeclContext(SS, Ctx))
6151 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006152
6153 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006154 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006155 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006156 unsigned DiagID = 0;
6157 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006158 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006159 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006160 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006161 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006162
6163 case LookupResult::FoundUnresolvedValue: {
6164 // We found a using declaration that is a value. Most likely, the using
6165 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006166 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006167 IILoc);
6168 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6169 << Name << Ctx << FullRange;
6170 if (UnresolvedUsingValueDecl *Using
6171 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006172 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006173 Diag(Loc, diag::note_using_value_decl_missing_typename)
6174 << FixItHint::CreateInsertion(Loc, "typename ");
6175 }
6176 }
6177 // Fall through to create a dependent typename type, from which we can recover
6178 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006179
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006180 case LookupResult::NotFoundInCurrentInstantiation:
6181 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006182 return Context.getDependentNameType(Keyword,
6183 QualifierLoc.getNestedNameSpecifier(),
6184 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006185
6186 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006187 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006188 // We found a type. Build an ElaboratedType, since the
6189 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006190 return Context.getElaboratedType(ETK_Typename,
6191 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006192 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006193 }
6194
6195 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006196 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006197 break;
6198
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006199
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006200 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006201 return QualType();
6202
Douglas Gregord57959a2009-03-27 23:10:48 +00006203 case LookupResult::FoundOverloaded:
6204 DiagID = diag::err_typename_nested_not_type;
6205 Referenced = *Result.begin();
6206 break;
6207
John McCall6e247262009-10-10 05:48:19 +00006208 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006209 return QualType();
6210 }
6211
6212 // If we get here, it's because name lookup did not find a
6213 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006214 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006215 IILoc);
6216 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006217 if (Referenced)
6218 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6219 << Name;
6220 return QualType();
6221}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006222
6223namespace {
6224 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006225 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006226 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006227 SourceLocation Loc;
6228 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006229
Douglas Gregor4a959d82009-08-06 16:20:37 +00006230 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006231 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006232
Mike Stump1eb44332009-09-09 15:08:12 +00006233 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006234 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006235 DeclarationName Entity)
6236 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006237 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006238
6239 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006240 /// transformed.
6241 ///
6242 /// For the purposes of type reconstruction, a type has already been
6243 /// transformed if it is NULL or if it is not dependent.
6244 bool AlreadyTransformed(QualType T) {
6245 return T.isNull() || !T->isDependentType();
6246 }
Mike Stump1eb44332009-09-09 15:08:12 +00006247
6248 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006249 /// rebuilt.
6250 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006251
Douglas Gregor4a959d82009-08-06 16:20:37 +00006252 /// \brief Returns the name of the entity whose type is being rebuilt.
6253 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006254
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006255 /// \brief Sets the "base" location and entity when that
6256 /// information is known based on another transformation.
6257 void setBase(SourceLocation Loc, DeclarationName Entity) {
6258 this->Loc = Loc;
6259 this->Entity = Entity;
6260 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006261 };
6262}
6263
Douglas Gregor4a959d82009-08-06 16:20:37 +00006264/// \brief Rebuilds a type within the context of the current instantiation.
6265///
Mike Stump1eb44332009-09-09 15:08:12 +00006266/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006267/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006268/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006269/// partial specialization thereof). This routine will rebuild that type now
6270/// that we have entered the declarator's scope, which may produce different
6271/// canonical types, e.g.,
6272///
6273/// \code
6274/// template<typename T>
6275/// struct X {
6276/// typedef T* pointer;
6277/// pointer data();
6278/// };
6279///
6280/// template<typename T>
6281/// typename X<T>::pointer X<T>::data() { ... }
6282/// \endcode
6283///
Douglas Gregor4714c122010-03-31 17:34:00 +00006284/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006285/// since we do not know that we can look into X<T> when we parsed the type.
6286/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006287/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006288/// as the canonical type of T*, allowing the return types of the out-of-line
6289/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006290TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6291 SourceLocation Loc,
6292 DeclarationName Name) {
6293 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006294 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006295
Douglas Gregor4a959d82009-08-06 16:20:37 +00006296 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6297 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006298}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006299
John McCall60d7b3a2010-08-24 06:29:42 +00006300ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006301 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6302 DeclarationName());
6303 return Rebuilder.TransformExpr(E);
6304}
6305
John McCall63b43852010-04-29 23:50:39 +00006306bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006307 if (SS.isInvalid())
6308 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006309
Douglas Gregor7e384942011-02-25 16:07:42 +00006310 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006311 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6312 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006313 NestedNameSpecifierLoc Rebuilt
6314 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6315 if (!Rebuilt)
6316 return true;
John McCall63b43852010-04-29 23:50:39 +00006317
Douglas Gregor7e384942011-02-25 16:07:42 +00006318 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006319 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006320}
6321
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006322/// \brief Produces a formatted string that describes the binding of
6323/// template parameters to template arguments.
6324std::string
6325Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6326 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006327 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006328}
6329
6330std::string
6331Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6332 const TemplateArgument *Args,
6333 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006334 llvm::SmallString<128> Str;
6335 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006336
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006337 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006338 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006339
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006340 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006341 if (I >= NumArgs)
6342 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006343
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006344 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006345 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006346 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006347 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006348
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006349 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006350 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006351 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006352 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006353 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006354
Douglas Gregor87dd6972010-12-20 16:52:59 +00006355 Out << " = ";
6356 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006357 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006358
6359 Out << ']';
6360 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006361}